Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to restart a Spring batch job

I have a spring batch job which reads, transforms and writes to an Oracle database. I am running the job via the CommandLineJobRunner utility (using a fat jar + dependencies generated with the maven shade plugin); the job subsequently fails halfway through due to "java heap memory limit reached" and the job is not marked as FAILED but rather still shows status STARTED.

I tried to re-run the job using the same job parameters (as the docs suggest) but this gives me this error:

5:24:34.147 [main] ERROR o.s.b.c.l.s.CommandLineJobRunner - Job Terminated in error: A job execution for this job is already running: JobInstance: id=1, version=0, Job=[maskTableJob]

org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=1, version=0, Job=[maskTableJob] at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:120) ~[maskng-batch-1.0-SNAPSHOT-executable.jar:1.0-SNAPSH

I have tried all sorts of things (like manually setting the status to FAILED, using the -restart argument) but to no avail. Is there something I am missing here as I thought one of the strong points of spring batch is its ability to restart jobs where they left off....!!?

like image 973
Christopher Richard Dobbs Avatar asked Aug 03 '16 13:08

Christopher Richard Dobbs


People also ask

How do I terminate a Spring Batch job?

The method is stop(long executionId) You would have to use JobExplorer to find the correct executionId to stop. Also from within a job flow config you can configure a job to stop after a steps execution based on exit status (see https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#stopElement).

How do I run a Spring Batch job continuously?

If you want to launch your jobs periodically, you can combine Spring Scheduler and Spring Batch. Here is a concrete example : Spring Scheduler + Batch Example. If you want to re-launch your job continually (Are you sure !), You can configure a Job Listener on your job. Then, through the method jobListener.


1 Answers

First thing that you should know is Joblauncher cannot be used to restart the job which has already run . The reason why you are getting "JobExecutionAlreadyRunningException" is because the parameter that you are passing is already present in the DB and hence you are getting this exception .

In spring batch , job can be restarted if it has completed with "FAILED" status or "STOPPED" status.

JobOperator has restart method which can be used to restart a failed job by passing the jobexecution id which was completed with "FAILED" status or "STOPPED" status.

Please note that a job cannot be restarted if it has completed with "FINISHED" status . In this case you will have to submit new job with new job parameters

If you want to manually set the status of job as failed then run the below query and restart the job using JobOperator.restart() method.

update batch_job_execution set status="FAILED", version=version+1 where job_instance_id=jobId;

Improper handling of transaction management could be one possible reason why your job status is not getting updated with the "FAILED" status . Please make sure you are transaction is getting completed even if the job has encountered run time exception.

like image 198
DevG Avatar answered Sep 24 '22 06:09

DevG