如何在Camunda中将ValidateRequest配置为独立Service Task并实现请求验证响应?
问题描述
现有需求如下:
- 调用
http://localhost:8080/order/{id}接口触发工作流启动; - 通过独立Java类
ValidateRequest完成请求验证; - 验证完成后返回“Hello World”或对应的错误信息。
目前已有可正常运行的工作流,希望将ValidateRequest设置为独立的Service Task,询问具体实现方法。
现有代码
控制器类 MyTestRestController
@RestController public class MyTestRestController { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private ValidateRequest validateRequest; @GetMapping("/order/{id}") public String test(@PathVariable int id) throws Exception { Map<String, Object> controlParameters = new HashMap<>(); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hello-world-process",controlParameters); if (id == 1) { controlParameters.put("errorFlag", true); completeTask(processInstance, controlParameters); throw new IllegalArgumentException(" Illegal Argument "); } controlParameters.put("errorFlag", false); completeTask(processInstance, controlParameters); return "Hello World"; } }
验证服务类 ValidateRequest
@Service public class ValidateRequest implements JavaDelegate{ @Override public void execute(DelegateExecution exec) throws Exception { // TODO Auto-generated method stub int id =1; System.out.println(" Starting ValidateRequest"); Thread.sleep(10000); if(id==1) { throw new IllegalArgumentException(" Illegal Argument 2 "); } } }
实现步骤
1. 修改工作流BPMN模型
在目标工作流中添加Service Task,并完成以下配置:
- 设置任务实现方式为
Java class,选择ValidateRequest类; - 添加输入映射:将接口传入的
id作为流程变量,映射到DelegateExecution中,确保验证类能获取到该参数; - 添加边界错误事件:为Service Task绑定边界错误事件,捕获
IllegalArgumentException,并指向错误结束节点(比如命名为errorEndEvent),标记流程因验证失败终止。
2. 调整控制器逻辑
移除控制器中手动验证和completeTask的硬编码逻辑,改为启动工作流时传入id参数,并根据流程执行结果返回对应信息:
@RestController public class MyTestRestController { @Autowired private RuntimeService runtimeService; @Autowired private HistoryService historyService; @GetMapping("/order/{id}") public String test(@PathVariable int id) throws Exception { // 传入id作为流程变量 Map<String, Object> variables = new HashMap<>(); variables.put("id", id); // 启动工作流 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hello-world-process", variables); // 同步等待流程执行完成(生产环境建议用异步回调或消息通知) while (historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).finished().count() == 0) { Thread.sleep(100); } // 根据流程结束节点判断结果 HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery() .processInstanceId(processInstance.getId()) .singleResult(); if ("errorEndEvent".equals(historicInstance.getEndActivityId())) { return "验证失败:参数非法"; } else { return "Hello World"; } } }
3. 完善验证类逻辑
修改ValidateRequest,从流程变量中获取真实的id参数并执行验证:
@Service public class ValidateRequest implements JavaDelegate{ @Override public void execute(DelegateExecution exec) throws Exception { // 从流程变量中获取接口传入的id Integer id = (Integer) exec.getVariable("id"); System.out.println("Starting ValidateRequest with id: " + id); // 模拟验证耗时 Thread.sleep(10000); // 执行真实验证逻辑 if(id != null && id == 1) { throw new IllegalArgumentException("Illegal Argument"); } } }
4. 配置错误处理流程
在BPMN模型中,确保边界错误事件正确关联IllegalArgumentException,并将错误流指向错误结束节点,这样流程在验证失败时会自动终止到该节点,方便控制器通过历史记录判断结果。
内容的提问来源于stack exchange,提问作者Hello
相关产品推荐
相关产品推荐

