Job Execution Listener is failed to store value in Job Execution Context Table

Multi tool use


Job Execution Listener is failed to store value in Job Execution Context Table
I have implemented the function to store number of processed with failure and success into job execution context in afterJob() method of JobExecutionListener implemented:
public void afterJob(JobExecution jobExecution) {
final long jobExecutionId = jobExecution.getId();
final BatchStatus jobStatus = jobExecution.getStatus();
final ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();
String exitCodeAndMessage = null;
Map<String, Integer> recordsProcessed = null;
switch (jobStatus) {
case COMPLETED:
//exitCodeAndMessage = getExitCodeAndMessageFromEveryStep(jobExecution);
recordsProcessed = getExitCodeAndMessageFromEveryStep(jobExecution);
if (exitCodeAndMessage == null) {
exitCodeAndMessage = "COMPLETED";
}
jobExecutionContext.putString("AfterJob", "Success");
jobExecutionContext.put("recordsProcessed", recordsProcessed);
break;
After running the job, there is no value stored inside the table. Why?
How do I need to add code in order to write information to the DB?
– user3865583
yesterday
1 Answer
1
The job execution context is not saved after the job is finished but is saved in between every step execution. So you can use a StepExecutionListener
and add these metrics in the afterStep
method. More details on this here: https://docs.spring.io/spring-batch/4.0.x/reference/html/domain.html#executioncontext
StepExecutionListener
afterStep
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
None of the code you wrote writes information to the DB, and the JobExecution has already completed so Spring is not going to persist any changes you make to the context at this point.
– Joe Chiavaroli
yesterday