activiti 定时执行任务TimerCatchEvent 调用外部系统接口
可以使用activiti的JavaDelegate接口,在execute方法中调用外部系统接口。
具体步骤如下:
-
创建一个Java类,实现org.activiti.engine.delegate.JavaDelegate接口。
-
在execute方法中调用外部系统接口,如下所示:
public void execute(DelegateExecution execution) throws Exception {
// 获取流程变量
String orderId = (String) execution.getVariable("orderId");
// 调用外部系统接口
String result = callExternalSystem(orderId);
// 设置流程变量
execution.setVariable("result", result);
}
- 在流程定义中配置定时执行任务节点,并指定JavaDelegate实现类,如下所示:
<process id="myProcess" name="My process">
<startEvent id="startEvent" />
<sequenceFlow id="flow1" sourceRef="startEvent" targetRef="timerCatchEvent" />
<timerCatchEvent id="timerCatchEvent">
<timerEventDefinition>
<timeCycle>R/PT5M</timeCycle>
</timerEventDefinition>
<extensionElements>
<activiti:field name="delegateExpression">
<activiti:string>#{myJavaDelegate}</activiti:string>
</activiti:field>
</extensionElements>
</timerCatchEvent>
<sequenceFlow id="flow2" sourceRef="timerCatchEvent" targetRef="endEvent" />
<endEvent id="endEvent" />
</process>
其中,delegateExpression指定了JavaDelegate实现类的名称,如下所示:
<activiti:string>#{myJavaDelegate}</activiti:string>
- 在流程启动前,将JavaDelegate实现类注册到流程引擎中,如下所示:
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
// 注册JavaDelegate实现类
Map<String, JavaDelegate> beans = new HashMap<>();
beans.put("myJavaDelegate", new MyJavaDelegate());
cfg.setBeans(beans);
// 创建流程引擎
ProcessEngine processEngine = cfg.buildProcessEngine();
// 部署流程定义
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("myProcess.bpmn20.xml")
.deploy();
// 启动流程实例
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
- 当定时执行任务节点触发时,流程引擎会自动调用JavaDelegate实现类的execute方法,在该方法中调用外部系统接口,并设置流程变量
原文地址: http://www.cveoy.top/t/topic/fsFg 著作权归作者所有。请勿转载和采集!