Message Boards

Liferay 7 - Message Bus

Amit G, modified 6 Years ago.

Liferay 7 - Message Bus

New Member Posts: 6 Join Date: 9/1/17 Recent Posts
We recently utilized Message Buses in Liferay 6.2 to handle bulk emails/sms as we were looking for asynchronous architecture.
A week back our management decided to move over to Liferay 7. But it seems there is no support for "customized message buses" in the Liferay 7.
There are some pre-configured message buses in Liferay 7 which though cannot be customized to this particular need.
It seems integrating a messaging bus system (like activeMQ etc.) would be an overkill and that is the reason we relied on Message Buses.

Please validate our understanding and suggest alternate ways to implement it.
thumbnail
David H Nebinger, modified 6 Years ago.

RE: Liferay 7 - Message Bus

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Amit G:
But it seems there is no support for "customized message buses" in the Liferay 7.


Blatantly false statement.

There are some pre-configured message buses in Liferay 7 which though cannot be customized to this particular need.


Well, you shouldn't really change Liferay's own bus listeners as you may break functionality the portal relies on.

It seems integrating a messaging bus system (like activeMQ etc.) would be an overkill and that is the reason we relied on Message Buses.


This is likely true. The LMB is a light-weight server side framework and not a full-blown MQ, but that lightness is often enough to get the job done.

Now if we take out the false assumptions, I think your post would have been better received had it actually been:

I would like to do some custom Liferay Message Bus messaging, but I haven't found any documentation on it after searching dev.liferay.com. Is this still supported and, if so, how would I do it?


Good question. Yes the message bus is still there, it is still supported and is still doable even under OSGi. I'm sorry the doco support isn't there, I'll see if I can raise it as an issue with the docs team.

There are two key parts for using the LMB; you need a listener and you need a sender.

For the listener, you can implement pretty much like:

@Component(
	immediate = true, property = {"destination.name=MyEchoDestination"},
	service = MessageListener.class
)
public class EchoMessageListener extends BaseMessageListener {

	@Override
	protected void doReceive(Message message) throws Exception {
		if (_log.isInfoEnabled()) {
			_log.info("Received: " + message);
		}

		String payload = (String)message.getPayload();

		if (_log.isInfoEnabled()) {
			_log.info("Message payload: " + payload);
		}

		String responseDestinationName = message.getResponseDestinationName();

		if ((responseDestinationName != null) &&
			(responseDestinationName.length() > 0)) {

			Message responseMessage = new Message();

			responseMessage.setDestinationName(responseDestinationName);
			responseMessage.setResponseId(message.getResponseId());

			//This is just for demo purposes

			responseMessage.setPayload(payload);

			_messageBus.sendMessage(
				message.getResponseDestinationName(), responseMessage);
		}
	}

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

	@Reference
	private volatile MessageBus _messageBus;
}


For OSGi, this will result in registering a MessageListener instance for the destination defined in the properties.

The next part you need is the message sending code:

@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=category.sample",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=Echo Portlet",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)
public class EchoPortlet extends MVCPortlet {

	...

	public void sendMessage(
		ActionRequest actionRequest, ActionResponse actionResponse) {

		if (_log.isInfoEnabled()) {
			_log.info("Sending message to DE Echo service");
		}

		Message message = new Message();

		message.setDestinationName("MyEchoDestination");
		message.setPayload("Hello World!");
		message.setResponseDestinationName("MyEchoResponse");

		_messageBus.sendMessage(message.getDestinationName(), message);
	}

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

	@Reference
	private MessageBus _messageBus;
}


That should get you there. Note that you're responsible for the MyEchoResponse listener emoticon









Come meet me at 2017 LSNA!
Amit G, modified 6 Years ago.

RE: Liferay 7 - Message Bus

New Member Posts: 6 Join Date: 9/1/17 Recent Posts
Thanks David for such an elaborate response. Apologies for posting the question as I did. We had spent a considerable effort in implementing message buses in 6.2 and were really feeling let down when we couldn't find any pointers in any of the forum - hope you understand. But we shall make sure we rephrase any of our future questions.

Last but not the least, I may ping you again in case there are any further queries on message buses while implementing on the lines you suggested.

Thanks again!!

Amit
Amit G, modified 6 Years ago.

RE: Liferay 7 - Message Bus

New Member Posts: 6 Join Date: 9/1/17 Recent Posts
Hi David,

We tried to execute the code that you shared but the Message listener's doReceive() is not being invoked though the message is being sent successfully to the message bus.
We have also attached the project with the two simple files . I am not what we are missing here.

Best
Amit
thumbnail
David H Nebinger, modified 6 Years ago.

RE: Liferay 7 - Message Bus

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Sorry about that, that was my oversight.

Along with the listener and the sender, you also need to configure messaging so the LMB will know to watch for the messages to the destination and invoke the listeners.

An example of how you do this is covered in the AuditMessagingConfigurator: https://github.com/liferay/liferay-portal/blob/master/modules/apps/foundation/portal-security-audit/portal-security-audit-wiring/src/main/java/com/liferay/portal/security/audit/wiring/internal/messaging/AuditMessagingConfigurator.java.











Come meet me at 2017 LSNA!
Amit G, modified 6 Years ago.

RE: Liferay 7 - Message Bus

New Member Posts: 6 Join Date: 9/1/17 Recent Posts
Thank you much David. Your guidance and inputs have been extremely helpful!!
thumbnail
Parth Ghiya, modified 6 Years ago.

RE: Liferay 7 - Message Bus

Junior Member Posts: 35 Join Date: 7/2/13 Recent Posts
What configurations needs to be done ?

I also am facing the same issue where my receivers doReceive() Method is not getting called.
ronak vora, modified 3 Years ago.

RE: Liferay 7 - Message Bus

Junior Member Posts: 25 Join Date: 9/26/18 Recent Posts
Same here into Liferay 7.1.2 doReceive method is not calling ???and how can we call by changing Impl class???