Getting batch job id nside ItemProcessListener in spring batch 5

huangapple go评论45阅读模式
英文:

Getting batch job id nside ItemProcessListener in spring batch 5

问题

I want to display batch job id in my logs on err. Any idea? ty.

英文:

My listener is as given below :

@Component
@Slf4j
public class ProcessorExecutionListener implements ItemProcessListener<OriginDAO, DestinationDAO> {


    @Override
    public void onProcessError(OriginDAO originADAO, Exception e) {
        log.error("Want to display batch job id here in the logs. Is it possible?");
    }

}

I want to display batch job id in my logs on err. Any idea? ty.

答案1

得分: 0

以下是要翻译的代码部分:

@Component
@StepScope
@Slf4j
public class ProcessorExecutionListener implements ItemProcessListener<OriginDAO, DestinationDAO> {

	@Value("#{stepExecution}")
	private StepExecution stepExecution;

	@Override
	public void onProcessError(OriginDAO item, Exception e) {
		Long jobId = stepExecution.getJobExecution().getJobId();
		log.error("batch job id = {}", jobId);
	}
	
}

This listener should be registered in the step as a listener as well.

英文:

You can do that by making the listener step-scoped and inject the step execution in it. Here is an example:

@Component
@StepScope
@Slf4j
public class ProcessorExecutionListener implements ItemProcessListener&lt; OriginDAO, DestinationDAO&gt; {

	@Value(&quot;#{stepExecution}&quot;)
	private StepExecution stepExecution;

	@Override
	public void onProcessError(OriginDAO item, Exception e) {
		Long jobId = stepExecution.getJobExecution().getJobId();
		log.error(&quot;batch job id = {}&quot;, jobId);
	}
	
}

This listener should be registered in the step as a listener as well.

huangapple
  • 本文由 发表于 2023年5月15日 02:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定