| < Day Day Up > |
2.13 Portlet messaging
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
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
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
First, however, you must become familiar with the
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
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
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
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
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 PortletMessageThis 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 messageimport 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 > |