7.6.2 Sending a message
In this section, you will update ActionEventPortlet.java to send out a broadcast message from within its actionPerformed method. The message will be broadcast to all portlets implementing the MessageListener interface and using the DefaultPortletMessage object. Follow these steps:
1. If IBM RAD is not running, start the IBM Rational Application Developer by clicking Start | Programs | IBM Rational | IBM Rational Application Developer V6.0 | Rational Application Developer.
2. If the Test Environment is still running, stop the server by invoking Servers (make sure you switch to the Web perspective), right-click WebSphere Portal 5.1 Test Environment and click Stop.
3. Next, you will update the actionPerformed() method to instantiate a DefaultPortletMessage object (the parameter value contains the message to be included in the object) and send the message (the parameter null indicates that this is a broadcast message). Add the highlighted code to the actionPerformed method in ActionEventPortlet.java (located in the /Java Resources/Java Source/actionevent/ folder) as illustrated in Example 7-7. Example 7-7 Modifying implementation of actionPerformed() methods
... 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.equalsIgnoreCase(ACTION_RED) ) { // Create the string of HTML to be rendered String value = "Action <FONT color=\"#ff0000\">RED</FONT>"; // Create an instance of portlet data to store values PortletData portData = request.getData(); try { // Save value into portlet data portData.setAttribute("value",value); portData.store(); } catch (AccessDeniedException ade) { }catch (IOException ioe) { } // Send a portlet message PortletMessage message = new DefaultPortletMessage(value); try { this.getPortletConfig().getContext().send(null, message); }catch (AccessDeniedException ade){} } if( actionString.equalsIgnoreCase(ACTION_BLUE) ) { // Create the string of HTML to be rendered String value = "Action <FONT color=\"#0000ff\">BLUE</FONT>"; // Create an instance of portlet data to store values PortletData portData = request.getData(); try { // Save value into portlet data portData.setAttribute("value",value); portData.store(); } catch (AccessDeniedException ade) { }catch (IOException ioe) { } // Send a portlet message PortletMessage message = new DefaultPortletMessage(value); try { this.getPortletConfig().getContext().send(null, message); }catch (AccessDeniedException ade){} } }...
4. Save and close the ActionEventPortlet.java file.
5. Next, you will slightly update the ActionEventPortletView.jsp page to notify that you are now sending a message. Double-click the ActionEventPortletView.jsp under the /WebContent/actionevent/jsp/html/ directory. Figure 7-3 Project Explorer View
6. Insert the text highlighted in Example 7-8. Example 7-8 Adding the code to broadcast PortletMessage
<%@ page session="false" contentType="text/html" import="java.util.*, actionevent.*"%><%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %> <portletAPI:init/> <DIV style="margin: 6px"> <H3 style="margin-bottom: 3px">Welcome!</H3>This is a sample <B>view mode</B> page. You have to edit this page to customize it for your own use.<BR>The source file for this page is "/Web Content/actionevent/jsp/html/ActionEventPortletView.jsp". <br><% if (request.getAttribute("value") == null) { %> <B>No action performed, select your action in Edit Mode</B><% } else { %> <B><%=request.getAttribute("value") %> ... was selected ! and this information was broadcasted as a message.</B><% } %> </DIV>
Note: At this point, you have implemented all the required logic in ActionEventPortlet to be able to send a broadcast message from within its actionPerformed() method.
7. Save and close the ActionEventPortletView.jsp file.
ibm.com/redbooks
Prev | Next