6

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

Manish
  • 1,946
  • 2
  • 24
  • 36
  • 1
    possible duplicate of [Spring Batch FlatFileItemWriter - How to use stepExecution.jobId to generate file name](http://stackoverflow.com/questions/21549951/spring-batch-flatfileitemwriter-how-to-use-stepexecution-jobid-to-generate-fil) – Luca Basso Ricci Feb 14 '14 at 10:53

1 Answers1

9

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.

Arnaud Potier
  • 1,750
  • 16
  • 28
  • That worked but http://stackoverflow.com/questions/21549951/spring-batch-flatfileitemwriter-how-to-use-stepexecution-jobid-to-generate-fil, seems more proper way of getting this done – Manish Feb 14 '14 at 11:58
  • It is possible to get the jobId directly from `StepExecution` by: `stepExecution.getJobExecutionId();` – Nom1fan Mar 11 '19 at 13:21