Roufid

Liferay IPC using event (Inter Portlet Communication)

Introduction

Once a portlet is placed on a portal page, its data is not directly shared with other portlets. IPC (Inter Portlet Communication) defines the ways that a Portlet can interact and communicate with another Portlet. There are four ways to make inter portlet communication :

In this post, we will see how to communicate between none-JSF portlets using events.

Events

JSR 286 (Portlet 2.0) introduced portlet communication using events. A new life cycle was introduced that occurs before the rendering phase aiming to handle event :

Liferay portlet phase

Below steps to follow to make a Portlet communication using events:

Concrete example

Let’s see a full example of using the IPC with event.


The code source is available on Github. Download the source

Let’s consider two portlets :

IPC using Portlet event

Defining the event

First step is to define the event that will be fired and processed by portlets in the portlet.xml file. If the portlets that handle the event are in the same Liferay Plugin Project, so only one definition of the event must be in the portlet.xml file. If not, each portlet must declare it in its portlet.xml file.

In this example, the two portlets are in the same Liferay Plugin Project. So they are defined in the same portlet.xml file along with the event.

The event that will be fired and processed will be called “SelectCarEvent” and will store the car identifier which is a java.lang.String. The declaration is as following :

Portlet 1 : CarList

Defining the CarList portlet in the portlet.xml file. This portlet will publish the “SelectCarEvent” event. It must define the <supported-publishing-event> property :

In the jsp view, i used an actionURL to process a portlet action from which the event will be fired. Below the code :

In the selectCarAction, the defined event (selectCarEvent) is fired with the selected car identifier:

Portlet 2 : CarInformation

Defining the CarInformation portlet in the portlet.xml file. This portlet will process the “SelectCarEvent” event. It must define the <supported-processing-event> property :

To process the event, the portlet must have a method annotated with javax.portlet.ProcessEvent with a qname parameter that refers to the event.

Rendering after deployment :

The CarList portlet is on the left. CarInformation portlet is on the right.

Liferay inter portlet communication using event before selecting a car

Below the result after selecting BMW car.

Liferay inter portlet communication using event after selecting BMW


The code source is available on Github. Download the source