How

As I already mentioned, IMO one of the great features of the JavaEE/Jakarta EE programming model is the CDI event mechanism. This model is one of the cleanest ways to decouple your applications.

However, this mechanism was designed to work in a single instance mode and doesn’t fit well for clustered environments use case. Basically, there is no way to observes an event fired in another machine in the cluster, requiring to use some other event technology and event manual translation.

The main goal of this invention is to extend the CDI event mechanism to a clustered environment, making easy and almost transparent for the users firing an event on one node and observes it on another node. But how?

Metaprogramming to the rescue

First, let’s create a new annotation called Clustered and let’s add it for the events that we want to propagate on the cluster.

Now, we can add it for an event:

Our users fires this event has a regular CDI event.

Behind the scenes, what our invention does:

Observe all CDI events and check if a CDI event object type has @Clustered annotation [check the code]:

2. If this event has @Clustered annotation, we serialised it and sent a serialised cluster message with all event data. (on Business Central we use AMQ/Artemis for this) [check the code]:

3. In each other node, we receive this event, deserialise it and fire as a regular CDI event (reproducing it): [check the code]

So basically, what my invention do is to observe all CDI events and if a CDI event object type has @Clustered annotation, we sent a serialised cluster message with all event data, deserialise on other nodes and regenerate a new CDI event.

CDI Events Distribution in a Cluster Environment via Metaprogramming

Using that invention doesn’t matter if the event was fired on the local instance or fired in other nodes because all CDI observables will receive the same event data, making the same CDI event programming model that works on a single instance works almost transparently on cloud environments.