Spring boot JobOperator.stop(executionId) 和 JobOperator.restart(executionId) 行为
Posted
技术标签:
【中文标题】Spring boot JobOperator.stop(executionId) 和 JobOperator.restart(executionId) 行为【英文标题】:Spring boot JobOperator.stop(executionId) and JobOperator.restart(executionId) behavior 【发布时间】:2018-05-10 08:57:01 【问题描述】:我正在尝试使用 Spring 批处理来处理批处理作业。
当我尝试使用executionId
stop
一个正在运行的作业时,该作业在SpringBatchDb.BATCH_JOB_EXECUTION
表中的以下条目中停止
'104','3','104','2017-11-27 11:39:10','2017-11-27 11:39:10','2017-11-27 11:39 :48','STOPPED','STOPPED','org.springframework.batch.core.JobInterruptedException','2017-11-27 11:39:48',NULL
请注意,STATUS
和 EXIT_CODE
已更新为 STOPPED
。但它会抛出一个异常,org.springframework.batch.core.launch.NoSuchJobException: No job configuration with the name [testJob] was registered
at org.springframework.batch.core.configuration.support.MapJobRegistry.getJob(MapJobRegistry.java:66)
at org.springframework.batch.core.launch.support.SimpleJobOperator.restart(SimpleJobOperator.java:275)
at org.springframework.batch.core.launch.support.SimpleJobOperator$$FastClassBySpringCGLIB$$44ee6049.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
at org.springframework.batch.core.launch.support.SimpleJobOperator$$EnhancerBySpringCGLIB$$318ff269.restart(<generated>)
at com.test.mypackage.batch.dao.BatchJobDaoImpl.restartJobExecution(BatchJobDaoImpl.java:62)
。
当我尝试使用 executionId 重新启动同一个作业时,它无法启动并给出相同的异常(如上所示)。
我的代码很简单,
@Autowired
private DataSource dataSource;
@Autowired
private JobOperator jobOperator;
@Override
public Long stopRunningExecution(Long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException
jobOperator.stop(executionId);
return executionId;
@Override
public Long restartJobExecution(long executionId) throws JobParametersInvalidException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, NoSuchJobException
return jobOperator.restart(executionId);
这里有什么问题?
【问题讨论】:
【参考方案1】:我遇到了类似的错误,但能够解决问题。
由于您尚未定义 JobRegistry,因此出现此问题。如果您定义 JobRegistry 并将其添加到 JobOperator Bean 中,则不会发生上述异常。
例如:
@Bean
public JobRegistry jobRegistry()
return new MapJobRegistry();
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor()
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry());
return jobRegistryBeanPostProcessor;
@Bean
public JobOperator jobOperator()
SimpleJobOperator jobOperator = new SimpleJobOperator();
jobOperator.setJobExplorer(getJobExplorer());
jobOperator.setJobRepository(getJobRepository());
jobOperator.setJobLauncher(getJobLauncher());
jobOperator.setJobRegistry(jobRegistry());
return jobOperator;
【讨论】:
以上是关于Spring boot JobOperator.stop(executionId) 和 JobOperator.restart(executionId) 行为的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?
《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》