Spring 批处理挂起,没有输出
Spring Batch hangs with no output
我有一个 Spring 从 Spring 引导应用程序启动的批处理作业,如下所示:
主要:
@SpringBootApplication
@ImportResource("jobApplicationContext.xml")
public class BatchJobRunner {
public static void main(String[] args) {
SpringApplication.run(BatchJobRunner.class, args);
}
}
在我的工作申请上下文中,我有以下项目:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<batch:job id="myJob" job-repository="jobRepository">
<batch:split id="main" task-executor="simpleAsyncTaskExecutor" next="step3">
<batch:flow>
<batch:step id="flow1">
<!-- definition -->
</batch:step>
</batch:flow>
<batch:flow>
<batch:step id="flow2">
<!-- definition -->
</batch:step>
</batch:flow>
</batch:split>
<batch:step id="step3">
<batch:tasklet ref="someTasklet"/>
</batch:step>
</batch:job>
</beans>
最后,我 运行 是这样的:
java -jar my-module.jar
作业开始但是:
它不打印任何东西。这是我的 log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.logger.org.springframework.batch=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
作业最后挂起。我在步骤 3 中放置了一个 Sys.out.print,它确实打印了,但是 spring 启动应用程序保持 运行ning 并且永远不会退出。我还尝试添加一个 @AfterJob
和一个 System.exit(..)
但它也没有帮助。
我正在使用 Spring f/w 4.1.8,spring boot 1.2.8 和 spring batch 3.0.6(我无法升级我的 spring-core,因为某些依赖项使用该版本)。
知道我做错了什么吗?
编辑:
看起来 beforeJob 和 afterJob 侦听器根本没有触发。
您错过了 @EnableBatchProcessing docs。
ClassCastExeption 可能是 Spring 不同绑定的结果(在 xml 早期和 java 后期)。尝试在 java 中完全配置您的批次。结果可能如下所示(这是在数据库中存储的存储库,内存存储库应该看起来类似):
@Configuration
@EnableBatchProcessing
@ComponentScan("my.batch.*")
@ImportResource("classpath:batch-config.xml")
@PropertySource(value = "classpath:batch.properties")
public class BatchConfiguration implements BatchConfigurer {
@Autowired
private DataSource dataSource;
private PlatformTransactionManager transactionManager;
private JobRepository jobRepository;
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
@Override
public JobRepository getJobRepository() throws Exception {
return jobRepository;
}
@Override
public PlatformTransactionManager getTransactionManager() throws Exception {
return transactionManager;
}
@Override
public JobLauncher getJobLauncher() throws Exception {
return jobLauncher;
}
@Override
public JobExplorer getJobExplorer() throws Exception {
return jobExplorer;
}
@PostConstruct
public void initialize() {
try {
transactionManager = new DataSourceTransactionManager(dataSource);
jobRepository = createJobRepository();
jobExplorer = createJobExplorer();
jobLauncher = createJobLauncher();
} catch (Exception ex) {
throw new BatchConfigurationException(ex);
}
}
private JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean repoFactory = new JobRepositoryFactoryBean();
repoFactory.setDataSource(dataSource);
repoFactory.setTransactionManager(transactionManager);
repoFactory.setTablePrefix(PREFIX);
repoFactory.afterPropertiesSet();
return repoFactory.getObject();
}
private JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean explorerFactory = new JobExplorerFactoryBean();
explorerFactory.setDataSource(dataSource);
explorerFactory.setTablePrefix(PREFIX);
explorerFactory.afterPropertiesSet();
return explorerFactory.getObject();
}
private JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
我有一个 Spring 从 Spring 引导应用程序启动的批处理作业,如下所示:
主要:
@SpringBootApplication
@ImportResource("jobApplicationContext.xml")
public class BatchJobRunner {
public static void main(String[] args) {
SpringApplication.run(BatchJobRunner.class, args);
}
}
在我的工作申请上下文中,我有以下项目:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<batch:job id="myJob" job-repository="jobRepository">
<batch:split id="main" task-executor="simpleAsyncTaskExecutor" next="step3">
<batch:flow>
<batch:step id="flow1">
<!-- definition -->
</batch:step>
</batch:flow>
<batch:flow>
<batch:step id="flow2">
<!-- definition -->
</batch:step>
</batch:flow>
</batch:split>
<batch:step id="step3">
<batch:tasklet ref="someTasklet"/>
</batch:step>
</batch:job>
</beans>
最后,我 运行 是这样的:
java -jar my-module.jar
作业开始但是:
它不打印任何东西。这是我的 log4j.properties:
log4j.rootLogger=INFO, stdout log4j.logger.org.springframework.batch=INFO log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
作业最后挂起。我在步骤 3 中放置了一个 Sys.out.print,它确实打印了,但是 spring 启动应用程序保持 运行ning 并且永远不会退出。我还尝试添加一个
@AfterJob
和一个System.exit(..)
但它也没有帮助。
我正在使用 Spring f/w 4.1.8,spring boot 1.2.8 和 spring batch 3.0.6(我无法升级我的 spring-core,因为某些依赖项使用该版本)。
知道我做错了什么吗?
编辑:
看起来 beforeJob 和 afterJob 侦听器根本没有触发。
您错过了 @EnableBatchProcessing docs。
ClassCastExeption 可能是 Spring 不同绑定的结果(在 xml 早期和 java 后期)。尝试在 java 中完全配置您的批次。结果可能如下所示(这是在数据库中存储的存储库,内存存储库应该看起来类似):
@Configuration
@EnableBatchProcessing
@ComponentScan("my.batch.*")
@ImportResource("classpath:batch-config.xml")
@PropertySource(value = "classpath:batch.properties")
public class BatchConfiguration implements BatchConfigurer {
@Autowired
private DataSource dataSource;
private PlatformTransactionManager transactionManager;
private JobRepository jobRepository;
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
@Override
public JobRepository getJobRepository() throws Exception {
return jobRepository;
}
@Override
public PlatformTransactionManager getTransactionManager() throws Exception {
return transactionManager;
}
@Override
public JobLauncher getJobLauncher() throws Exception {
return jobLauncher;
}
@Override
public JobExplorer getJobExplorer() throws Exception {
return jobExplorer;
}
@PostConstruct
public void initialize() {
try {
transactionManager = new DataSourceTransactionManager(dataSource);
jobRepository = createJobRepository();
jobExplorer = createJobExplorer();
jobLauncher = createJobLauncher();
} catch (Exception ex) {
throw new BatchConfigurationException(ex);
}
}
private JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean repoFactory = new JobRepositoryFactoryBean();
repoFactory.setDataSource(dataSource);
repoFactory.setTransactionManager(transactionManager);
repoFactory.setTablePrefix(PREFIX);
repoFactory.afterPropertiesSet();
return repoFactory.getObject();
}
private JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean explorerFactory = new JobExplorerFactoryBean();
explorerFactory.setDataSource(dataSource);
explorerFactory.setTablePrefix(PREFIX);
explorerFactory.afterPropertiesSet();
return explorerFactory.getObject();
}
private JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}