Java JobLauncherCommandLineRunner没有';作业完成后,不能退出
我不确定是否正确使用了Java JobLauncherCommandLineRunner没有';作业完成后,不能退出,java,spring,spring-batch,Java,Spring,Spring Batch,我不确定是否正确使用了JobLauncherCommandLineRunner。我正在开发一个批处理作业,它将通过命令行调用,运行到完成,然后停止。它从命令行获取1个参数,我使用: 弹簧靴1.5.9 弹簧批次3.0.8 当我通过命令行调用时,例如: java -Dspring.profiles.active=solr,cassandra -Dspring.config.location=/path/to/job.yml -jar myjob.jar jobParam=/path/to/fil
JobLauncherCommandLineRunner
。我正在开发一个批处理作业,它将通过命令行调用,运行到完成,然后停止。它从命令行获取1个参数,我使用:
- 弹簧靴1.5.9
- 弹簧批次3.0.8
java -Dspring.profiles.active=solr,cassandra -Dspring.config.location=/path/to/job.yml -jar myjob.jar jobParam=/path/to/file.csv
应用程序似乎“永远”运行(至少在作业完成之后)。是否有在作业完成时关闭上下文的配置
目前我的main
非常简单,我希望保持这种方式。但也许我需要自定义逻辑来在作业完成后停止上下文
@SpringBootApplication
public class MyJob {
public static void main(String[] args) {
SpringApplication.run(MyJob.class, args);
}
}
tldr放置
SpringApplication.exit(上下文)在SpringApplication之后运行code>。在main
方法中运行命令
@SpringBootApplication
public class MyJob {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(NingesterApplication.class, args);
System.exit(SpringApplication.exit(context));
}
}
我明白了。有几件关键的事情需要理解:
- 自动向s的应用程序上下文注册李>
- 此
exitdegenerator
(特别是a)将从链接到JobExecutionEvent
的每个JobExecution
中收集
- 为执行的每个作业发布一个
JobExecutionEvent
JobLauncherCommandLineRunner
作为应用程序上下文启动的一部分启动作业
因此,批处理作业将在SpringApplication.run(MyJob.class,args)之前运行到完成代码>返回应用程序上下文(因为作业是上下文启动的一部分)
因此,我需要做的就是在我的应用程序类中再添加一行:
@SpringBootApplication
public class MyJob {
private static final Logger log = LoggerFactory.getLogger(MyJob.class);
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(NingesterApplication.class, args);
// The batch job has finished by this point because the
// ApplicationContext is not 'ready' until the job is finished
// Also, use System.exit to force the Java process to finish with the exit code returned from the Spring App
System.exit(SpringApplication.exit(context));
}
}
然后,确保使用System.exit()
确保应用程序将以与BatchStatus
序数值匹配的退出代码退出
旁注:日志有点误导,因为我看到
启动工作
工作完成了
上下文开始
上下文关闭李>
但在功能上它是有效的:
2018-01-18 12:47:58.363 INFO 1504 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=myjob]] launched with the following parameters: [{jobParam=/path/to/file.csv}]
2018-01-18 12:47:58.399 INFO 1504 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
2018-01-18 12:50:12.347 INFO 1504 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=myjob]] completed with the following parameters: [{jobParam=/path/to/file.csv}] and the following status: [COMPLETED]
2018-01-18 12:50:12.349 INFO 1504 --- [ main] g.n.j.n.ningester.NingesterApplication : Started NingesterApplication in 136.813 seconds (JVM running for 137.195)
2018-01-18 12:50:12.350 INFO 1504 --- [ main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@24313fcc: startup date [Thu Jan 18 12:47:55 PST 2018]; root of context hierarchy