How can I get jobId in ItemWriter in spring batch

spring, spring-batch

Solution

I don't know about your method doing it with xml, but here is how I would do it:

You need to add a field and a method to your class (be it writer or reader) such as

private Long jobId;

@BeforeStep
public void getInterstepData(StepExecution stepExecution) {
    JobExecution jobExecution = stepExecution.getJobExecution();
    this.jobId = jobExecution.getJobId();
}

In this case the method will be called before the step and the id will be accessible through the field.

Hope that helps.

Problem

Is it possible to get jobId or JobName in ItemWriter in spring batch. I read about late binding but not sure how to use it to get job name or job id in Item Writer. I configured my writer as ``` <bean id="myWriter" ref="com.eg.man.EodWriter" scope="step"> <property name="jobInstanceId" value="#{stepExecution.jobExecution.jobId}"/> </bean> ``` but I don't know how to use it in writer class to get value. Edit I also found this link which say you can get id in Write but again how to use it in writer class

Original source

Related problems