Giter Club home page Giter Club logo

camunda-example-eventsubscriptions's Introduction

Querying Event Subscriptions or Hardening Message Correlation

Introduction

TL;DR

This process application contains unit tests that demonstrate how to check if a process instance is "ready-to-receive" a message before actually cerrelate it. Just have a look into InMemoryH2Test.

And run mvn clean package to see all tests suceeding.

Why Should I Check For An Individual Receiver Before Correlation?

In general, there is no strict requirement to check whether a message correlation attempt is going to succeed or to fail. However, the BPMN standard requires a message to be delivered to exactly 1 (ONE) receiver. In case no one is waiting for the message it just will pop out.

The Camunda Engine implements messaging through a subscription based approach. As soon as the token of a process instance arrives at a receive event or receive task a subscription for the message is created. As an effect of that, multiple process instances (receivers) can wait for the same message at the same time. To prevent this unstandardized behaviour RuntimeService.correlateMessage() throws a MismatchingMessageCorrelationException if not exactly one process instance is ready-to-receive. More precisely, this exception is thrown if no one is waiting or if more than one is waiting for a certain message.

The MismatchingMessageCorrelationException is a RuntimeException. Depending on your environment this might set a running transaction to rollback, which might cause trouble. Hence sometime you want to check beforehand if a process instance is ready-to-receive a message.

A Deeper Look

The easiest possibility is to query for existing subscriptions:

SubscriptionQuery subscriptionQuery = runtimeService.createEventSubscriptionQuery()
  .eventName("my_message")
  .eventType("message");
List<EventSubscription> eventSubscriptions = subscriptionQuery.list();

The listing above shows how a query on event subscriptions is used. The example code queries for subscriptions that match the following requirements:

  1. Event Type is message
  2. Message Name is my_message

If the List eventSubscriptions contains exactly one element only one subscription to the message my_message was found and the correlation is going to work.

if (eventSubscriptions.size() == 1) {
  runtimeService.correlateMessage("my_message");
} else {
  log.warn("correlation not possible at the moment");
}

Often you want to identity the waiting process instance by process variables containing business data or correlation key. If you want to do so you have to use a different query:

runtimeService.createExecutionQuery() 
        .messageEventSubscription()
        .processVariableValueEquals("correlationId", correlationId)
        .list();

camunda-example-eventsubscriptions's People

Contributors

berndruecker avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.