Friday, January 28, 2005

Java Portlets - Inter-Portlet Communication II

Continuing from my previous article, this article describes the alternate method to share data across multiple portlets, by making use of PortletContext.

One can directly make use of setAttribute(String, Object) method to share data across portlets.

In this method, there's a different catch in this method. A porlet that sets attribute is shared by the portlet, no matter how many times it is used in a page or anywhere. It is even shared by all users. Use this method with caution!

I would suggest using this method when a common notification needs to be shown to others. An example of this would be showing unavailability of a service used by a portlet. Real-life example would be a portlet getting latest news from Yahoo should show an available note when the service becomes unavailable.

Think once again before using inter-portlet communication. Is it really required? Cannot you do without it?

Thursday, January 27, 2005

Java Portlets - Inter-Portlet Communication I

Inspired from Portlet Tip 1. Thanks for the comment for the original URL.

There are many articles on this topic that you can search over the net. However, I don't really understand the reason for having so. Well, you can put whole of things in session or portlet-context and that's really the way to do so. Interestingly, when I searched on Google, the top searches were the threads complaining about unable to work on inter-portlet communication.

Here's how you can achieve it using PortletSession.

By default setAttribute(String, Object) method sets the attribute in the scope of the portlet.

Make use of the overloaded method setAttribute(String, Object, int) method with the scope being set as APPLICATION so that it can be shared across portlets. Note, however, that the application scope here limits to the session and is not same as the APPLICATION scope of the servlet.

As such, you can set the attribute in one portlet and get using another. However, there is a catch.

JSR-168 does not specify the order in which the rendering methods - doView, doEdit or doHelp etc - for the various portlets on a page should be called. It is, as such, possible that the order in which you expected a method to be called may not turn out to be so.

But there's a ray of hope. processAction is called before any rendering method. Therefore, it is advisable to use this method to set all attributes that need to be shared across portlets and then be retrieved in rendering methods.