Liferay Message Bus

  • Introduction
    • Liferay’s Message bus is a service level API for exchange messages inside the liferay.
    • It supports synchronous and asynchronous messaging and Cluster messaging support with Liferay ClusterLink.
    • You can leverage the Message Bus to send messages between and within your plugins. 
    • It’s conceptually similar to Java Messaging Service (JMS) Topics, but sacrifices transactional, reliable delivery capabilities, making it much lighter-weight.

 

  • Liferay uses Message Bus for :
    • Auditing
    • Search Engine Integration
    • Sending subscription emails
    • Monitoring
    • Document library processing
    • Background Tasks
    • Running Scheduler tasks
    • Running asynchronous background processes
    • Running cluster operation like cache replication

 

  • Liferay Message Bus Components
    • Destinations : Address or endpoints to which listeners register to receive messages
    • Senders : Invoke the Message Bus to send messages to Destination (Initiates the Message Bus)
    • Listeners : Consume messages received at destinations. They receive all messages sent to their registered destination.

Destinations :

  • A named endpoint for sending message to and receiving message from.
  • Provides loose coupling between senders and receiver.

Destination Types:

  • Parallel Destination
    • Messages sent here are queued
    • There’s one worker thread per message per message listener.
  • Serial Destination
    • Messages sent here are queued.
    • Worker threads from a thread pool deliver the messages to each registered message listener, one worker thread per message.
  • Synchronous Destination
    • Messages sent here are directly delivered to message listeners.
    • The thread sending the message here delivers the message to all message listeners also.

Note : Liferay has preconfigured destinations for various purposes. The DestinationNames class defines String constants for each of them. For example,DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR (value is "liferay/document_library_pdf_processor") is for deployment event messages. Since destinations are tuned for specific purposes, don’t modify them.

Example : Create Message Bus Listener For Documents and Media

                        DocumentsAndMediaMessageBusListner.class

Senders : Once you’ve  created a message, there are three ways to send it with the Message Bus:

  • Directly with MessageBus
  • Asynchronously with SingleDestinationMessageSender
  • Synchronously with SynchronousMessageSender

Follow this steps to send message directly:

1) Get a MessageBus reference.

      @Reference

      MessageBus _messageBus;

2) Create a message.

      Message message = new Message();

3) Call the MessageBus reference’s sendMessage method with the destination and message: 

      _messageBus.sendMessage(“destinationName”, message);

Direct Sending Example:

Asynchronous Sending Example:

Synchronous Sending : Blocks thread until receiving a response or the response times out.

Operation Modes :

  1. DEFAULT: Delivers the message in a separate thread and also provides timeouts, in case the message is not delivered properly.
  2. DIRECT: Delivers the message in the same thread of execution and blocks until it receives a response.

Synchronous Sending Example:

Listener Registration Methods :   Here are the ways to register your listener with Message Bus:

  1. Automatic Registration as a Component: Publish the listener to the OSGi registry as a Declarative Services Component that specifies a destination. Message Bus automatically wires the listener to the destination.
  2. Registering via MessageBus: Obtain a reference to the Message Bus and use it directly to register the listener to a destination.
  3. Registering directly to a Destination: Obtain a reference to a specific destination and use it directly to register the listener with that destination

 

Automatic Registration as a component

 That's IT! As per the above example, you can easily implement the message bus in Liferay.