7.7 Broadcasting messages

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...

1. Create a new portlet project:

a. Select File | New | Portlet Project.

b. Enter Message as the project name and select WebSphere Portal V5.1 as Target server.

c. In the event handling page, check only the option Add message listener.

d. Click Finish.

2. Open the MessagePortlet.java file located in the /Java Resources/JavaSource/message/ folder and modify the messageReceived() method to receive the broadcast 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. You need to configure the new project to run in test environment. Right-click WebSphere Portal V5.1 Test Environment server and select Add and remove projects. Select MessageEAR in the available projects panel and click Add.

Figure 7-11 Add and remove projects window

5. Click Finish.

6. Before starting the server open the server properties by double-clicking in WebSphere Portal 5.1 Test environment server. Click Portal panel and check enable base portlets for portal administration and customization option.

Portlets that send and receive message must be on the same page. By default, when you configure more than one project on the server they will appear in different pages. You will need to customize the ActionEvent page adding the new Message portlet.



Figure 7-12 Portal options

7. Save and close the server options window.

8. Right-click the ActionEvent project or Message project and choose Run | Run on Server.

9. When the Web browser appears, you will see the applications in different pages.

Figure 7-13 Running two portlet applications

10. To customize your page, select Administration in the portal theme. You need to log in as an administrator user to see this option.

11. In Administration window, select on the left panel Manage Pages under Portal User Interfaces. On the right panel, you will see options to work with your pages. Select My Portal and then Test Environment. You will see a page as show in Figure 7-14.

Figure 7-14 Manage pages

12. Click the pencil icon to edit page layout.

Figure 7-15 Edit page layout

13. Click Add portlet button. A new window appears with all portlets that have been installed. Fill the search file with the name of the portlet you are looking for, Message in our example, and click Search

Figure 7-16 Adding a portlet to a page

14. In the results of your search you will see the Message portlet, check it and click OK.

15. Now you will see the Message portlet in ActionEvent page layout.

16. Click DONE.

17. Click My Portal to test the application.

18. You will see the tree portlets on ActionEvent page.

Figure 7-17 Before sending a broadcast message to all portlets

19. 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-18 After sending a broadcast message


Redbooks
ibm.com/redbooks

 

Prev | Home