Documentation

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

  1. Start the Liferay Docker image:

    docker run -it -m 8g -p 8080:8080 liferay/dxp:7.3.10-dxp-1
    
  2. After DXP/Portal initializes, visit it with your browser at http://localhost:8080.

  3. Sign in using the default credentials:

    User Name: test@liferay.com

    Password: test

  4. Download and unzip the example project:

    curl https://learn.liferay.com/dxp/latest/en/developing-applications/core-frameworks/message-bus/liferay-w3a4.zip -O
    
    unzip liferay-w3a4.zip
    
  5. Build and deploy the project module.

    cd liferay-w3a4
    
    ./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
    

    Note

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

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

    STARTED com.acme.w3a4.impl_1.0.0 [2177]
    
  7. 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:

DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR

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:

@Override
public void receive(Message message) {
	if (_log.isInfoEnabled()) {
		Object payload = message.getPayload();

		_log.info("Received message payload " + payload.toString());
	}
}

private static final Log _log = LogFactoryUtil.getLog(
	W3A4MessageListener.class);

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,

@Component(
	property = "destination.name=" + 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 destination.name 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