7.6 Sample scenario

 < Day Day Up > 

Message events are a way for portlets to communicate with each other. This is accomplished through the familiar event-listener model. Portlets that need to listen for message events must implement a MessageListener interface, and portlets that need to send message events do so within the handling of their own Action Events, as you will see in this sample scenario. Message events can be sent to named portlets or broadcast to all portlets on the same page. All events are handled within the page's event-processing phase, after which comes the content generation phase.

For this sample scenario, the action event sample portlet application (see Chapter 5, "Action event handling" on page 181) will be modified to include message events so that you will see an example of how these can work together within portlet applications.

7.6.1 Description

In this scenario, you will enhance the ActionEvent portlet application to send messages to a new message receiver portlet as illustrated in Figure 7-2 on page 232.

Figure 7-2. Message Event handling scenario

graphics/07fig02.gif

Figure 7-2 shows the flow for this scenario, as follows :

  1. The actionPerformed() method in the ActionEventPortlet.java portlet will be extended to send a broadcast message event (DefaultPortletMessage) upon arrival of action events.

  2. The MessageReceiver portlet, which will implement the MessageListener interface, receives the message in the new messageReceived method.

  3. The received message is saved in the PortletRequest object.

  4. The Portal invokes the doView method which in turn invokes the JSP (select).

  5. The JSP retrieves the message from the request object and displays the message.

This scenario will be implemented using a broadcast style of message event rather than point-to-point messaging. In addition, the DefaultPortletMessage object will be used.

Note

While the DefaultPortletMessage object allows you to send messages to portlets in different applications, you can only send a String type message.


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 Studio is not running, start the IBM WebSphere Studio Site Developer by clicking Start-> Programs-> IBM WebSphere Studio-> Site Developer 5.0 .

  2. If the Test Environment is still running in Debug mode, stop the server by invoking Servers (make sure you switch to the portlet perspective), right-click Test Environment (started in Debug mode) 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 Source/actionevent/ folder) as illustrated in Example 7-7.

    Example 7-7. Modifying implementation of actionPerformed() method
     ...     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        if(actionString.equalsIgnoreCase(ACTION_RED)){           // Create the string of HTML to be rendered           String value = "Action <FONT color=\"#ff0000\">RED</FONT>";           // Create a portlet request           PortletRequest request = event.getRequest();           // 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 e){}  }        if(actionString.equalsIgnoreCase(ACTION_BLUE)){           // Create the string of HTML to be rendered           String value = "Action <FONT color=\"#0000ff\">BLUE</FONT>";           // Create a portlet request           PortletRequest request = event.getRequest();           // 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 e){}  } } ... 
  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 /Web Content/actionevent/jsp/html/ directory.

    Note : Make sure ActionEventPortletView.jsp is in the html directory.

    Figure 7-3. Edit tView.jsp

    graphics/07fig03.jpg

  6. Insert the text highlighted in Example 7-8.

    Example 7-8. Adding the code to broadcast PortletMessage
     <%@ page 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> 
  7. Save and close the ActionEventPortletView.jsp file.

    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.6.3 Creating the target portlet

In this section, you will use a wizard to create the target portlet to receive the message sent by ActionEventPortlet.java.

  1. Click File-> New-> Other .

  2. Select Portlet Development from the left column and Portlet from the right. Click Next to continue.

    Figure 7-4. Add a portlet wizard

    graphics/07fig04.jpg

  3. If required, select the ActionEvent project.

  4. Examine and accept the default values. The wizard will add a basic portlet to the ActionEvent project.

    Figure 7-5. Adding a portlet

    graphics/07fig05.jpg

  5. Click Next .

  6. In the next window, change the portlet settings information to be relevant to the portlet you are adding. Enter the following information:

    1. Application name : MessageReceiver application

    2. Portlet name: MessageReceiver portlet

    3. Portlet title: MessageReceiver portlet

    4. Check the Change code generation options box to enter the following information:

      1. Package prefix: messagereceiver

        Important : Use lowercase for this prefix to follow naming conventions.

      2. Class prefix: MessageReceiver

    Figure 7-6. Portlet settings

    graphics/07fig06.jpg

  7. Click Next .

  8. Uncheck the Add form sample and the Add action listener boxes. Check the Add message listener box to add the messageReceived method. Click Next .

    Figure 7-7. Adding a message listener

    graphics/07fig07.jpg

  9. Do not check the Add credential vault handling box (not required in this sample scenario). Click Next .

    Figure 7-8. Credential vault handling is not required

    graphics/07fig08.jpg

  10. Leave the options for markups and modes unchecked. Click Finish to add the portlet to your project.

    Figure 7-9. Markups and modes

    graphics/07fig09.jpg

  11. You will now see the new portlet files in the Navigator panel.

    Figure 7-10. Navigator panel

    graphics/07fig10.jpg

  12. Open the MessageReceiver.java file located in the /Java Source/messagereceiver/ folder by double-clicking it.

  13. Add the following highlighted code to this file to receive the broadcast Portlet Message in the messageReceived () method.

    Note : The messageReceived() method implements the logic to receive the PortletMessage. In this example, you only need to check for messages of type DefaultPortletMessage, which is the type of message sent by ActionEventPortlet. Then the message is extracted via the getMessage() method, and you set the text of this message into a portlet request as an attribute with name MyMessage.

    Example 7-9. MessageReceiver.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("MyMessage", messageText)  ;        }        else {            // Add general PortletMessage handler here        }    } ... 
  14. When you are done, save the file and exit.

  15. Now that you can receive the message, you will need to modify the MessageReceiverView.jsp to display the message to the user . To edit this file, open the /Web Content/messagereceiver/jsp/html/ folder and double-click the file.

    Note : The Java code inside the scriptlet checks the value of the portlet request attribute MyMessage. If null, no message has been received yet, and it displays that it is ready to receive a message. If not null, the message is displayed with HTML markup. Make the following changes.

    Example 7-10. MessageReceiverView.jsp file (message receiver portlet)
     <%@ page contentType="text/html" import="java.util.*, messagereceiver.*"%> <%@ 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/messagereceiver/jsp/html/MessageReceiverView.jsp".  <br>   <% if (request.getAttribute("MyMessage") == null) { %>   <B>Ready to receive message...</B>   <% } else { %>   <B>Received a message:</B>   <B><%= request.getAttribute("MyMessage") %></B>   <% } %>  </DIV> 
  16. When you are done, save the file and exit.

    You have now implemented the code to receive and display a broadcast portlet message to the user.

7.6.4 Running the portlet application

In this section, you will run the portlet application you have developed to send a message from the message sender portlet (ActionEventPortlet.java) to the message receiver portlet (MessageReceiver.java). Follow these steps:

  1. Right-click the ActionEvent project and choose Run on Server . The project will be published and then started.

    Note : You will see that the internal Web browser brings up the two portlets on your screen, as shown in Figure 7-11. Notice that the ActionEvent portlet indicates that no action has been performed and the MessageReceiver portlet indicates that it is ready to receive a message.

    Figure 7-11. Running the messaging project in the Portal Server Test Environment

    graphics/07fig11.jpg

  2. Choose the Edit mode of ActionPortlet as indicated with the arrow in Figure 7-11 on page 242. Click the Red Action button. This will both (a) create an action that you will see the action in ActionPortlet, and (b) broadcast a message which will be sent and shown in MessageReceiver.

    Figure 7-12. Creating an action and broadcasting the message

    graphics/07fig12.jpg

  3. You will see the value shown in both the ActionEvent portlet and the MessageReceiver portlet.

    Note : In summary, you have seen how the portlet API implements message events which can be useful for passing data between portlets that need to be notified of other portlet's actions and events. This is a very useful feature of the API when building portlet applications that contain multiple portlets.

    Figure 7-13. Red action and Red message broadcast

    graphics/07fig13.jpg

  4. You can enter Edit mode again and select the Blue Action button. The results will again be displayed accordingly .

    Figure 7-14. Blue action and message broadcast

    graphics/07fig14.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