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

时间:2014-03-27 11:49:02

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

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

@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。

感谢。

3 个答案:

答案 0 :(得分:2)

您有几个选择:

  1. 实施您自己的查询以加载所有正在运行的作业。 JobRepository持久化到一个非常简单的数据库模式,因此为此编写自己的DAO非常简单。
  2. 如果您知道所关注作业的所有名称,可以使用JobExplorer#findRunningJobExecutions(String jobName)循环播放它们。但是,如果您有作业名称,这只会起作用。
  3. 我建议选项1。

    您可以在此处详细了解JobExplorerhttp://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/explore/JobExplorer.html

答案 1 :(得分:0)

你也可以在Spring Batch Admin中看看他们是如何做到的,特别是JobExecutionController

答案 2 :(得分:0)

我们在Spring Batch中具有以下状态

import * as mod from './';

describe('test suites', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });
  it('#requestForSomething', () => {
    const action = { type: 'GET_DATA', productDetails: {} };
    const spy = jest.spyOn(mod, 'getData').mockReturnValueOnce(action);
    const dispatchSpy = jest.spyOn(mod.store, 'dispatch');
    mod.requestForSomething(1);
    expect(spy).toBeCalledWith(1);
    expect(dispatchSpy).toBeCalledWith(action);
  });

  it('#getData', () => {
    const spy = jest.spyOn(mod, 'someFunction').mockReturnValueOnce({});
    const actualValue = mod.getData(1);
    expect(actualValue).toEqual({ type: 'GET_DATA', productDetails: {} });
    expect(spy).toBeCalledWith(1);
  });

  it('#someFunction', () => {
    const actualValue = mod.someFunction(1);
    expect(actualValue).toBe(1);
  });
});

您可以使用:

 PASS  src/stackoverflow/58757332/index.spec.ts (7.351s)
  test suites
    ✓ #requestForSomething (5ms)
    ✓ #getData (1ms)
    ✓ #someFunction

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        8.659s