Inter Portlet Communication : Events

This post is on Events in portlets in IBM WebSphere portal 8 using RAD.

JSR 286 (Portlet 2.0) defines a lifecycle for events, so that eventing is possible between portlets that are in same/different web applications. An event is a lifecycle operation that occurs before the rendering phase. Events allow portlets to respond on actions or state changes not directly related to an interaction of the user with the portlet.

Important Points:

  • Declare events in its deployment descriptor (Portlet.xml) by using <event-definition> element in portlet application section.
  • In portlet section each portlet specifies the events it would like to publish through the  <supported-publishing-event> element and the events it would like to process through the <supported-processing-event> element.
  • The portlet creates events using the setEvent() method during action processing.
  • Portlets can also create events during the event phase by calling the setEvent() method on EventResponse.
  • The events are processed by the portlet container after the action processing has finished.
  • The portlet container calls the processEvent() method for each event targeted to the portlet with an EventRequest and EventResponse object.
  • The portlet can access the event that triggered the current process event call by using the EventRequest.getEvent() method.
  • Events names are represented as QNames to identify them uniquely. The event name can be retrieved by using the getQName() method that returns the complete QName of the event, or by using the getName().

In The sample application we will have two portlets in different wars. I will be using faces portlet (not necessary).  After deploying the application on portal server on same/different page we need to do wiring among those. I will be focusing on manually writing the code. Of-course you can use RAD wizard for it, using which you don’t need to see configuration file at all. But its good to know what actually is happening there.

Sample Application will have selectcity portlet and an products portlet. Selection on selectcity portlet will automatically update the products portlet. The idea is similar to some telecom company sites where you select city from the top dropdown and the plans/offers change in the main page.

Application 1:  SelectCity

  • Create a Portlet project SelectCity. In the wizard, create a portlet and check to create a custom portlet class. You can add custom class afterward also, by creating a class extending FacesPortlet and making an entry in portlet.xml.
  • Make your manage beans accordingly. I made a simple Action/Form classes.
  • My page only contains a dropdown with fixed city list, on change of which a JS is called that submits a page.
  • Following is the portlet.xml for SelectCity:
    SelectCityPortletXML
  • Go to your custom portlet class for this portlet. You can set event in the processAction method as follow:
    ProcessAction
    Here selectCityForm is my manage bean.
    Note – If you are using Faces portlet and having all you logic in Manage beans, make sure you set the event after super.processAction call, otherwise the value set will be null.

Application 2:  Products

  • Create a Portlet project Products. In the wizard, create a portlet and check to create a custom portlet class.
  • Make your manage beans accordingly. I created only a Form bean.
  • Following is the portlet.xml for Products:
    ProductsPortletXML
  • Go to the custom portlet class for this portlet and override processEvent method of the parent as follows:
    SetEvent
    Here product Form is the manage bean.

Now Deploy both the application.

There are two ways that portlets could be arraged: Same page or Different Page.

Lets see when they are in same Page:

AddingPortletToPage

 

AddingWiresSamePage

 

 

All done. Now Test your page.

Second possibility is when the portlets are in different page. In such scenario we first have to define a global target in the page where we are processing the even as follows:

Then from the source page add wires to the target page as follows:
DefineGlobalTarget
AddEventsDifferentPage

Test your pages:
IPCResult

Here we have only send one String parameter. You can also send a whole Form or other class using Events. All you need to do is to implement Serializable interface.

Hope this helps!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: