Unable to restart a Spring batch job

与世无争的帅哥 提交于 2019-12-06 12:31:27

问题


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....!!?


回答1:


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.



来源:https://stackoverflow.com/questions/38745019/unable-to-restart-a-spring-batch-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!