Sending Messages Across a Cluster

To ensure a message sent to a destination is received by all cluster nodes, you must register a ClusterBridgeMessageListener at that destination. This bridges the local destination to the cluster and ensures that messages sent to the destination are distributed across the cluster’s JVMs. You should do this in a registrator class, like those shown in Registering Message Listeners.

Follow these steps to create a registrator class that registers a ClusterBridgeMessageListener to a destination:

  1. Create the registrator class as an OSGi component:

    @Component(
        immediate = true,
        service = MyMessageListenerRegistrator.class
    )
    public class MyMessageListenerRegistrator {
        ...
    }
    
  2. Create a MessageListener variable:

    private MessageListener _clusterBridgeMessageListener;
    
  3. Create a Destination reference and set its destination.name property to your destination. For example, this reference is for the destination liferay/live_users:

    @Reference(target = "(destination.name=liferay/live_users)")
    private Destination _destination;
    
  4. In the registrator’s activate method, create a new ClusterBridgeMessageListener and set it to the MessageListener variable you created earlier. Then set the ClusterBridgeMessageListener’s priority and register the ClusterBridgeMessageListener to the destination:

    @Activate
    protected void activate() {
    
        _clusterBridgeMessageListener = new ClusterBridgeMessageListener();
        _clusterBridgeMessageListener.setPriority(Priority.LEVEL5)
        _destination.register(_clusterBridgeMessageListener);
    }
    

    The Priority enum has ten levels (Level1 through Level10, with Level10 being the most important). Each level is a priority queue for sending messages through the cluster. This is similar in concept to thread priorities: Thread.MIN_PRIORITY, Thread.MAX_PRIORITY, and Thread.NORM_PRIORITY.

  5. In the registrator’s deactivate method, unregister the ClusterBridgeMessageListener from the destination:

    @Deactivate
    protected void deactivate() {
    
        _destination.unregister(_clusterBridgeMessageListener);
    }
    

Here’s the full registrator class for this example:

@Component(
    immediate = true,
    service = MyMessageListenerRegistrator.class
)
public class MyMessageListenerRegistrator {
    ...

    @Activate
    protected void activate() {

        _clusterBridgeMessageListener = new ClusterBridgeMessageListener();
        _clusterBridgeMessageListener.setPriority(Priority.LEVEL5)
        _destination.register(_clusterBridgeMessageListener);
    }

    @Deactivate
    protected void deactivate() {

        _destination.unregister(_clusterBridgeMessageListener);
    }

    @Reference(target = "(destination.name=liferay/live_users)")
    private Destination _destination;

    private MessageListener _clusterBridgeMessageListener;
}

Registering Message Listeners

Sending a Message

Using the Message Bus

« Sending a MessageIntroduction to Cache Configuration »
Was this article helpful?
0 out of 0 found this helpful