Valath
Valath

Reputation: 920

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

What is the best way to check the following condition: Given pid is pending on a particular intermediate message event.

Some thing that I have tried :

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
}

My bmp.xml code of intermediate message event:

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

My Java code :

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;

}

&& checking

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

Can anyone confirm is this the correct code/way to achieve my goal. Else please share the right approach.

Upvotes: 0

Views: 2290

Answers (1)

Alexander Anikin
Alexander Anikin

Reputation: 1098

In particular case for one specific process you can use .activitiId(), that is specifying id of bpmn element (in your case messageintermediatecatchevent1) instead of message name it's subscribed for. This technique is used for messageReceived element in activiti user guide. But it's not so great idea, since your process can possibly wait for this message in different intermediateMessageCatchingEvents and element can be renamed later.

Instead of .activitiId() you should use .messageEventSubscriptionName().

In more general case instead of .singleresult() you can use .list() and analyze its length. While 0 and 1 are obvious, more than one execution in your process waiting for message is a possibility that can be treated differently.

Also it's good idea to check business key or some process variable instead of specific process id.

P.S. Note that name/id for message in process can be different. AFAIK .messageEventSubscriptionName uses name of message, not id.

Upvotes: 1

Related Questions