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
@Configuration @EnableBatchProcessing public class BatchConfiguration { // read, write ,process and invoke job } JobParameters jobParameters = new JobParametersBuilder(). addString("fileName", "xxxx. txt"). toJobParameters(); stasrtjob = jobLauncher.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With