Inter Portlet Coordination with JSR 286

Vikas Gupta

Coordination between portlets is a very common requirement. An example of information sharing between portlets can be a weather portlet displaying the weather information of a city and a map portlet displaying the location of the city. Since, both the portlets would be using the same zip code for a user, there should be mechanism provided by the portlal containers to allow portlets to share the zip code.

Prior to JSR 286, the support for inter portlet communication was rather minimal and information sharing between different portlets was accompalished primarily using application scoped session objects or vendor specific APIs. Both of above methods were rather problematic as in the former maintaining the uniqueness of the session attribute over a complex aaplication was a concern and in the later portability of the portlet was hampered. In order to provide coordination between portlets the Java Portlet Specification v2.0 (JSR 286) introduces the following mechanisms:

  1. public render parameters in order to share render state between portlets.
  2. portlet events that a portlet can receive and send. 

Let's have a look how to use the above features.

Public Render Parameters

In JSR 168, the render parameters propagated by one portlet were only available in the render method of the same portlet. This is explained in the following figure.
 jsr-168-render

In JSR 286, the render parameters propagated by one portlet can be made available to the render methods of other portlets. This is explained in the following figure.
jsr-286-render

In order to allow coordination of render parameters with other portlets, within the same portlet application or across portlet applications, in JSR 286 portlets, the portlet can declare public render parameters in its deployment descriptor using the public-render-parameter element in the portlet application section. In the portlet section each portlet can specify the public render parameters it would like to share via the supported-public-render-parameter element. The supported public-ender-parameter element must reference the identifier of a public render parameter defined in the portlet application section in a public-render-parameter element. The portlet should use the defined public render parameter identifier in its code in order to access the public render parameter.

Let's see how it works..
Consider the following example..

1. Set the public render parameters at the portlet application level.

 
    id1
    x:param1


    id2
    x:param2
 

2. Specify the render parameter the portlet would like to share in the portlet section.

 
  PortletA
  id1
  id2


  PortletB
  id1


PortletC
  id2

The public render paramters declared above are processed as explained in the figure below.
parameter-sending
Now, since the public render parameters are encoded in the URL, the values that can be shared between portlets are restricted to String and String arrays. Since, public render parameters are available only in the render method, the information shared by the portlets should be used for rendering the view rather than for processing the shared information. 

Portlet Events
Portlet events could be generated as a result of a user interaction with other portlets. The portlet event model is a loosely coupled, brokered model that allows creating portlets as stand-alone portlets that can be wired together with other portlets at runtime. Portlet programmers should therefore not make any specific assumptions about the environment of portlets they are running together with. The means of wiring different portlets together is portal implementation specific. An example where a portlet may want to offer receiving events is for state changes triggered by simple user interactions, e.g. adding an item to a shopping cart. By offering this as an event to other portlets these can trigger adding items to the shopping cart based on the user interactions happing inside these portlets.

EventPortlet Interface
In order to receive events the portlet must implement the EventPortlet interface in he javax.portlet package. The portlet container will call the processEvent method for each event targeted to the portlet with an EventRequest and EventResponse object.

Events are targeted by the portal / portlet container to a specific portlet window in the current client request. Events are a lifecycle operation that occurs before the rendering phase. The portlet may issue events via the setEvent method during the action processing which will be processed by the portlet container after the action processing has finished. As a result of issuing an event the portlet may optionally receive events from other portlets or container events. A portlet that is not target of a user action may optionally receive container events, e.g. a portlet mode changed event, or events from other portlets, e.g. an item was added to the shopping cart event. 

The JSR 286 event processing is explained in the following figure.
jsr-286-event-processing

To create portlets that use the eventing feature, follow these steps
1. Declare the events in the portlet.xml     
(i) Set the event definition at the portlet application level. This specifies the event name and the object type.


 
 . . .
 . . .
 

 
   x:Address
   com.xebia.Address
 
  @XmlRootElement
  public class Address implements Serializable {
      public Address() {
      }
      private String street;
      private String city;
      private String country;

      //getters and setters
   }

Note: The object must be serializable and must be instrumented with valid JAXB annotation. This might be required to ensure that portlets can send events to and receive events from remote portlets. However, in case of local communication, portal containers, for optimization purposes, might not serialize event payload.

(ii) In the portlet section, specify the event name defined above for those portlets that want to publish this event.


  ContinentPortlet
  ContinentPortlet
    .................
   
     x:Address
   

(iii) In the portlet section, specify the event name defined above for those portlets that want to process this event.


  ContinentMapPortlet
  ContinentMapPortlet
  
    x:Address
  


  ContinentInfoPortlet
  ContinentInfoPortlet
  
    x:Address
  

2. Issue an event in the portlet that was specified as supported-publishing-event in the portlet.

public class ContinentPortlet extends GenericPortlet {
   public void processAction(ActionRequest request, ActionResponse response)
   throws PortletException,IOException {
	QName qname = new QName("http:xebia.com/address" , "Address");
        Address add = new Address();
        //set values in address
        response.setEvent(qname, add);
    }
}

3. Process the event in the portlet that has specified as supported-processing-event in the portlet

public class ContinentInfoPortlet extends GenericPortlet {
  public void processEvent(EventRequest request, EventResponse response) {
    Event event = request.getEvent();
    if(event.getName().equals("Address")){
      Address payload = (Address )event.getValue();
      //process payload here
    }
  }
}

Portlet events provide more sphisticated way of exchanging information between portlets as compared to public render paramters as they can be used to share objects rather than simple string values. They also provide an additional callback method, processEvent, which can be used to process the event information before the view for the portlet in rendered. Also, portlet events share the information in a type safe manner as the event payload id bound to a type which we declare in the portlet.xml. Also, portlet specification does not standardizes how to wire the portlets, so, portal containers are free to choose convenient mechanisms to wire the portlets together.

However, as per the portlet specification, portlet events are not as reliable means of communication as is JMS, since the specification does not mandate the portal containers to persist the portlet event data. So, in case of server failures, the portlet events can be lost.

Summary
In this blog, we discussed about the JSR 286 inter portlet communication features. First, we discussed about the public render parameters and how it works. Secondly, we discussed about the portlet events, and it's working. So, how to decide when to choose either of the above two portlets. I would use the following principle

In case, the receiver portlet, does not need to do any processing/businees logic, it is advisable to use public render parameters as they avoid the overhead for portlets event creation and wiring the portlets together, which is required in case of portlet events. As already discussed, wiring portlets together is portal container specific and is an addtional overhead. However, to allow receiver portlets to process the shared information and sending type safe values, we need to use portlet events. 

Overall, JSR 286 portlet coordination features make complex portal applications modular and manageable.

Note: The source code of for portlet events can be downloaded from here.

Comments (45)

  1. [...] Space Server and Liferay. Combining these with existing features like eventing (Vikas has done a nice writeup on this), you can do all of your Web Space Server development without leaving the comfort of your [...]

  2. [...] これらに加え、イベント (Vikas はこれについてよい記事を書いています) などの既存の機能も使い、 WebSpace Server での全ての開発を [...]

  3. [...] dans WebSpace Server et Liferay. Si l’on ajoute à cela les fonctions existantes, comme la gestion graphique des communications entre portlets (Eventing Storyboard), vous pouvez réaliser l’ensemble de vos développements Portal sans quitter votre [...]

  4. Neha - Reply

    June 29, 2009 at 7:58 am

    hi,
    Can you please send this sample project along with source code. I want to do co-ordination in jboss portal

  5. Vikas Gupta - Reply

    June 29, 2009 at 11:06 am

    Yes, I can.
    Can you please let me know where to send?

  6. Neha - Reply

    June 29, 2009 at 1:20 pm

    please send it to [removed to protect privacy]

  7. Mohammed Ali - Reply

    July 2, 2009 at 10:00 am

    Hi,
    Can I pass an arraylist or linked list of objects through the event. I mean instead of " Address add = new Address();" we make " LinkedList add = new LinkedList();
    Thanks You

  8. New 2 portlets - Reply

    July 2, 2009 at 10:55 am

    Hi,

    Neha can you send the sample for Jboss Portal after you finish, or publish it somewhere, so people interesed (like me) can download it ?

    I also have another question regarding JSR 286: can a portlet be reseted every time it is accesed (by this I mean to render it's first page every time, not the portlet's last state)? Can this be done using portlet events ?

    Regards

  9. Smitha - Reply

    July 3, 2009 at 4:38 am

    Hi Neha,

    I am started learning the portlets. Appreciate if you could mail the sample code for the portlet events to my email address: learner_jsp@yahoo.com

    Thanks,

    Smitha

  10. Naga - Reply

    July 10, 2009 at 3:00 am

    hi,
    Can you please send this sample project along with source code. I want to do co-ordination in websphere portal.

    Thanks,

  11. mrk - Reply

    July 13, 2009 at 3:03 pm

    Hi Vikas,
    Can you please send this sample project. I am working on interportlet communication on websphere portal.

    Thanks in advance..

    mrk

  12. Ambarish - Reply

    July 21, 2009 at 9:36 am

    Hi Vikas,
    I am new to the world of portlets. Your blog has been of great help to me.
    I am working on inter-portlet communication on WebSphere Portal Express. Can you pls send me the code to my email.

    Thanks in advance,
    -- Ambarish

  13. Prabhu Ranagn - Reply

    August 7, 2009 at 11:50 am

    Hi Vikas,

    Can you please e-mail me the source code for developing IPC portlets. It would be nice if you can e-mail, the 'Source code' and 'deployable *.sar or *.war' file.

    Waiting for your early response....

    Regards,
    Prabhu.

  14. [...] Inter Portlet Coordination with JSR 286 [...]

  15. Mani - Reply

    September 22, 2009 at 8:47 am

    I am new to portlets and trying to learn interportlet communication on websphere portal. It would be great if you could mail me the sorce code..
    thanks in advance..!!!

  16. Michael Haulussy - Reply

    September 25, 2009 at 12:23 pm

    I am investigating JSR-286 portals and I am very interested in IPC eventing, can you send me the sourcecode?

    Regads,
    Michael

  17. Bojan - Reply

    October 8, 2009 at 10:31 pm

    Hello,

    During a University project, one of the teams was in charge of the IPC configuration and they didn't reached the goal.

    I'm trying, now that the project has finished, to do it myself because it looks like very interesting.

    Could you please send me the source of the project in order to help me in my quest? :)
    (kljakic.bojan@gmail.com)

  18. ravi - Reply

    October 13, 2009 at 7:01 pm

    Can send me source code to my email. Thanks in advance

  19. Nirmala - Reply

    October 15, 2009 at 3:22 pm

    Even after following all the above mentioned steps for Portlet Events, events are not getting triggered. Please let me know if any other configuration is missing in above explaination.

  20. Nirmala - Reply

    October 15, 2009 at 3:24 pm

    Also can you please send me the working version of source code of any example for IPC. Examples from net are not working!!

  21. Ved Swaminathan - Reply

    October 19, 2009 at 6:03 pm

    Could anyone send me the source code. PLease let me know if someone has got it.

  22. Beata - Reply

    October 21, 2009 at 1:11 pm

    Hi,
    Could anyone send me the source code too. My email: boobu@go2.pl

    Thanks!

  23. nirmal - Reply

    October 22, 2009 at 8:24 am

    Hi I am looking for source code so if any one has source code please do send to nirmal_p@rediffmail.com or please attach here so that every one can donwnload

    thanks in advance
    nirmal

  24. rita - Reply

    November 2, 2009 at 12:14 pm

    Hi,
    Can you also send me the source code, please!
    Thank in adavance,

    rita
    ritamalcata@hotmail.com

  25. digitalseance - Reply

    November 17, 2009 at 8:10 pm

    Nice writeup, I'm beginning my migration to IBM 6.1.x and beginning the migration to JSR286. We exploited the bug allowing multiple parameters being passed via JSR168 wires and now have to migrate to passing complex types via events. After reading your blog, and a little on the info center, I migrated our first app in under 20mins. Thanks! ::digg::

  26. Navs - Reply

    November 20, 2009 at 4:54 pm

    Hi Vikas,

    It is indeed the first writeup that is well written and is not promoting any brands!!
    Could you please email me the source code @ the email address I have provided in this "Leave a Reply" form.

    Cheers,
    Navs.

  27. Rohit - Reply

    November 22, 2009 at 8:15 pm

    Its very gud to understand jsr286 difference with 168. It is described in a well mannerd with diagrams n all. Thanks buddy!!

  28. Sharad - Reply

    November 23, 2009 at 11:21 am

    Hi Vikas,

    Can you pls. send me the source code at tosharad@gmail.com

    Thanks a lot!!

  29. Raghav - Reply

    January 5, 2010 at 8:22 pm

    Can you pls. send me the source code at raghscool@gmail.com

  30. [...] compatible avec une vision composant. Pour ceux qui veulent en savoir plus, la ressource issue du blog xebia est la meilleure référence que j’ai trouvé. Dans cette ressource, vous retrouverez [...]

  31. Anthony Lengle - Reply

    February 4, 2010 at 7:05 pm

    Very good text. I've found your blog via Google and I'm really happy about the information you provide in your posts. Btw your blogs layout is really broken on the Kmelon browser. Would be great if you could fix that. Anyhow keep up the good work!

  32. Srini - Reply

    February 7, 2010 at 2:38 am

    Excellent write up Vikas. Its very clear. Please send me the source code

  33. Rajesh - Reply

    February 18, 2010 at 10:39 am

    Very cool and descriptive write up man. thanks vikas.

  34. Srinivas - Reply

    March 3, 2010 at 8:45 am

    Hi,

    Can u please send me the source code , i am a star in portal technology.Please send the code to srinivas.bogireddy@gmail.com. Tnaks in advance.

    -Srinivas

  35. natoine - Reply

    March 8, 2010 at 10:50 pm

    Hi.
    I've used your post to make a french tutorial about IPC : http://www.natoine.fr/wordpress/?p=401
    I have a question maybe you can help.
    I proceed an event and i want to set an attribute as a result of the processing.
    I've tried to do a call to request.setAttribute("my_attribute_label" , MyObject) to the end of processEvent method declaration.
    But attribute my_attribute_label is always null when processing doView.

    Is there a way to set an attribute in processEvent call ?

  36. Karthik Rayala - Reply

    March 24, 2010 at 7:33 am

    Hi Vikas,

    Can you help me in creating a popup portlet using Dynamic User Interfaces. I request you to send me a sample on how to use this Dynamic Portlets or Dynmaic Pages.

    http://publib.boulder.ibm.com/infocenter/wpdoc/v6r1/index.jsp?topic=/com.ibm.wp.ent.doc_v6101/dev/wpsdynui_cpts.html

    Awaiting your response.

  37. venkat - Reply

    April 13, 2010 at 1:49 pm

    Hi guys

    this is venkat i am very new to jsr286 api ,you people are doing great , i want to join in this blog

  38. Srinivas - Reply

    June 23, 2010 at 8:27 am

    can i pass the parameters using Public render parameters between 2 WAR's

  39. Mark Ricard - Reply

    September 27, 2010 at 8:56 pm

    $350 to anyone who can provide me a working example with source code of:

    1.) Two portlets, each in their OWN war file, each in their own eclipse project, each containing a single 2.0 portlet using Icefaces 2.0 JSF portlet technology. (not 1.2)
    2.) Portlet "A" would have a single text box and command button that when pushed would show what was typed over in Portlet "B". The screen would NOT redraw as Icefaces would use its ajax push technology.
    3.) The communication mechanism is the event based mechanism described in this posting using "event-definition" type configuration.
    4.) Annotations should not be used.

    Please respond to chicagomark71@yahoo.com. Money paid via paypal. Your first deliverable would be the two war files themselves that I could test. First come first served, I only pay out to the person who solves this first.

  40. Lê Thị Phương - Reply

    June 29, 2011 at 11:07 am

    Hi all
    when use event portlet to communication how can we make client side of portlet auto update view when it has change view

  41. Leo - Reply

    July 13, 2011 at 5:44 pm

    Excellent ariticle! Could you email me a copy of the sample code as well please? I need to develop interactive portals for a university portal.

    Thanks!

    liuy23503@gmail.com

  42. Guna - Reply

    August 18, 2011 at 12:07 pm

    Very Nice Article...
    It helps me lot......

  43. chetan - Reply

    December 7, 2011 at 6:30 pm

    How can we navigate from one page to another using public render parameter just as we do in case of events.
    Is it possible to transfer render parameters from one page to another

    I am using websphere portal 7.

    please provide some sample to do .

    chetankpr@yahoo.co.in

  44. krithiga - Reply

    January 20, 2012 at 7:21 am

    I am trying to use setRenderParameters(Map ).
    In that case i want to share the map values in other portlet.

    example:
    processAction(..)
    {
    Map hm =request.getParametermap();
    response.setRenderParameters(hm);

    }

    Now how can i share the parameter "hm" in deployment descriptor.

    need guidance in this .

    krithiga

  45. Rohit Mallah - Reply

    July 24, 2012 at 12:23 pm

    Hi Can u please send me the code for this to my email ID rohitmallah1@gmail.com

Add a Comment