Creato da pastuweb.com
Share My Page
My Social Accounts
Account FaceBook Account LinkedIn Account Twitter Account Google Plus Account Git Hub
The message bus is pretty simple. Applications can send a message over the bus to a listener.
The listener, by virtue of its being a listener for this particular type of message, can hear the message and knows what to do with it.
 
The Message Boards portlet sends a message to a listener over the message bus and then returns the response to the user, who can continue interacting with the portal in any way he or she chooses.
Meanwhile, the listener that receives the message can act on it on a separate thread.
 
Configuring the bus to send your messages
 
You need to add some Spring configuration to your WEB-INF/src/META-INF folder.
 
 
This configuration file declares three things that you need in order to make the message bus work for you:
  • One or more listeners to listen for messages and take action
  • One or more destinations so the bus can deliver your messages
  • A configurator that links the destination(s) to the listener(s)

The Spring configuration goes in a file called messaging-spring.xml, in the META-INF folder in src (docroot/WEB-INF/src/META-INF/messaging-spring.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans default-destroy-method="destroy"
 default-init-method="afterPropertiesSet"
 xmlns="http://www. springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
<bean id="messageListener.nota_listener"
class="com.appuntivari.portlet.NotaConsoleListener" />
 
<bean id="destination.nota"
class="com.liferay.portal.kernel.messaging.ParallelDestination">
<property name="name" value="appuntivari/nota" />
</bean>
 
<bean id="messagingConfigurator"
    class="com.liferay.portal.kernel.messaging.config.PluginMessagingConfigurator">
 
<property name="messageListeners">
<map key-type="java.lang.String" value-type="java.util.List">
<entry key="appuntivari/nota">
<list value-type="com.liferay.portal.kernel.messaging.MessageListener">
<ref bean="messageListener.nota_listener" />
</list>
</entry>
</map>
</property>
 
<property name="destinations">
<list>
<ref bean="destination.nota"/>
</list>
</property>
</bean>
 
</beans>

 

1) You first define the classes that will be your listeners and your destinations
but you can have as many listeners and destinations as you want.
 
2) The configurator is where you wire them together. 
This tells the message bus that for the destination appuntivari/nota, the listener that should be injected into the call is the one defined as the messageListener.nota_listener, which of course maps to your NotaConsoleListener.
And the destination type is Liferay's implementation of a parallel destination, because you want this to execute as soon as possible, rather than wait in a queue for other jobs to complete.
 
Implementing the sender and the listener
 

 

Next, let's implement the listener class and make sure you send the message in the event you've defined, which is adding a nota.
 

 

This event is in the NotesLocalServiceImpl class, specifically in the addNota() method.
You can add a small method there called sendMessage() to build the message and then send it across the bus:

 

 
private void sendMessage(Notes nota, ServiceContext serviceContext) {
Message message = new Message();
message.put("userId", serviceContext.getUserId());
message.put("nota_title", nota.getTitle());
MessageBusUtil.sendMessage("appuntivari/nota", message);
}
 
So I will send a message at the destination "appunti/nota". In this destination there is a Listener that process some stuff.
You can, of course, include as much or as little information as you have on hand.
 
Now, I'll implement:
 
public class NotaConsoleListener implements MessageListener {
 
public void receive(Message message) throws MessageListenerException {
try {
doReceive(message);
}
catch (Exception e) {
e.printStackTrace();
}
}
 
protected void doReceive(Message message) throws Exception {

 

String notaTitle = message.getString("nota_title");
System.out.println("Title Nota entered: " + notaTitle );
}
 
}
 
The Message class provides many convenience methods for getting various Java types, and you can send your own classes across the message bus as well.
 

 

In order to find the Spring configuration file, you need to tell Liferay where to look for it. This is done by adding a context configuration location to your project's web.xml file:
 
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/META-INF/messaging-spring.xml
</param-value>
</context-param>
 
Other scope, you might send an email to everyone with the Title of new Nota. That way, those users are notified when a new nota is entered, and they can go to read.  You could write another listener that updates a web content page with a count of the number of notes entered and so on.