2

如果我使用计时器启动事件来安排进程的启动时间,我如何查询哪些进程将启动以及何时启动?

另外,对于捕获计时器中间事件,我也有同样的问题。camunda 引擎是否提供 API(Java 或 REST)来查询哪些计时器正在运行以及它们预计何时到期?

4

1 回答 1

4

您可以通过 Java 和 REST API 检索使用计时器事件的进程的下一个计划执行时间,如下所示:

示例 Java API:

import org.camunda.bpm.ProcessEngineService;
import org.camunda.bpm.container.RuntimeContainerDelegate;
import org.camunda.bpm.engine.ManagementService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.management.JobDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.Job;

import java.util.HashMap;
import java.util.List;

public class StackOverflow {

  public HashMap<ProcessDefinition, List<Job>> queryNextScheduledExecutionOfTimers() {
    ProcessEngineService processEngineService = 
    RuntimeContainerDelegate.INSTANCE.get().getProcessEngineService();
    ProcessEngine defaultProcessEngine = processEngineService.getDefaultProcessEngine();

    // optional step - get all active process definitions
    RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
    List<ProcessDefinition> processDefinitions =
        repositoryService.createProcessDefinitionQuery().active().list();

    ManagementService managementService = defaultProcessEngine.getManagementService();

    HashMap<ProcessDefinition,List<Job>> timerJobsByProcessDefinition = new HashMap<ProcessDefinition, List<Job>>();
    for (ProcessDefinition processDefinition : processDefinitions) {
      List<JobDefinition> jobDefinitions =
          managementService.createJobDefinitionQuery()
              .active()
              .processDefinitionId(processDefinition.getId())
              .list();

      for (JobDefinition jobDefinition : jobDefinitions) {
        // if you want to lookup the activity to highlight it inside the process diagram for example
        String activityId = jobDefinition.getActivityId();
        // if you want to display the configured expression / date / cron expression when the timer should fire
        String jobConfiguration = jobDefinition.getJobConfiguration();
        // if you want to distinguish between timer start event / catching timer intermediate event / boundary timer event
        String timerType = jobDefinition.getJobType();

        List<Job> jobs = managementService.createJobQuery()
            .active()
            .timers()
            .jobDefinitionId(jobDefinition.getId())
            .orderByJobDuedate()
            .list();

        timerJobsByProcessDefinition.put(processDefinition, jobs);
      }
    }

    return timerJobsByProcessDefinition;
  }
}

示例 REST API:

用于http://localhost:8080/engine-rest/process-definition/检索流程定义

对于每个流程定义,通过查询作业定义 http://localhost:8080/engine-rest/job-definition?active=true&processDefinitionId=${YOUR_PROCESS_DEFINITION_ID}

对于每个流程定义,通过以下方式查询作业http://localhost:8080/engine-rest/job?active=true&timers=true&processDefinitionId=${YOUR_PROCESS_DEFINITION_ID}

那么您必须使用 jobConfigurationId 将生成的作业与作业配置相关联。

于 2014-04-08T11:36:32.120 回答