BPMN Event Handling (Java)

Goal

Model a process with message events and run it.

Short description for Java developers

  • Update the Tweet QA process to include a message start event by a superuser and a Tweet withdrawn message boundary event in the Camunda Modeler.
  • Write a new test class to get a MismatchingMessageCorrelationException with a single process instance.
  • Improve the test case to avoid the exception.
  • Add another test to start the process and send a boundary event

Detailed steps for Java developers

  1. Update the TwitterQA process as shown below. Add a message start event for a Tweet submitted by a superuser which does not need an approval. Add an message boundary event to Review tweet and connect it to an end event. This will allow a user (or anybody else) to withdraw or cancel a tweet submission.
  2. For the Tweet submitted by superuser start event add the Message Name superuserTweet. Message names can be added in the Details section of the General tab of the Properties Panel after you click on the plus (+) sign in Message. You can edit the Message Name which then will be added the drop down selection of messages.
  3. Repeat for Tweet withdrawn boundary event using a message name of tweetWithdrawn. Save your updates.
  4. Create a new test method. Be sure your @Deployment annotation uses Your name - Exercise 1.bpmn to point to the correct process model. Add the @Test annotation, too.
  5. In your new method start a process by creating a message and correlating it with an event. Be sure to add a content variable. Given that superuserTweet is correlated with a start event, it will simply start the process.

        ProcessInstance processInstance = runtimeService()
            .createMessageCorrelation("superuserTweet")
            .setVariable("content", "My Exercise 11 Tweet (ADD YOUR NAME HERE)- " + System.currentTimeMillis())
            .correlateWithResult()
            .getProcessInstance();
    
  6. Next, let’s use our handy process started assertion:

        assertThat(processInstance).isStarted();
    
  7. If you recall, we enabled an Asynchronous Before on the Publish tweet task so the process should have a job waiting there to be executed in the test. For this example we won’t be executing the job. We’ll be sending the Tweet withdrawn event to illustrate what happens if you send an event that the process is not expecting. Send the tweetWithdrawn event when the process should be waiting for the Asynchronous Before job at Publish tweet to be completed. You’ll notice that we need to have some sort of correlation to ensure the message is sent to the correct instance. In this case we’ll use the process instance ID for correlation.

        runtimeService()
            .createMessageCorrelation("tweetWithdrawn")
            .correlateWithResult();
    
  8. Run the JUnit test as is. What happened? Did you get a MismatchingMessageCorrelationException? If you did it was because the process instance was found using the process instance id for correlation but since the process was waiting at Tweet published it could not accept the tweetWithdrawn event.

  9. Remove the code correlating the message after you have seen the issue with the MismatchingMessageCorrelationException.

  10. Update your test method to query for and complete the job at Publish tweet and assert that the process ends.

        // get the job
        List<Job> jobList = jobQuery()
           .processInstanceId(processInstance.getId())
           .list();
    
        // execute the job
        assertThat(jobList).hasSize(1);
        Job job = jobList.get(0);
        execute(job);
    
        assertThat(processInstance).isEnded();
    
  11. Run your JUnit test again. Did it pass? Check your process test coverage output and check the Camunda Demo Twitter feed.

  12. Add another method to test the Tweet withdrawn message called testTweetWithdrawn. Be sure to start the process with a content variable, assert that it is started and is wating at Review tweet. Next, send a tweetWithdrawn message with the proper correlation using the tweet content.

      @Test
      @Deployment(resources="Your name - Exercise 1.bpmn")
      public void testTweetWithdrawn() {
    	Map<String, Object> varMap = new HashMap<>();
        varMap.put("content", "Test tweetWithdrawn message");
        ProcessInstance processInstance = runtimeService()
            .startProcessInstanceByKey("TwitterQAProcess", varMap);
        assertThat(processInstance).isStarted().isWaitingAt(findId("Review tweet"));
        runtimeService()
           .createMessageCorrelation("tweetWithdrawn")
           .processInstanceVariableEquals("content", "Test tweetWithdrawn message")
           .correlateWithResult();
        assertThat(processInstance).isEnded();
      }
    
  13. Run your JUnit test again. Did the tests pass? Do you see new tweets? Check your process test coverage. The TwitterQA.html summary should look something like this:

On this Page: