Microservice choreography with the Payara Micro clustered CDI Event Bus

Uncategorized

One of the key concepts of a micro-service architecture is the coordination of multiple services using loosely coupled event based choreography (http://www.thoughtworks.com/insights/blog/scaling-microservices-event-stream).

 

In a choreography based approach, micro-services generate events in response to significant changes and other services subscribe to these events to create a loosely coupled architecture. For example, a Customer microservice could generate a CustomerCreated event when a customer is created, and a Finance oriented microservice receive the event and create a customer account in the finance system.
With this in mind, the latest version of Payara Micro contains a light-weight CDI based event bus. Using the CDI events api  you can send CDI events from one microservice deployed to one Payara Micro instance to another microservice deployed to another instance.

 

Example Application

We have an example application in our GitHub examples respositoryThis example consists of two web applications, deployed to two separate Payara Micro nodes. One web application sends events, unsurprisingly named event-sender and the other receives events (event-receiver). When accessed, the event-sender web application sends the CustomMessage POJO to the event-receiver web application on the other Payara Micro node which collects it for display.

 

CDI-Events-Bus-1

There are instructions on how to run the example on GitHub.

 

Sending an Event

To send an event using Payara Micro you create a CDI event generator annotated with @Outbound. The @Outbound annotation indicates the event should be sent outbound from the micro service. CustomMessage is just a standard POJO from the example application. You can send any POJO from your application as long as it is Serializable.

 

@Inject
    @Outbound
    Event<CustomMessage> event;

To send the event you then just use the standard CDI api;

CustomMessage message = new CustomMessage ( 'test', 'server-1');
 event.fire(message);

 

Receiving an Event

To receive an Event, you just use the standard Observers api on your bean combined with the Payara Micro @Inbound annotation. The @Inbound annotation tells Payara Micro that you are interested in receiving events raised by other Payara Micro instances in the microservices fabric;

    public void observe(@Observes @Inbound CustomMessage event) {
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "MessageReceiverBean Received Event {0}", event);
    }

 

Payara Micro, Microservices & Choreography

Using these standard CDI apis and Payara Micro you can rapidly deploy multiple light-weight Java EE microservices and have them exchange POJO event messages to choreograph higher level business functions. 

To try this out on Payara Micro just download the latest release and follow the instructions from the example.

 

 

 

Comments (8)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

  1. Dave Macpherson

    Just curious, how asynchronous is this? Does the service thread that fires the event completely block until an observer thread on another instance finishes processing it?

    Also, if there are multiple “observer” instances running, do they all get it, or is it delivered to just one observer?

  2. Steve Millidge

    Internally we are using a Hazelcast distributed topic. The service thread on the sender doesn’t block while observers are processing the event. Within a single micro instance the receiving thread uses standard CDI apis to publish the CDI event to all observers. As CDI is not asynchronous the CDI publishing thread will be synchronous across all observers in the same JVM. Observers on all JVMs will receive the message.

  3. S Gerr

    Hello, Steve. Does it work with payara server full profile? Funny, I’ve implemented the same functionality the same way as the library for my app… 😉 Whether I’ve done the void work? 😉

    1. Stephen Millidge

      Hi, It doesn’t work on Payara Server full profile yet as we haven’t integrated it. It’s in the backlog though.

      1. S Gerr

        Hi Steve. Thanks for your reply. Are there any timetable for integration?

  4. Ondrej Mihályi

    It took me some time to figure out, so I’d like to point out that it is necessary to initialize the event bus, otherwise no messages will be delivered.

    It is necessary to call ClusteredCDIEventBus.initialize() to initialize the event bus. In Steve’s example app, it is done here:
    https://github.com/payara/Payara-Examples/blob/master/Payara-Micro/cdi-clustered-events/event-receiver/src/main/java/fish/payara/examples/payaramicro/event/receiver/EventsServlet.java#L50

  5. Gaurav Dhamija

    Is there a mechanism to know if all subscribers have finished processing the event? What if one of the subscriber was offline and comes back online later? How do we model a business process using choreography between micro-services.

  6. Julien Sié

    Hi folks. It works on 5.183… but you should add the eventdata jar in the classpath.

Related Posts

Community_Announcement 4 minutes
Uncategorized

Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified

We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]

Blue background with coral and fish. Left text: 'MONTHLY CATCH'. Right: laptop screen with tech tabs and Payara Community logo. 4 minutes
Community

The Payara Monthly Catch – August 2025

Welcome aboard the August 2025 issue of The Payara Monthly Catch! With summer in full swing, things may have felt […]

What Is a Java Application Server? A Short Guide 6 minutes
Jakarta EE

What Is a Java Application Server? A Short Guide

Enterprise Java applications power global commerce, healthcare, government and countless other industries. These systems must be scalable, secure and […]