Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microservice Choreography with the Payara Micro Clustered CDI Event Bus

DZone's Guide to

Microservice Choreography with the Payara Micro Clustered CDI Event Bus

Orchestrating and integrating microservices can be tough. Here's an example of how it's done using Payara's CDI based event bus.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

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 http://docs.jboss.org/cdi/api/1.1/overview-summary.html 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 respository:

https://github.com/payara/Payara-Examples/tree/master/Payara-Micro/cdi-clustered-events

This 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.

Image title

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 from http://www.payara.fish/downloads and follow the instructions from the example

https://github.com/payara/Payara-Examples/tree/master/Payara-Micro/cdi-clustered-events


Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ee ,microservices ,payara ,cdi ,glassfish ,java

Published at DZone with permission of Steve Millidge. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}