There are needs in practical scenario when it’s needed to do some huge processing in the background. It’s like some command is given to do something, while the application is not waiting for the response, such as sending bulk emails, handling scheduler’s requests, processing some data etc. Asynchronous message bus is great way to achieve this kind of requirement in Liferay.
The Message Bus is a mechanism for sending message payloads to different components in Liferay, providing loose coupling between message producers and consumers to prevent class loading issues. It’s located in the global class loader, making it accessible to every deployed web application. Remote messaging isn’t supported, but messages are sent across a cluster when ClusterLink is enabled.
Following are the message bus components which we'll use while implementing it:
Step 1: First of all we need to create messaging-spring.xml in /src/META-INF of your plug-in portlet. If you don’t have a service builder in the portlet, then you won't be having this META-INF folder, in that case you have to create a dummy service for the same. Now we have to put the following contents in this file:
Step 2: Now we have to make entry of this file in spring config. To do so, we have to open the service.properties file and make the entry of this file in "spring.configs" property.
Step 3: What remains wow is to create a class which should process the messages. This class must inherit the class MessageListener and should also implement method receive. (Please help yourself with proper imports)
Step 4: All set, now use the following code invoke this listener from your code from action class, scheduler etc. from wherever you want.
Thanks for reading.
Ref: Liferay Message Bus
The Message Bus is a mechanism for sending message payloads to different components in Liferay, providing loose coupling between message producers and consumers to prevent class loading issues. It’s located in the global class loader, making it accessible to every deployed web application. Remote messaging isn’t supported, but messages are sent across a cluster when ClusterLink is enabled.
Following are the message bus components which we'll use while implementing it:
- Message Bus: Manages transfer of messages from message senders to message listeners.
- Destinations: Addresses or endpoints to which listeners register to receive messages.
- Listeners: Consume messages received at destinations. They receive all messages sent to their registered destinations.
- Senders: Invoke the Message Bus to send messages to destinations.
Step 1: First of all we need to create messaging-spring.xml in /src/META-INF of your plug-in portlet. If you don’t have a service builder in the portlet, then you won't be having this META-INF folder, in that case you have to create a dummy service for the same. Now we have to put the following contents in this file:
Step 2: Now we have to make entry of this file in spring config. To do so, we have to open the service.properties file and make the entry of this file in "spring.configs" property.
Step 3: What remains wow is to create a class which should process the messages. This class must inherit the class MessageListener and should also implement method receive. (Please help yourself with proper imports)
Step 4: All set, now use the following code invoke this listener from your code from action class, scheduler etc. from wherever you want.
Thanks for reading.
Ref: Liferay Message Bus