4 Replies Latest reply on Dec 28, 2011 11:04 AM by krishanps

    Message Event (throw and catch problem)

    krishanps
      Merry X-Mas, hope you all enjoyed also the holidays.

      I have short question, when I use message event to throw and catch it doesn't work, I only get as a output "Sending message: MyValue". If I instead uses signal events in the way that I replace both times the line EventMessage.bpmn2 it works properly.

      <messageEventDefinition messageRef="_8_Message"/> with the line <signalEventDefinition signalRef="_8_Message"/>

      Can you please tell me what could be the problem and how I can fix it?
      Thank you very much

      My *.bpmn file:
      EventMessage.bpmn2
          <!-- Throw a message event -->
          <intermediateThrowEvent id="_7" name="Script" >
            <dataInput id="_7_Input" />
            <dataInputAssociation>
                  <sourceRef>x</sourceRef>
                  <targetRef>_7_Input</targetRef>
            </dataInputAssociation>
            <inputSet>
                  <dataInputRefs>_7_Input</dataInputRefs>
            </inputSet>
            <messageEventDefinition messageRef="_8_Message"/>
          </intermediateThrowEvent>
        
          <!-- Catch a message event -->
          <intermediateCatchEvent id="_4" name="event" >
            <dataOutput id="_4_Output" name="event" />
            <dataOutputAssociation>
                        <sourceRef>_4_Output</sourceRef>
                        <targetRef>x</targetRef>
            </dataOutputAssociation>
            <outputSet>
                         <dataOutputRefs>_4_Output</dataOutputRefs>
            </outputSet>
            <messageEventDefinition messageRef="_8_Message"/>
          </intermediateCatchEvent>

      My *.java file:

       

      *.java
            public static final void main(String[] args) {
                  try {
                        // load up the knowledge base
                        KnowledgeBase kbase = readKnowledgeBase();
                        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
                        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
                        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());
                        ksession.getWorkItemManager().registerWorkItemHandler("Send Task", new SendTaskHandler());
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("x", "MyValue");
                        params.put("y", "OtherValue");            
                        // start a new process instance
                        ksession.startProcess("IntermediateCatchEvent", params);

                       
                        logger.close();
                  } catch (Throwable t) {
                        t.printStackTrace();
                  }
            }

            private static KnowledgeBase readKnowledgeBase() throws Exception {
                  KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
                  kbuilder.add(ResourceFactory.newClassPathResource("EventMessage.bpmn2"), ResourceType.BPMN2);
                  return kbuilder.newKnowledgeBase();
            }

       

        • 1. Re: Message Event (throw and catch problem)
          swiderski.maciej

          This is because you use default SendTaskHandler that is more like a dummy work item handler that just prints out the messages instead of sending it. You should implement your own handler to send a message.

           

          in other hands, what are you trying to do with message exchange? Message events/flows are more for exchanging information between processes rather than within a process. So, if that is your case, signals are more suitable.

           

          HTH

          1 of 1 people found this helpful
          • 2. Re: Message Event (throw and catch problem)
            krishanps

            Thank you for your hint Maciej.

            You are right I didn't implemented my own MessageEventSendTaskHelper.java. Now I have more options to trigger the message catch event.

             

            The model above was a just simple example to check if message events work but it doesn't before.

             

            With the Eclipse Bpmn 2.0 - plugin I have modelled 2 pools which I want to connect with the help of message events.

            My intention: If the first process is at the end node it can throw a message event so the other can continue his steps after it catched the message event.

            • 3. Re: Message Event (throw and catch problem)
              swiderski.maciej

              Goodie, completely valid case. Would be great if you could share your experience with that on the forum so others can benefit from it.

               

              Cheers

              • 4. Re: Message Event (throw and catch problem)
                krishanps

                Yes, of course. So I just give a reference to ksession from the main-method to the implemented MessageEventSendTaskHandler.java. So I can use it to call a signalEvent to initiate the message catch event.

                 

                My implementation changes:

                 

                 

                *.java

                public static final void main(String[] args) {

                        try {

                 

                            // load up the knowledge base

                            KnowledgeBase kbase = readKnowledgeBase();

                            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

                           

                            //creates an own SendTaskHandler to work on the same ksession to initiate a message event catch call

                            MessageSendTaskHandler msgSendTaskHandler = new MessageEventSendTaskHandler();

                            msgSendTaskHandler.setStatefulKnowledgeSession(ksession);

                           

                            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");

                            ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());

                            ksession.getWorkItemManager().registerWorkItemHandler("Send Task", msgSendTaskHandler);                       

                            Map<String, Object> params = new HashMap<String, Object>();

                            params.put("x", "MyValue");

                            params.put("y", "OtherValue");

                            // start a new process instance

                            ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent", params);                       

                            logger.close();

                        } catch (Throwable t) {

                            t.printStackTrace();

                        }

                    }

                 

                    private static KnowledgeBase readKnowledgeBase() throws Exception {

                        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                        kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-IntermediateCatchEventMessage.bpmn2"), ResourceType.BPMN2);

                        return kbuilder.newKnowledgeBase();

                    }

                }

                 

                 

                 

                The Implemented SendTaskHandler.java

                MessageEventSendTaskHandler.java

                public class  MessageEventSendTaskHandler implements WorkItemHandler {

                   

                    private StatefulKnowledgeSession ksession;

                    private long processInstanceID;

                   

                 

                    public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

                        String message = (String) workItem.getParameter("Message");

                        System.out.println("Die gesendete Nachricht: " + message);               

                        //starts Message Catch Event

                        getStatefulKnowledgeSession().signalEvent("Message-_8_Message", "hi",   getStatefulKnowledgeSession().getProcessInstance(1).getId());

                        manager.completeWorkItem(workItem.getId(), null);

                     

                    }

                   

                    public void setStatefulKnowledgeSession(StatefulKnowledgeSession ksession){

                        this.ksession = ksession;

                    }

                 

                    public StatefulKnowledgeSession getStatefulKnowledgeSession(){

                        return this.ksession;

                    }

                }

                 

                If you have questions feel free to post your questions :-)