14 July, 2011

Inter portlet communication (IPC)

Hello Everyone,
Here is the flow of IPC



Here below are the steps to follow for IPC :



Sender portlet

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 write code to send an event in sender class.

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


Receiver portlet

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

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

Second, add tis code to process an event

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

Now, just write code to your receiving portlet class,

@Override
public void processEvent(EventRequest request, EventResponse response)
{
Event event = request.getEvent();
if (event.getName().equals("message"))
{
String  message = (String) event.getValue();
}
}



Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...