How to stop batch job from the main method ? I am running the batch job from my method and now its giving me the below error. Any help on how to kill and stop it?

A job execution for this job is already running: JobInstance: id=18380, version=0, Job=[myFileGenerationJob] org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=18380, version=0, Job=[myFileGenerationJob] Done at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:120) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:172) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) at com.sun.proxy.$Proxy3.createJobExecution(Unknown Source) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:125) at com.mastercard.ess.eds.CustomerFileGenerationApp.main(CustomerFileGenerationApp.java:31) 

I am using the following code -

public class CustomerFileGenerationApp { public static void main(String[] args) { PropertyConfigurator.configure("src/main/resources/log4j.properties"); String[] springConfig = { "../META-INF/spring/customer-file-generation-job.xml"}; ApplicationContext context = new FileSystemXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("customerFileGenerationJob"); JobParameters jobParameters = new JobParametersBuilder().addString("runMode", "ABCD").toJobParameters(); try { JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("Done"); } } 
7

1 Answer

Unfortunatly I have no idea to deal with that Exception by using java code. But, You can manage that exception by updating related batch meta table datas by using some queries.

There are two tables you should check, batch_step_execution, batch_job_execution. (I assume that you create them already because the Spring Batch provides them to almost all kind of database.)

If those tables has a started(starting) and not completed job, You can encounter the exception above when you restart server.

So first, you can check whether running jobs exist by executing queries below.

SELECT STEP_EXECUTION_ID , JOB_EXECUTION_ID , STEP_NAME , STATUS , EXIT_CODE , bse.* FROM batch_step_execution bse WHERE 1=1 AND status IN ('STARTED', 'STARTING') AND exit_code NOT IN ('COMPLETED', 'FAILED') ORDER BY start_time desc ; SELECT bje.JOB_EXECUTION_ID , bji.JOB_NAME , bje.JOB_INSTANCE_ID , bje.STATUS , bje.EXIT_CODE , timediff(bje.CREATE_TIME, NOW()) , bje.* FROM batch_job_execution bje INNER JOIN batch_job_instance bji ON bje.job_instance_id = bji.job_instance_id WHERE 1=1 AND status IN ('STARTED', 'STARTING') AND exit_code NOT IN ('COMPLETED', 'FAILED') ORDER BY bje.create_time desc ; 

Second, if there are rows above, You can update those jobs to COMPLETED status by executing queries below.

UPDATE batch_step_execution SET END_TIME = now() ,LAST_UPDATED = now() ,STATUS = 'COMPLETED' ,EXIT_CODE = 'FAILED' WHERE STEP_EXECUTION_ID in ( select STEP_EXECUTION_ID from batch_step_execution bse where 1=1 and status not in ('COMPLETED', 'FAILED') or EXIT_CODE not in ('COMPLETED', 'FAILED') ) ; UPDATE BATCH_JOB_EXECUTION SET END_TIME = now() ,LAST_UPDATED = now() ,STATUS = 'COMPLETED' ,EXIT_CODE = 'FAILED' WHERE JOB_EXECUTION_ID in ( select bje.JOB_EXECUTION_ID from batch_job_execution bje where 1=1 and (status not in ('COMPLETED', 'FAILED')) or (EXIT_CODE not in ('COMPLETED', 'FAILED')) ) ; 

Third, please restart the server. In my case, it works when I encounter the JobExecutionAlreadyRunningException.

Thanks.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.