Listening for Messages

Listening for Messages

You can listen for messages sent to any registered Message Bus destination, whether it’s built-in to DXP/Portal, defined by third-parties, or created by you. Messages sent to the same destination typically have something in common, such as a similar event type or topic. Here you’ll deploy a class that listens for messages received at a destination called DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR. Documents and Media sends a message to this destination after processing every uploaded PDF file.

Run the Example Message Listener

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:

Sign in to Liferay at http://localhost:8080. Use the email address [email protected] and the password test. When prompted, change the password to learn.

Then, follow these steps:

  1. Download and unzip the example project:

    curl -O
  2. Build and deploy the project module.

    cd liferay-w3a4
    ./gradlew deploy$(docker ps -lq)

    This command is the same as copying the compiled module JAR to /opt/liferay/osgi/modules on the Docker container.

  3. Confirm the deployment in the Liferay Docker container console.

    STARTED com.acme.w3a4.impl_1.0.0 [2177]
  4. In the UI, upload a PDF file to Documents and Media.

After Documents and Media completes generating the PDF file preview, it sends a message to the destination where the example project’s MessageListener is listening. The Message Bus relays the message to all MessageListener’s registered with the destination. On receiving the relayed message, the project’s MessageListener logs the message’s payload and destination.

[liferay/document_library_pdf_processor-2][W3A4MessageListener:22] Received message payload [Ljava.lang.Object;@6df886c1 at destination liferay/document_library_pdf_processor

Here’s how it works.

Determine the Destination

Message destinations are referenced by their names. APIs specify destination names. For example, the DestinationNames class lists Liferay’s built-in destinations. The example MessageListener listens for messages sent to the Liferay destination named by the following String constant:


Search Liferay’s *DestinationNames classes in the source code or search other APIs for destinations to which you can add listeners. You’ll specify the destination name in your message listener.

Implement the MessageListener Interface

In the class where you want to receive messages, implement the MessageListener interface.

public class W3A4MessageListener implements MessageListener {

Override the receive method with logic for processing messages. Here’s the example receive method implementation:

	public void receive(Message message) {
		if (_log.isInfoEnabled()) {"Received message payload " + message.getPayload());

	private static final Log _log = LogFactoryUtil.getLog(


The above implementation logs the message payload and destination name. See the Message class for details on its other methods.

Register Your MessageListener With the Destination

Use a Component annotation to register your class to listen for messages at the desired destination. For example,

	property = "" + DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR,
	service = MessageListener.class
public class W3A4MessageListener implements MessageListener {

The above annotation registers the class as a MessageListener service component for receiving messages at a destination named DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR.

Set your component’s property value to your destination’s name.

When you deploy your project, the OSGi Runtime registers your MessageListener with the destination. Your MessageListener now receives messages sent to the destination.

Additional Information