7.5 PortletMessage

 <  Day Day Up  >  

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.*; public class AgendaMessage implements PortletMessage {    private AgendaBean entry;    public AgendaBean getAgendaEntry() {       return entry;    }    public void setAgendaEntry(AgendaBean newEntry) {       this.entry = newEntry;    } } 

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();         // Add action string handler here         PortletRequest request = event.getRequest();         if ( actionString != null && actionString.equals(ACTION_DETAILS) ) {            String opc = request.getParameter("option");            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.setAgendaEntry(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 {          // MessageEvent handler          PortletMessage msg = event.getMessage();          // Add PortletMessage handler here  if( msg instanceof AgendaMessage ) {   AgendaBean detailEntry = ((AgendaMessage)msg).getAgendaEntry()  ;              // 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

graphics/07fig01.gif

 <  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