【发布时间】:2018-06-15 16:04:44
【问题描述】:
我正在尝试将 Spring Batch 用于作业。我在两个单独的配置中有两个工作 tempJob 和 tempJob2。当尝试使用命令行参数(-Dspring.batch.job.names=tempJob)运行 tempJob 时,SpringBatch 尝试运行 tempJob 两次,我得到以下错误
2018-06-15 11:36:37.956 信息 14436 --- [主要] o.s.b.c.l.support.SimpleJobLauncher:工作:[SimpleJob: [name=tempJob]] 使用以下参数完成: [{TimeStamp=06152018 11:36:30}] 和以下状态:[已完成] 2018-06-15 11:36:38.049 信息 14436 --- [主要] ConditionEvaluationReportLoggingListener:
启动 ApplicationContext 时出错。显示条件报告 在启用“调试”的情况下重新运行您的应用程序。 2018-06-15 11:36:38.058 错误 14436 --- [main] os.boot.SpringApplication
: 应用程序运行失败java.lang.IllegalStateException:无法执行 CommandLineRunner 在 org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 com.example.springbatchdemo.SpringbatchdemoApplication.main(SpringbatchdemoApplication.java:22) [classes/:na] 由以下原因引起: org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: 作业实例已存在且已完成 参数={时间戳=06152018 11:36:30}。如果你想运行这个工作 再次,更改参数。在 org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:130) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) ~[na:1.8.0_60] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60] 在 java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60] 在 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:181) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 com.sun.proxy.$Proxy48.createJobExecution(Unknown Source) ~[na:na] at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:131) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE] 在 org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:163) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeRegisteredJobs(JobLauncherCommandLineRunner.java:148) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:135) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:128) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE] 在 org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] ... 5个常用框架 省略
2018-06-15 11:36:38.060 信息 14436 --- [主要] s.c.a.AnnotationConfigApplicationContext :关闭 org.springframework.context.annotation.AnnotationConfigApplicationContext@770c2e6b: 启动日期 [Fri Jun 15 11:36:31 EDT 2018];上下文层次的根 2018-06-15 11:36:38.061 信息 14436 --- [主要] o.s.c.support.DefaultLifecycleProcessor :在阶段停止 bean -2147482648 2018-06-15 11:36:38.062 信息 14436 --- [主] ory$ResourceAnnotationApplicationContext:关闭 ResourceAnnotationApplicationContext:com.example.springbatchdemo.config.TempConfig 2018-06-15 11:36:38.063 信息 14436 --- [主要] ory$ResourceAnnotationApplicationContext : 关闭 ResourceAnnotationApplicationContext:com.example.springbatchdemo.config.TempConfig2 2018-06-15 11:36:38.064 信息 14436 --- [主要] o.s.j.e.a.AnnotationMBeanExporter :取消注册 JMX 公开 bean 关闭 2018-06-15 11:36:38.065 INFO 14436 --- [
main] o.s.j.e.a.AnnotationMBeanExporter :取消注册 JMX 暴露的 bean 2018-06-15 11:36:38.065 INFO 14436 --- [
主要] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - 已启动关机... 2018-06-15 11:36:38.098 INFO 14436 --- [
主要] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - 关机完成。
以下是我的 Spring Batch 配置 这是我的主要课程
@SpringBootApplication
@EnableBatchProcessing(modular=true)
public class SpringbatchdemoApplication {
public static void main(String[] args) {
String[] params = addTimestampAsParameter(args);
SpringApplication.run(SpringbatchdemoApplication.class, params);
}
@Bean
public ApplicationContextFactory runTempJob1() {
return new GenericApplicationContextFactory(TempConfig.class);
}
@Bean
public ApplicationContextFactory runTempJob2() {
return new GenericApplicationContextFactory(TempConfig2.class);
}
public static String[] addTimestampAsParameter(final String[] args){
String[] params = null;
if(null != args){
params = new String[args.length+1];
for(int i=0;i<args.length;i++){
params[i]=args[i];
}
}else{
params = new String[1];
}
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy hh:mm:ss");
String timeStamp = sdf.format(new Date());
params[params.length-1] = String.format("TimeStamp=%s", timeStamp);
return params;
}
}
以下是我的配置类
@Configuration
public class TempConfig {
@Autowired
JobBuilderFactory jobBuilder;
@Autowired
StepBuilderFactory stepBuilder;
@Bean
public Tasklet sampleTask(){
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("Executed Temp Step of TempConfig Job -> TempJob");
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Step tempStep(Tasklet sampleTask){
return stepBuilder.get("tempStep").tasklet(sampleTask).build();
}
@Bean
public Job tempJob(){
return jobBuilder.get("tempJob")
.start(tempStep(null))
.build();
}
}
和第二个Job配置
@Configuration
public class TempConfig2 {
@Autowired
JobBuilderFactory jobBuilder;
@Autowired
StepBuilderFactory stepBuilder;
@Bean
public Step tempStep1(){
return stepBuilder.get("tempStep1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("Executed Temp Step of TempConfig2 Job -> TempJob1");
return RepeatStatus.FINISHED;
}
}).build();
}
@Bean
public Job tempJob1(){
return jobBuilder.get("tempJob1")
.start(tempStep1())
.build();
}
}
当我进一步研究时发现,在给 -Dspring.batch.job.names SpringBatch 使用 JobLauncherCommandLineRunner.launchJobFromProperties() 方法来执行作业。此方法尝试使用 executeLocalJobs(jobParameters) 和 executeRegisteredJobs(jobParameters) 执行作业; 在调试时,我发现两种方法都在执行我的工作。
是否有任何解决方案可以使用带有 (modular=true) 的命令行参数只运行一次作业。如果您需要更多信息,请提供帮助并告知。
【问题讨论】:
-
上述问题您得到解决了吗?
-
@Sayali:不,我还没有找到任何解决方案。作为一种变通方法,我已经评论 (modular=true) 并且开发人员正在确保我们的代码中没有相同的 Job 或 Step 名称。如果您找到更好的解决方法或解决方案,请通知我。
标签: java spring spring-boot spring-batch