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

Liferay portlet phase

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

  • Define the event in the Portlet.xml file
    The first step is to define the event that will be fired and processed by portlets. The definition is set in the portlet configuration file Portlet.xml. Below the declaration :

    Xmlns : Give a namespace to the event
    Qname : Give a unique identifier in the namespace
    Value-type : Type of object stored in the event
  • Sender portlet
    • Declare that the portlet is publishing the defined event in portlet.xml
      To send the event, the portlet must specify that it’s publishing it. Add in portlet.xml file the <supported-publishing-event> property that refers to the event identifier. Example:
      Xmlns must be the same as the event definition. Same for the event identifier.
    • Firing the event
      Firing event from the sender portlet is done by using QName class that refers to the defined event by giving its namespace and identifier.

  • Receiver portlet
    • Declare that the portlet is processing the defined event in portlet.xml
      The receiver portlet must declare that it’s processing the defined event by adding the property         <supported-processing-event> in the portlet.xml

    • Process the event
      Fired events are processed in the processEvent phase. This phase was added as a new lifecyle in Portlet 2.0 specification.

      The value of the event must be based on the type defined in the deployment descriptor (Portlet.xml).

Concrete example

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

github-mark
The code source is available on Github. Download the source

Let’s consider two portlets :

  • CarList (Portlet 1): This portlet shows a list of cars. The user can select a car from the list. When the user select a car, an event will be fired with the selected car identifier. The car information will be displayed in the second portlet.
  • CarInformation  (Portlet 2): If an event is fired, it will be processed in the second portlet which will display the car information.
IPC using Porlet session

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

  • Portlet.xml

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

  • Selecting the car

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

  • Portlet.xml

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

  • Processing the event

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

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

Liferay inter portlet communication using event after selecting BMW

github-mark
The code source is available on Github. Download the source