Message Bus Event Listeners

When using Message Bus, you may wish to listen for events that take place within the Message Bus framework itself, independent of messages. For example, you can listen for when destinations and message listeners are added or removed. Here, you’ll learn how.

Listening for Destinations

Message Bus notifies event listeners when destinations are added and removed. To register these listeners, publish a MessageBusEventListener instance to the OSGi service registry (e.g., via an @Component annotation).

Here’s an example implementation of MessageBusEventListener. Use the destinationAdded and destinationDestroyed methods to implement any logic that you want to run when a destination is added or removed, respectively:

@Component(
    immediate = true,
    service = MessageBusEventListener.class
)
public class MyMessageBusEventListener implements MessageBusEventListener {

    void destinationAdded(Destination destination) {
        ...
    }

    void destinationDestroyed(Destination destination) {
        ...
    }
}

Listening for Message Listeners

Message Bus notifies DestinationEventListener instances when message listeners for destinations are either registered or unregistered. To register an event listener to a destination, publish a DestinationEventListener service to the OSGi service registry, making sure to specify the destination’s destination.name property.

@Component(
    immediate = true,
    property = {"destination.name=myCustom/Destination"},
    service = DestinationEventListener.class
)
public class MyDestinationEventListener implements DestinationEventListener {

    void messageListenerRegistered(String destinationName,
                                   MessageListener messageListener) {
        ...
    }

    void messageListenerUnregistered(String destinationName,
                                   MessageListener messageListener) {
        ...
    }
}

Using the Message Bus

« Creating a DestinationRegistering Message Listeners »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0