从Controller获取正在运行的作业实例状态

问题描述 投票:2回答:3

是否有办法在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”方法。

谢谢。

java spring spring-mvc batch-processing spring-batch
3个回答
3
投票

您有几个选择:

  1. 执行您自己的查询以加载所有正在运行的作业。 JobRepository坚持一个非常简单的数据库架构,因此为此编写您自己的DAO非常简单。
  2. 如果您知道所关注作业的所有名称,则可以使用JobExplorer#findRunningJobExecutions(String jobName)遍历它们。但是,只有在您具有工作名称的情况下,此方法才有效。

我推荐选项1。

您可以在此处了解有关JobExplorer的更多信息:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/explore/JobExplorer.html


0
投票

您也可以在Spring Batch Admin中查看他们的操作方式,特别是JobExecutionController


0
投票

我们在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());
© www.soinside.com 2019 - 2024. All rights reserved.