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:
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.
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.
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.
<public-render-parameter> <identifier>id1</identifier> <qname xmlns:x="http://sun.com/params">x:param1</qname> </public-render-parameter> <public-render-parameter> <identifier>id2</identifier> <qname xmlns:x="http://sun.com/params">x:param2</qname> </public-render-parameter>
2. Specify the render parameter the portlet would like to share in the portlet section.
<portlet> <portlet-name>PortletA</portlet-name> <supported-public-render-parameter>id1</supported-public-render-parameter> <supported-public-render-parameter>id2</supported-public-render-parameter> <portlet> <portlet> <portlet-name>PortletB</portlet-name> <supported-public-render-parameter>id1</supported-public-render-parameter> </portlet> <portlet> <portlet-name>PortletC</portlet-name> <supported-public-render-parameter>id2</supported-public-render-parameter> <portlet>
The public render paramters declared above are processed as explained in the figure below.

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.
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.
<portlet-app ...> <portlet> . . . . . . </portlet> <event-definition> <qname xmlns:x="http:xebia.com/address">x:Address</qname> <value-type>com.xebia.Address</value-type> </event-definition> </portlet-app>
@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.
<portlet> <description>ContinentPortlet</description> <portlet-name>ContinentPortlet</portlet-name> ................. <supported-publishing-event> <qname xmlns:x="http:xebia.com/address">x:Address</qname> </supported-publishing-event> </portlet>
(iii) In the portlet section, specify the event name defined above for those portlets that want to process this event.
<portlet> <description>ContinentMapPortlet</description> <portlet-name>ContinentMapPortlet</portlet-name> <supported-processing-event> <qname xmlns:x="http:xebia.com/address">x:Address</qname> </supported-processing-event> </portlet> <portlet> <description>ContinentInfoPortlet</description> <portlet-name>ContinentInfoPortlet</portlet-name> <supported-processing-event> <qname xmlns:x="http:sun.com/events">x:Address</qname> </supported-processing-event> </portlet>
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.
Tags: JSR 286, portlets, websphere, websphere portal
Filed under websphere | 35 Comments »
[...] 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 [...]
[...] これらに加え、イベント (Vikas はこれについてよい記事を書いています) などの既存の機能も使い、 WebSpace Server での全ての開発を [...]
[...] 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 [...]
hi,
Can you please send this sample project along with source code. I want to do co-ordination in jboss portal
Yes, I can.
Can you please let me know where to send?
please send it to [removed to protect privacy]
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
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
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
hi,
Can you please send this sample project along with source code. I want to do co-ordination in websphere portal.
Thanks,
Hi Vikas,
Can you please send this sample project. I am working on interportlet communication on websphere portal.
Thanks in advance..
mrk
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
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.
[...] Inter Portlet Coordination with JSR 286 [...]
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..!!!
I am investigating JSR-286 portals and I am very interested in IPC eventing, can you send me the sourcecode?
Regads,
Michael
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)
Can send me source code to my email. Thanks in advance
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.
Also can you please send me the working version of source code of any example for IPC. Examples from net are not working!!
Could anyone send me the source code. PLease let me know if someone has got it.
Hi,
Could anyone send me the source code too. My email: boobu@go2.pl
Thanks!
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
Hi,
Can you also send me the source code, please!
Thank in adavance,
rita
ritamalcata@hotmail.com
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::
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.
Its very gud to understand jsr286 difference with 168. It is described in a well mannerd with diagrams n all. Thanks buddy!!
Hi Vikas,
Can you pls. send me the source code at tosharad@gmail.com
Thanks a lot!!
Can you pls. send me the source code at raghscool@gmail.com
[...] 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 [...]
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!
Excellent write up Vikas. Its very clear. Please send me the source code
Very cool and descriptive write up man. thanks vikas.
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
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 ?