7.7 Broadcasting messages

 < Day Day Up > 

In this section, we show how to send a broadcast message to all portlets on a page that have implemented the MessageListener interface. For example follow these steps:

  1. Create a new portlet application project:

    1. Select File-> New-> Portlet Application Project .

    2. Enter Message as the project name and in the event handling page, check only the option Add message listener .

    3. Click Finish .

    4. The new project has been added to DefaultEAR project; to be sure, open the application.xml file located in /DefaultEAR/META-INF/ folder. The Module tab should include the ActionEvent.war and Message.war. If there are more applications, remove them.

  2. Open the MessagePortlet.java file located in the /Java Source/message/ folder and modify the messageReceived() method to receive the broadcasted message. You only need to check for a message of type DefaultPortletMessage, which is the type of message sent by ActionEventPortlet. Once the message is extracted, it will set the text of this message as an attribute into the portlet request.

    Example 7-11. MessagePortlet.java
     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 DefaultPortletMessage ) {              String messageText = ((DefaultPortletMessage)msg).  getMessage  ();              // Add DefaultPortletMessage handler here  PortletRequest request = event.getRequest()  ;  request.setAttribute("message", messageText)  ;         }         else  {              // Add general PortletMessage handler here         }      } 
  3. Modify the MessagePortletView.jsp to display the message to the user .

    Example 7-12. MessagePortletView.jsp
     ..... <H3 style="margin-bottom: 3px">New application.</H3> <br>  <% if ( request.getAttribute("message") == null ) { %>   <B>No message has been received from another portlet application.</B>   <% } else { %>   <B>Message received: <%= (String)request.getAttribute("message") %></B>   <% } %>  ..... 
  4. Because you have added a new application, you have to restart the server. Restart the server and then right-click the ActionEvent project or Message project and choose Run on Server to test the application.

  5. You will see that the internal Web browser brings up the tree portlets on your screen, as shown in Figure 7-15.

    Figure 7-15. Before sending a broadcast message to all portlets

    graphics/07fig15.jpg

  6. Go to the Edit mode of the ActionEvent portlet and click the Red action button; now both portlets (the portlet in the same application of ActionEventPortlet and the portlet in the other application) display the message.

    Figure 7-16. After sending a broadcast message

    graphics/07fig16.jpg

Sending a message to a portlet in a different application

Now modify the actionPerformed() method of ActionEventPortlet.java class to send a message to a specific portlet in a different application project.

  1. Open the ActionEventPortlet.java file located in the /Java Source/actionevent/ folder of ActionEvent project. In the actionPerformed() method, when the action received is ACTION_RED , send the message only to the portlet called Message portlet.

    Example 7-13. actionPerformed() method-ActionEventPortlet.java
     if(actionString.equalsIgnoreCase(ACTION_RED)){         .......         // Send a portlet message         PortletMessage message = new DefaultPortletMessage(value);         try{  this.getPortletConfig().getContext().send("Message portlet",message)  ;         }catch (AccessDeniedException e){} ..... 
  2. Save the change and run the application. Now it is not necessary to restart the server; you only have to close the browser and run the project. Go to the Edit mode of the ActionEvent portlet and click the Red action button; now only the portlet called Message portlet receives the message.

Figure 7-17. After sending a message to a specific portlet

graphics/07fig17.jpg

 < 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