gpt4 book ai didi

java - Spring Batch 挂起,没有输出

转载 作者:行者123 更新时间:2023-11-30 08:40:54 24 4
gpt4 key购买 nike

我有一个从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com