Event subprocess with message start event not working

I want to run event sub process every time on arrival of new message from queue.
I have created sample process as below, where my main process just prints simple text(In actual scenario it will connect to broker and will subscribe to topic)
Second is event subprocess which should be triggered whenever start event is triggered with runtimeService.messageEventReceived(“newMessage”, execution.getId());

process is as below

<?xml version="1.0" encoding="UTF-8"?>

< message id=“messageId” name=“newMessage”>
< process id=“testqueue” name=“testqueue” isExecutable=“true”>

<startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
<scriptTask id="sid-1B51A9DB-F05F-48C4-84F6-A8AA0EAD0978" scriptFormat="javascript" flowable:autoStoreVariables="false">
  <script><![CDATA[java.lang.System.out.println(">>>>>Inside First script task>>>>>>>");]]></script>
</scriptTask>
<endEvent id="sid-E071EF29-9B9A-47A7-B8D6-27A6EF7570FC"></endEvent>
<sequenceFlow id="sid-E7410E2E-C9F4-414F-874C-DD45E6A89627" sourceRef="sid-1B51A9DB-F05F-48C4-84F6-A8AA0EAD0978" targetRef="sid-E071EF29-9B9A-47A7-B8D6-27A6EF7570FC"></sequenceFlow>
<sequenceFlow id="sid-5C644FCA-EBCA-4881-9568-404D8C47BBA4" sourceRef="startEvent1" targetRef="sid-1B51A9DB-F05F-48C4-84F6-A8AA0EAD0978"></sequenceFlow>
<subProcess id="sid-F57ABAA8-8857-4E50-B4EB-81A1DF5B5398" name="subProcess" triggeredByEvent="true">
  <startEvent id="sid-CAF6BD3B-827D-47F8-88BA-A001DF47BF35" isInterrupting="false">
    <messageEventDefinition messageRef="messageId"></messageEventDefinition>
  </startEvent>
  <scriptTask id="sid-4FB47A3D-1529-474C-BA23-6A411EB5AE0E" scriptFormat="javascript" flowable:autoStoreVariables="false">
    <script><![CDATA[java.lang.System.out.println(">>>>>Inside Second script task>>>>>>>");]]></script>
  </scriptTask>
  <endEvent id="sid-A54BE62A-4C24-4BB5-86D0-07BB8EDF8CF5"></endEvent>
  <sequenceFlow id="sid-B21FF271-ED8E-414F-91FA-AF9F058B126E" sourceRef="sid-4FB47A3D-1529-474C-BA23-6A411EB5AE0E" targetRef="sid-A54BE62A-4C24-4BB5-86D0-07BB8EDF8CF5"></sequenceFlow>
  <sequenceFlow id="sid-4CB2E0D9-9890-4CF5-8282-A311F314A321" sourceRef="sid-CAF6BD3B-827D-47F8-88BA-A001DF47BF35" targetRef="sid-4FB47A3D-1529-474C-BA23-6A411EB5AE0E"></sequenceFlow>
</subProcess>

< /process>
< /definitions>

I want to run it using below code

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(“testqueue”);

Execution execution = runtimeService.createExecutionQuery()
.processInstanceId(processInstance.getId())
.messageEventSubscriptionName(“newMessage”)
.singleResult();

runtimeService.messageEventReceived(“newMessage”, execution.getId());

But i am getting execution as null and due to that i get exception that there is no subscription to message “newMessage”.

Please help me to identify what wrong i am doing here.

Hey @ajayfarad,

You are trying to send a message to a completed process instance. Your current process is straight through, i.e. it starts and completes immediately.

Add a wait state and it would properly.

Cheers,
Filip

1 Like