java - Spring Batch 挂起,没有输出

标签 java spring spring-boot spring-batch

我有一个从 Spring 引导应用程序启动的 Spring Batch 作业,如下所示:

主要内容:

@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 boot 应用程序一直在运行并且从不退出。我还尝试添加一个带有 System.exit(..)@AfterJob,但它也没有帮助。

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

知道我做错了什么吗?

编辑:

看起来 beforeJob 和 afterJob 监听器根本没有触发。

最佳答案

ClassCastExeption 可能是 Spring 不同绑定(bind)的结果(早期在 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;
}
}

关于java - Spring Batch 挂起,没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35397058/

相关文章:

java - 使用 GridBagLayout 将内容对齐到顶部

spring - ChannelInterceptorAdapter 已弃用 - 替代品是什么?

java - 如何在 Spring Rest Controller 中获取发件人的 uri?

spring - 在 protected Spring Boot应用程序中访问静态内容

java - 找不到 org.springframework.beans.factory.annotation.Autowire 的类文件

java - 在 JavaFX 游戏中使用 Node 对象作为 GameObject 是否高效?

java - Sublime Text 3 Java 自动完成

java - 如何使用 CXF 客户端发布 XML?

java - 如何修复 : My Web App forces all users to "interact" with the same list,,而不是为每个用户提供自己的列表

java - sun.rmi.transport.tcp.TCPTransport 使用 100% CPU