3/26/2011

Inter Portlet Communication in Liferay - Part 1

You will always have the requirement for portlets to communicate with each other.  For example: To book a flight you select the country you wish to fly to. When selecting a country in your portlet, the weather portlet also changes to display the current weather situation in your desired holiday location.


This is done by Inter Portlet Communication and in this series I want to show you how it´s done.


Part 1 : IPC Events



IPC Events, as introduced in JSR-286 (Portlets 2.0), work like any other event you might know from J2EE: They react to events being fired and the sender doesn´t need to care whether there is any receiver. 
This loosely coupled approach makes using the Events so much fun :)

In the example we will do right now we will just send a "Hello World" String from one portlet to another.

To choose a portlet as sender or receiver you need to add the configuration for the sender or receiver to the portlet.xml of the chosen portlet.

Sender configuration

To configure a portlet to be available to send events, add the following to your portlet.xml:

First, you have to define the event to send. To do this, you need to specify a namespace and a name for your event. This definition is the event definition that has to go into the portlet.xml of the sender AND of all receivers.
In our case it defines, that an event called "message" for the namespace http:your.private.namespace.com/yourEvent exists which is a String

<event-definition>
<qname xmlns:t="http:your.private.namespace.com/yourEvent">t:message</qname>
<value-type>java.lang.String</value-type>
</event-definition>

To choose your portlet as a sending portlet additionally add the following:

<supported-publishing-event>
   <qname xmlns:u="http:your.private.namespace.com/yourEvent">t:message</qname>
</supported-publishing-event>

This selects your portlet as a sending portlet for the chosen event. This is all for the configuration of the sender portlet. Now let´s take a look at the code to send an event.



public void sendEvent(ActionRequest actionRequest,
ActionResponse actionResponse) {
QName qname = new QName(
"http:your.private.namespace.com/yourEvent",
"message");
actionResponse.setEvent(qname,"Hello World");
return;
}


Believe it or not ... but this is all it takes to send out an event.


Receiver configuration

First add the event definition we created earlier to the portlet.xml of your receiving portlet.

Second, add the following "i am a receiving portlet" definition to the portlet.xml:

<supported-processing-event>
<qname xmlns:u="http:your.private.namespace.com/yourEvent">t:message</qname>
</supported-processing-event>

Now, just a little code to your receiving portlet class, and you can already test it:

@Override
public void processEvent(EventRequest request, EventResponse response) {
Event event = request.getEvent();

if (event.getName().equals("message")) {

//hooray we have our event
String  message = (String) event.getValue();
}
}
That´s it ... IPC with Events based on the JSR 286 Specification


If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.