是否有办法在Controller中获得我之前开始的正在运行的作业?这是启动器的控制器和任务执行器配置:
@Controller
@RequestMapping(value = "/action/file")
public class ArquivoController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier(value = "jobValidateFile")
private Job jobValidateFile;
@RequestMapping(value = "/validate", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> validate(@RequestParam Long fileId, Principal user) {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("fileId", new JobParameter(fileId));
JobExecution execution = this.jobLauncher.run(this.jobValidateFile, new JobParameters(parameters));
return new ResponseBuilder().toSuccessResponse();
}
@RequestMapping(value = "/status", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> seeStatus(@RequestParam Long fileId) {
// Get the running job status here.
return new ResponseBuilder().toSuccessResponse();
}
}
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
如您所见,我将启动器作为异步任务。我正在尝试做的是一点一点地获得批处理状态。例如,我要制作一个JavaScript,该JavaScript将每分钟调用一次“ seeStatus”方法。
谢谢。
您有几个选择:
JobExplorer#findRunningJobExecutions(String jobName)
遍历它们。但是,只有在您具有工作名称的情况下,此方法才有效。我推荐选项1。
您可以在此处了解有关JobExplorer
的更多信息:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/explore/JobExplorer.html
您也可以在Spring Batch Admin中查看他们的操作方式,特别是JobExecutionController。
我们在Spring Batch中具有以下状态
COMPLETED, STARTING, STARTED, STOPPING, STOPPED, FAILED, ABANDONED, UNKNOWN;
您可以使用:
import org.springframework.batch.core.JobExecution;
@Autowired
JobLauncher jobLauncher;
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Status : " + execution.getStatus());