You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java API调用KIE Server执行含Rest任务的BPM流程报错如何解决

问题根因
  1. 调用RuleServicesClient执行批量命令启动流程失败:RuleServicesClient的默认执行上下文不会加载服务端全局配置的Rest Work Item Handler,导致流程运行到Rest Service Task时找不到对应的处理器抛出异常;同时你原代码中注释了newFireAllRules命令,就算流程能正常启动,规则也不会自动触发。
  2. 调用ProcessServicesClient启动流程规则不触发:你没有提前将规则需要匹配的RestFlowRequest事实对象插入到流程关联的KIE会话工作内存中,规则无匹配对象所以不会执行。
解决方案

方案1:调整RuleServicesClient调用逻辑(单请求完成所有操作)

操作步骤

  1. 进入你部署的KIE容器配置页面,在kie-deployment-descriptor.xml中补充注册Rest Work Item Handler,确保容器级会话可以加载到该处理器。
  2. 取消代码中newFireAllRules命令的注释,调整批量命令执行逻辑。

调整后代码示例

KieServices kieServices = KieServices.Factory.get();
CredentialsProvider credentialsProvider = new EnteredCredentialsProvider(USERNAME, PASSWORD);
KieServicesConfiguration kieServicesConfig = KieServicesFactory.newRestConfiguration(KIE_SERVER_URL, credentialsProvider);
kieServicesConfig.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfig);
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);

List<Command<?>> commands = new ArrayList<>();
KieCommands commandFactory = kieServices.getCommands();
// 插入规则需要的事实对象
commands.add(commandFactory.newInsert(new RestFlowRequest("Sample"), "SampleRequest"));
// 启动流程
commands.add(commandFactory.newStartProcess("RuleFlowSample.DecisionRestBPM"));
// 触发所有规则(必须保留,否则规则不会执行)
commands.add(commandFactory.newFireAllRules("numberOfFiredRules"));

BatchExecutionCommand batchExecutionCommand = commandFactory.newBatchExecution(commands, "你的Stateful会话ID");
ServiceResponse<ExecutionResults> response = rulesClient.executeCommandsWithResults(CONTAINER_ID, batchExecutionCommand);

注意:newBatchExecution的第二个参数为你流程绑定的stateful会话ID,若未自定义可填默认会话ID,不要使用stateless会话。

方案2:组合使用两类Client(兼容性更高)

不需要修改容器配置,拆分两步执行,既保证Rest任务正常运行,也能触发规则。

操作步骤

  1. 先通过RuleServicesClient将RestFlowRequest对象插入到会话工作内存,供后续流程的规则节点匹配。
  2. 再调用ProcessServicesClient启动流程,流程运行时会自动加载全局配置的Rest Work Item Handler,Rest任务可正常执行。

代码示例

KieServices kieServices = KieServices.Factory.get();
CredentialsProvider credentialsProvider = new EnteredCredentialsProvider(USERNAME, PASSWORD);
KieServicesConfiguration kieServicesConfig = KieServicesFactory.newRestConfiguration(KIE_SERVER_URL, credentialsProvider);
kieServicesConfig.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfig);

// 第一步:插入事实到会话工作内存
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
List<Command<?>> commands = new ArrayList<>();
KieCommands commandFactory = kieServices.getCommands();
commands.add(commandFactory.newInsert(new RestFlowRequest("Sample"), "SampleRequest"));
BatchExecutionCommand batchExecutionCommand = commandFactory.newBatchExecution(commands, "你的Stateful会话ID");
rulesClient.executeCommandsWithResults(CONTAINER_ID, batchExecutionCommand);

// 第二步:启动流程
ProcessServicesClient processService = kieServicesClient.getServicesClient(ProcessServicesClient.class);
processService.startProcess(CONTAINER_ID,"RuleFlowSample.DecisionRestBPM");
注意事项
  • 流程必须绑定stateful类型的KIE会话,stateless会话会在单次请求结束后清空工作内存,后续启动流程时会找不到已插入的事实。
  • 若规则需要在流程启动前先执行,可在第一步插入事实后补充newFireAllRules命令即可。

内容的提问来源于stack exchange,提问作者user11964028

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:15:03