7.5 PortletMessage
This interface defines the message object that will be sent between portlets inside the same portlet application on the same page. 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 7-4 illustrates a simple custom message used to carry a detail information about an entry of an agenda.
Example 7-4 Creating a custom message
import org.apache.jetspeed.portlet.PortletMessage; public class AgendaMessage implements PortletMessage { private AgendaBean entry; public AgendaBean getEntry() { return entry; } public void setEntry(AgendaBean entry) { this.entry = entry; } }
![]()
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 7-5; a message is sent with the information of an entry selected in other portlet in the same application.
Example 7-5 Sending a custom message
public void actionPerformed(ActionEvent event) throws PortletException { if( getPortletLog().isDebugEnabled() ) getPortletLog().debug("ActionListener - actionPerformed called"); // ActionEvent handler String actionString = event.getActionString(); PortletRequest request = event.getRequest(); // Add action string handler here if ( actionString != null && actionString.startsWith(ACTION_DETAILS)) { //get the entry selected from the actionString String opc = actionString.substring(actionString.indexOf("=")+1); int elem = Integer.valueOf(opc).intValue(); Vector list = getSessionAgenda(request); AgendaBean entry = (AgendaBean)list.elementAt(elem); //send a message with this object AgendaMessage msg = new AgendaMessage(); msg.setEntry(entry); getPortletConfig().getContext().send(null,msg); } } .....
![]()
If a portlet wants to receive this message, it has to implement the messageListener interface.
Example 7-6 Receiving a custom message
public void messageReceived(MessageEvent event) throws PortletException { if( getPortletLog().isDebugEnabled() ) getPortletLog().debug("MessageListener - messageReceived called"); // MessageEvent handler PortletMessage msg = event.getMessage(); // Add PortletMessage handler here if( msg instanceof AgendaMessage ) { AgendaBean detailEntry = ((AgendaMessage)msg).getEntry(); // Add DefaultPortletMessage handler here PortletRequest request = event.getRequest(); request.setAttribute("detailEntry", detailEntry); } else { // Add general PortletMessage handler here } }
![]()
Now you can see all the entries in one portlet and detailed information about an entry you selected previously in the other portlet. Figure 7-1 shows the result after selecting the third entry of the agenda.
Figure 7-1 Receiving a custom message with an entry of the agenda
ibm.com/redbooks
Prev | Next