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:
-
Create the registrator class as an OSGi component:
@Component( immediate = true, service = MyMessageListenerRegistrator.class ) public class MyMessageListenerRegistrator { ... }
-
Create a
MessageListener
variable:private MessageListener _clusterBridgeMessageListener;
-
Create a
Destination
reference and set itsdestination.name
property to your destination. For example, this reference is for the destinationliferay/live_users
:@Reference(target = "(destination.name=liferay/live_users)") private Destination _destination;
-
In the registrator’s
activate
method, create a newClusterBridgeMessageListener
and set it to theMessageListener
variable you created earlier. Then set theClusterBridgeMessageListener
’s priority and register theClusterBridgeMessageListener
to the destination:@Activate protected void activate() { _clusterBridgeMessageListener = new ClusterBridgeMessageListener(); _clusterBridgeMessageListener.setPriority(Priority.LEVEL5) _destination.register(_clusterBridgeMessageListener); }
The
Priority
enum has ten levels (Level1
throughLevel10
, withLevel10
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
, andThread.NORM_PRIORITY
. -
In the registrator’s
deactivate
method, unregister theClusterBridgeMessageListener
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;
}