Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jobId in ItemWriter in spring batch

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

like image 876
Manish Avatar asked Feb 14 '14 10:02

Manish


People also ask

How can I get JobParameters in Spring Batch processor?

@Configuration @EnableBatchProcessing public class BatchConfiguration { // read, write ,process and invoke job } JobParameters jobParameters = new JobParametersBuilder(). addString("fileName", "xxxx. txt"). toJobParameters(); stasrtjob = jobLauncher.

What is ItemWriter in Spring Batch?

ItemWriter. It is the element of the step of a batch process which writes data. An ItemWriter writes one item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this interface.

Is it possible to restart a completed job in Spring Batch?

By default, it does not allow completed jobs to be restarted. When a job is restarted, Spring Batch will create a new job execution for the particular job instance that failed, and it will restart at the failed step, executing from that point forward.


1 Answers

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.

like image 126
Arnaud Potier Avatar answered Sep 29 '22 18:09

Arnaud Potier