[英]Spring Batch Job logging on completion
I have a Spring batch job which runs like this: 我有一个像这样运行的Spring批处理作业:
public void myMethod() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
logger.log("Job Starting");
JobParameters param = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(myJob(), param);
logger.log("Job Finished");
}
This runs asynchronously by using a TaskExecutor
. 通过使用
TaskExecutor
异步运行。 So the second logger.log
logs immediately after the job has been called. 因此,第二个
logger.log
在调用作业后立即记录。
I want the logging to log after myJob()
has finished. 我希望在
myJob()
完成后记录日志。
I have tried changing the actual job to do this but this doesn't seem like the best solution. 我曾尝试更改实际工作以执行此操作,但这似乎不是最佳解决方案。 I also tried looking for methods such as
andThen
and onComplete
but I couldn't find anything. 我也尝试寻找诸如
andThen
和onComplete
方法,但找不到任何东西。
Is there a way to do this? 有没有办法做到这一点?
You might want to have a look at JobExecutionListener
this allows you to do some processing before and after the job execution. 您可能想看看
JobExecutionListener
它使您可以在作业执行之前和之后进行一些处理。
public class DoSomethingAroundJob implements JobExecutionListener {
public void beforeJob(JobExecution jobExecution) {
System.out.println("Called beforeJob().");
}
public void afterJob(JobExecution jobExecution) {
System.out.println("Called afterJob().");
}
}
Then you would need to configure you Job to use this listener. 然后,您需要将Job配置为使用此侦听器。 For example.
例如。
@Bean
public Job myJob(){
return jobs.get("myJob")
.listener(new DoSomethingAroundJob())
.build();
}
There would be other parts of your job, but the above just shows an example of adding a listener wit JavaConfig. 您的工作还有其他部分,但是上面仅显示了添加带有JavaConfig的侦听器的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.