Activiti:如何检查当前流程实例是否在给定的中间消息事件上挂起

Activiti : How to check if the current process instance is pending on a given intermediate message event

检查以下情况的最佳方法是什么: 给定的 pid 正在等待特定的中间消息事件。

我尝试过的一些东西:

Execution execution = runtimeService.createExecutionQuery().processInstanceId(<pId>)
                .activityId(<messageEventName>).singleResult();
if(null!=execution){
//I have got assurance that this pId is pending with this messageEventName currently
}

我的bmp.xml中间消息事件代码:

<intermediateCatchEvent id="messageintermediatecatchevent1" name="MessageCatchEvent">
    <messageEventDefinition messageRef="actionOnPendingLR"></messageEventDefinition>
  </intermediateCatchEvent>

我的Java代码:

private Execution checkIfProcessPendingOnPendingLRMessage(UserAction request) throws Exception {
    String pId = request.getProcessInstanceId();
    Execution execution = null;
    String messageName = "messageintermediatecatchevent1";
    try {
        execution = runtimeService.createExecutionQuery().processInstanceId(pId)
                .activityId(messageName).singleResult();
    } catch (Exception exe) {
        logger.error("supplied process instance is not matching on the state in which it is pending" + pId, exe);
    }

    if (execution == null) {
        throw new RuntimeException(
                "Couldn't find waiting process instance with id '" + pId + "' for message '" + messageName + "'");
    }
    return execution;

}

&& 检查

(null!=checkIfProcessPendingOnPendingLRMessage(request)) //in main method.

任何人都可以确认这是实现我的目标的正确 code/way 吗?否则请分享正确的方法。

在特定情况下,对于一个特定流程,您可以使用 .activitiId(),即指定 bpmn 元素的 ID(在您的情况下为 messageintermediatecatchevent1),而不是它订阅的消息名称。此技术用于 activiti 用户指南中的 messageReceived 元素。但这不是一个好主意,因为您的进程可能会在不同的 intermediateMessageCatchingEvents 中等待此消息,并且稍后可以重命名元素。

您应该使用 .messageEventSubscriptionName() 而不是 .activitiId()。

在更一般的情况下,您可以使用 .list() 而不是 .singleresult() 并分析其长度。虽然 0 和 1 是显而易见的,但在您等待消息的进程中有不止一次执行是可能的,可以区别对待。

此外,检查业务键或某些流程变量而不是特定流程 ID 也是个好主意。

P.S。请注意,处理中的消息的 name/id 可能不同。 AFAIK .messageEventSubscriptionName 使用消息名称,而不是 id。