【问题标题】:Get running job instance status from Controller从 Controller 获取正在运行的作业实例状态
【发布时间】:2014-03-27 11:49:02
【问题描述】:

有没有办法让我之前在 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>

如您所见,我将启动器作为异步任务。我正在尝试做的是一点一点地获取批次状态。例如,我要制作一个每分钟调用“seeStatus”方法的javascript。

谢谢。

【问题讨论】:

    标签: java spring spring-mvc batch-processing spring-batch


    【解决方案1】:

    你有几个选择:

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

    我推荐选项 1。

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

    【讨论】:

      【解决方案2】:

      您也可以在Spring Batch Admin 中查看他们是如何做到的,尤其是JobExecutionController

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多