2.13 Portlet messaging

 <  Day Day Up  >  

One of the most significant advantages of the Portlet architecture is the portlets' ability to communicate with each other to create dynamic, interactive applications. Portlets can use messages to share information, notify each other of a user 's actions or simply help better manage screen real estate.

Messages can be sent to all portlets on page, a specific named portlet or to all portlets in a single portlet application. To send a message to all portlets on a page, you must send an instance of the DefaultPortletMessage.

In order to make full use of the potential, you need to adequately architect the entire portlet application anticipating inter-portlet communication. Attempting to implement effective and meaningful message after significant portlet development will cause some difficulty and may require the entire application to be overhauled. This is true for several reasons. For example, access to certain storage objects, such as PortletData, is limited to certain modes. Therefore, if the initial design of an application makes significant use of the PorltetData object, implementing messaging later to share configuration information would require a considerable effort. Furthermore, in order to reduce or eliminate code, action event and message event functionality can be combined into a common method. However, to achieve this, it is necessary to consider the information passed via the action or message objects.

To help you understand where messaging may fit into your applications, it is important to become familiar with some of the common uses of portlet messaging. This section will present two examples demonstrating common usage of portlet messaging. The first example illustrates how one portlet can use messaging to control the navigation of another portlet. The second example will demonstrate how a portlet can notify other portlets when a user has altered their configuration information via the Edit mode.

First, however, you must become familiar with the core objects used in the messaging architecture.

2.13.1 MessageListener

The MessageListener interface must be implemented by the portlets you want the portal server to send messages to. The interface defines the single method listed in Example 2-28 on page 103. Since the portlet may be notified by more than one other portlet and therefore may receive different types of messages, it should validate the type of message received prior to working with the object. This is illustrated in Example 2-28 on page 103.

Example 2-28. Implementing the MessageListener interface
 public void  messageReceived  (MessageEvent event) throws PortletException {    if (event.getMessage() instanceof DefaultPortletMessage) {       DefaultPortletMessage msg = (DefaultPortletMessage) event.getMessage();       String message = msg.getMessage();       //Do something based on the message    } } 

Be aware that when a portlet receives a message, it is not in Edit or Configure mode and therefore faces certain restrictions. For instance, portlets do not have write access to the PortletData object when they are not in Edit mode. Also, they cannot adjust the attributes stored in the PortletSettings object unless they are in Configure mode. Attempts to store attributes in these objects when not in the appropriate mode result in an AccessDeniedException.

Therefore, when attempting to share configuration or settings information between portlets, you need to choose your scope carefully or decide to persist to an outside resource.

2.13.2 MessageEvent

This object is sent to registered MessageListeners by the portlet container when a portlet executes the send method of the PortletContext object. There are two important methods available in this object

  • getMessage : returns the message object sent with this event. Since this method returns a PortletMessage, the result must be casted to the appropriate type as illustrated in Example 2-28.

  • getRequest :this method returns the current PortletRequest. The request can be used to access the PortletSession object or to store data to be used in the doView method.

2.13.3 DefaultPortletMessage

This object implements the PortletMessage interface and provides the basic functionality needed for sending string messages between portlets. If you broadcast a DefaultPortletMessage to null, it will be sent to all portlets on the page implementing the MessageListener interface. Example 2-29 on page 104 illustrates sending a simple broadcast message to all portlets on the same page regardless of application affiliation .

Example 2-29. Broadcasting a message to all portlets on a page
 PortletMessage msg = new DefaultPortletMessage("Some Message"); getPortletConfig().getContext().send(null, msg); 

2.13.4 PortletMessage

This interface defines the message object that will be sent between portlets. Since it is a flag interface, it does not define any methods to be implemented. Therefore, you are free to create message objects that can store a wide variety of information. Example 2-30 illustrates a simple custom message used to carry an employee object.

Example 2-30. Creating a custom message
 import org.apache.jetspeed.portlet.*; import java.net.*; public class  EmployeeMessage  implements PortletMessage {    private Employee emp;    public Employee getEmployee() { return emp; }    public void  setEmployee  (Employee employee) { this.emp = employee;} } 

If you simply need to send a string message between portlets, the DefaultPortletMessage provides this basic functionality. It is not possible to send a broadcast message using custom messages. Sending a custom message to null will only send the message to portlets implementing the MessageListener interface on the same page and deployed as part of the same portlet application. This is illustrated in Example 2-31.

Example 2-31. Sending a custom message
 public void  actionPerformed  (ActionEvent event) throws PortletException {       Employee employee = new Employee();       //Create an employee object with parameters from a form       EmployeeMessage msg= new EmployeeMessage();       msg.setEmployee(employee);       getPortletConfig().getContext().send(null, msg); } 
 <  Day Day Up  >  


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net