Java 从控制器获取正在运行的作业实例状态

Java 从控制器获取正在运行的作业实例状态,java,spring,spring-mvc,batch-processing,spring-batch,Java,Spring,Spring Mvc,Batch Processing,Spring Batch,有没有办法在控制器中获取我以前启动过的正在运行的作业?这里有启动器的控制器和任务执行器配置: @Controller @RequestMapping(value = "/action/file") public class ArquivoController { @Autowired private JobLauncher jobLauncher; @Autowired @Qualifier(value = "jobValidateFile") pri

有没有办法在控制器中获取我以前启动过的正在运行的作业?这里有启动器的控制器和任务执行器配置:

@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>
@控制器
@请求映射(value=“/action/file”)
公共类ArquivoController{
@自动连线
私有JobLauncher JobLauncher;
@自动连线
@限定符(value=“jobValidateFile”)
私人工作文件;
@RequestMapping(value=“/validate”,method=RequestMethod.GET)
public@ResponseBody映射验证(@RequestParam Long fileId,主要用户){
映射参数=新的HashMap();
parameters.put(“fileId”,新作业参数(fileId));
JobExecution execution=this.jobLauncher.run(this.jobValidateFile,new JobParameters(parameters));
返回新的ResponseBuilder().toSuccessResponse();
}
@RequestMapping(value=“/status”,method=RequestMethod.GET)
public@ResponseBody映射seeStatus(@RequestParam Long fileId){
//在此处获取正在运行的作业状态。
返回新的ResponseBuilder().toSuccessResponse();
}
}
如您所见,我将启动器作为异步任务。我想做的是,一点一点地获取批处理状态。例如,我将制作一个javascript,它将每分钟调用“seeStatus”方法


谢谢。

您有几个选择:

  • 实现您自己的查询以加载所有正在运行的作业。JobRepository坚持使用一个非常简单的数据库模式,因此为此编写自己的DAO将非常简单
  • 如果您知道所关心的作业的所有名称,可以使用
    JobExplorer\findRunningJobExecutions(字符串jobName)
    循环查看它们。但是,只有在你有工作名称的情况下,这才有效
  • 我建议选择1


    您可以在此处阅读更多关于
    JobExplorer的信息:

    您也可以看看他们是如何在中完成的,尤其是在中。

    我们在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());