Spring Batch挂起,没有输出

时间:2016-02-14 19:26:27

标签: java spring spring-boot spring-batch

我有一个Spring Batch作业,我是从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
    
  • 工作结束了。我在step3中放了一个Sys.out.print,确实打印了,但是spring boot应用程序一直在运行并且永不退出。我还尝试使用@AfterJob添加System.exit(..),但它也没有帮助。

我正在使用Spring f / w 4.1.8,spring boot 1.2.8和spring batch 3.0.6(我无法升级我的spring-core,因为某些依赖项使用该版本)。

知道我做错了吗?

修改

看起来像beforeJob和afterJob听众根本没有开火。

2 个答案:

答案 0 :(得分:2)

ClassCastExeption可以是Spring不同绑定的结果(早期在xml中,在java中较晚)。尝试在java中完全配置您的批处理。结果可能如下所示(这是在DB中存储的存储库,内存存储库看起来应该类似):

@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;
}
}

答案 1 :(得分:0)

您错过了 @EnableBatchProcessing docs