I think Message Bus uses Observer Pattern, I haven't demonstrated that. Do it later..
1. we need to create a listener to listen and act when we listened.
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
public class BookConsoleListener implements MessageListener {
private static Log _log = LogFactoryUtil.getLog(BookConsoleListener.class);
@Override
public void receive(Message message) throws MessageListenerException {
try {
doReceive(message);
} catch (Exception e) {
_log.error("Unable to process message " + message, e);
}
}
protected void doReceive(Message message) throws Exception {
String booktitle = message.getString("bookTitle");
System.out.println("Book Title Entered: " + booktitle);
}
}
2. in your *serveiceImpl, trigger message bus to send message. Like in my BookLocalServiceImpl, I have a private method sendMessage, when use addBook, it'll send a message.
private void sendMessage(Book book, ServiceContext serviceContext) {
Message message = new Message();
message.put("bookTitle", book.getTitle());
MessageBusUtil.sendMessage("rujuan/book", message);
}
3. configure them together. Write a messaging-spring.xml in /WEB-INF/classes/META-INF folder
<?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.book_listener"
class="com.rujuan.book.controller.BookConsoleListener" />
<bean id="destination.book" class="com.liferay.portal.kernel.messaging.ParallelDestination">
<property name="name" value="rujuan/book" />
</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="rujuan/book">
<list value-type="com.liferay.portal.kernel.messaging.MessageListener">
<ref bean="messageListener.book_listener" />
</list>
</entry>
</map>
</property>
<property name="destinations">
<list>
<ref bean="destination.book" />
</list>
</property>
</bean>
</beans>
4. configure messaging-spring.xml in service.properties file like previously we configure ext-spring.xml
spring.configs=\
WEB-INF/classes/META-INF/base-spring.xml,\
\
WEB-INF/classes/META-INF/hibernate-spring.xml,\
WEB-INF/classes/META-INF/infrastructure-spring.xml,\
\
WEB-INF/classes/META-INF/cluster-spring.xml,\
\
WEB-INF/classes/META-INF/portlet-spring.xml,\
\
WEB-INF/classes/META-INF/dynamic-data-source-spring.xml,\
WEB-INF/classes/META-INF/shard-data-source-spring.xml,\
WEB-INF/classes/META-INF/messaging-spring.xml,\
\
WEB-INF/classes/META-INF/ext-spring.xml
Now we've done all steps, we just need to redeploy now.