Java Specification Request (JSR) 286 introduced event-based coordination for portlets. JSR 286 allows portlets to send and receive events. Some portlets can define events to be published, and other portlets define which ones can receive such events. IBM® Rational® Application Developer provides the tools to create such events for portlets. In this part you will see how to enable event handling, for portlets in Rational Application Developer. These event-enabled portlets are wired together at run time to create a connection between the portlets. This tutorial, which is Part 4 of a five-part series, concentrates on enabling events for existing portlets, creation of new event-enabled portlets, and wiring together two event-enabled portlets so they can share data.
- The first portlet, the Deals portlet, acts as an event publisher portlet i.e. it will emit an event to transfer data.
- The second portlet, the ContactInformation portlet, acts as an Event Subscriber portlet, which receive an event and invoke a method that processes the exchanged data.
This is the flow of events:
- The Deals portlet publishes an event to pass the Contact Name to the ContactInformation portlet.
- The ContactInformation portlet receives an event to invoke a method that processes the data, for example, the Contact Name.
- Based on the Contact Name, an appropriate SDO method is invoked to fetch information from the CONTACT_PERSON DB2 table.
To enable eventing (event handling) between two portlets, the first step is to create a new portlet:
- Right-click on the DealsProject and select New > Portlet.
- Enter
ContactInformationas the portlet name. - Also, make sure that the portlet type is set to Faces Portlet. (See Figure 1.)
Figure 1. New Portlet creation page
- Click Next, and make sure that the Generate a custom portlet class option is checked, as Figure 2 shows.
Figure 2. Option to generate a custom portlet class
- Click Finish.
This will create a JSP called ContactInformationView.jsp and a portlet class named ContactInformationPortlet.java. Now that you have two portlets in place, you can enable them for \event handling (called eventing in the JSR).
Enable the source portlet to publish an event
To enable the Deals portlet to publish an event, follow these steps:
- Open the DealsView.jsp file.
- In the palette view, locate Event Source Trigger in the Portlet drawer.
- Click Event Source Trigger and drag it onto the Name expression in the Contact Name column, as shown in Figure 3.
Figure 3. Drag the event source trigger in the JSP
This invokes the Insert Event Source Trigger dialog window shown in Figure 4.
Figure 4. Insert Event Source Trigger dialog window
- Click the New Event button to create a new event to publish.
- In the "Event – Enable this portlet to Publish events" dialog window that opens, for Event Name, enter
ContactId, as shown in Figure 5.
Figure 5. Event creation dialog window
- Click Finish to populate the Insert Event Source Trigger window shown in Figure 6.
Figure 6. Event Source Trigger dialog
- Type
#{vardealsInformationList.CONTACT_ID}in the Value to Send field. - Click Finish.
The NAME field will be hyperlinked, as shown in Figure 7.
Figure 7. Hyperlinked column
Also notice how the Deals portlet node is decorated with a Publish event icon (blue arrow preceded by a yellow flag) in the Enterprise Explorer view. The event name is listed below it as shown in Figure 8.
Figure 8. Event decorated node
- Save the file.
Now you will need to create a JavaServer Faces Managed Bean to hold the data being exchanged between portlets through eventing.
- Right-click the Java Resources node, and select New > Class, as shown in Figure 9.
Figure 9. Creating a new class
- Specify the Package as
com.ibmand Name asContactBean, as shown in Figure 10. - Click Finish.
Figure 10. Specify class name
- When the ContactBean.java file has been created and opens in the editor, replace its contents with the code in Listing 1.
Listing 1. Replace contents of ContactBean.java file with this code
package com.ibm;
public class ContactBean {
long id;
String picURL;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPicURL() {
return picURL;
}
public void setPicURL(String picURL) {
this.picURL = picURL;
}
} |
- Open the DealsView.jsp file.
- 15.In the Page Data View, right-click on Faces Managed Bean and choose New > Faces Managed Bean as shown in Figure 11.
Figure 11. Creating a Faces managed bean
- Specify Name as contactbean, Class as com.ibm.ContactBean, and for Scope, select Session from the context menu, as shown in Figure 12.
Figure 12. Specifying Faces Managed Bean properties
- Click Finish.
Create an SDO Relational Record
Enabling the ContactInformation portlet for eventing is a two-step process. First, you need to create an SDO Relational Record using the DB2 table CONTACT_PERSON, and then you need to enable ContactInformation to receive an event.
Note:
Creation of a Services Data Object (SDO) has nothing to do with event enabling. You are creating an SDO because of the requirements of your use case.
To create a SDO Relational Record, follow these steps:
- Open ContactInformationView.jsp in the Page Designer, and open the Page Data view.
- Right-click on SDO Relational Records, and choose New > SDO Relational Record.
Figure 13. SDO Relational Record creation
- In the Add Relational Record dialog window, provide
contactInformationas the name, and click Next.
Figure 14. Add Relational Record dialog window
- For table, choose the CONTACT_PERSON table under the ADMINISTRATOR schema, and click Next (see Figure 15).
Figure 15. Choose the table
- Choose all the columns, as shown Figure 16, and click Finish.
Figure 16. Select all of the columns
As Figure 17 shows, a new Relational Record is created under the SDO Relational Records node in the Page Data view.
Figure 17. SDO record in the Page Data view
Now that you have created the SDO Relational Record, you need to drag the Relational Record to the portlet JSP to be able to use data from the CONTACT_PERSON table:
- Drag the contactInformation (CONTACT_PERSON) node to the portlet JSP (ContactInformationView.jsp) to invoke the Insert Record dialog window shown in Figure 18.
Figure 18. Insert Record dialog window
- Uncheck the column CONTACT_ID and click Finish.
Notice that the code is generated in the portlet JSP.
Enable the target portlet to receive the event information
Now that you have created and generated the code to use data from the CONTACT_PERSON table, you need to put together a final piece: passing a contact ID to the ContactInformation portlet. This contact ID will be used to fetch a record of a particular person from the CONTACT_PERSON table to display the relevant information in the ContactInformation portlet. Recall that the Deals portlet publishes an event by the name of ContactId to pass the contact ID. Now you need to enable the ContactInformation portlet so that it subscribes to that event.
- Locate the ContactInformation portlet under Portlet Deployment Descriptor in the Enterprise Explorer.
- Right-click on the portlet, and select Event > Enable this Portlet to Process Event (see Figure 19).
Figure 19. Enable the portlet to process events
This invokes the "Events – Enable this Portlet to Process events" dialog window.
- Select ContactId from the Event Name drop-down menu, as shown in Figure 20.
Figure 20. Enable this Portlet to Process events dialog
- Click Finish.
Notice that the ContactId event node under the ContactInformation portlet node is decorated with a Subscribe event icon (yellow flag preceded by green arrow), as shown in Figure 21.
Figure 21. Decorated event node
The previous action also generates a processEvent method in the ContactInformationPortlet.java, the portlet class for the ContactInformation portlet. Listing 2 shows the generated code.
Listing 2. The generated processEvent method
public void processEvent(EventRequest request,
EventResponse response) throws
PortletException, java.io.IOException {
super.processEvent(request, response);
FacesContext facesContext = FacesContext.getCurrentInstance();
Event sampleEvent = request.getEvent();
if(sampleEvent.getName().toString().equals("ContactId")) {
Object sampleProcessObject = sampleEvent.getValue();
}
facesContext.release();
} |
The processEvent method is invoked for each event targeted to the portlet, with an EventRequest and EventResponse object.
Listing 3 modifies the code in Listing 2 to add the business logic to the processEvent method.
Listing 3. Custom code added inside the processEvent method
public void processEvent(EventRequest request,
EventResponse response) throws
PortletException, java.io.IOException {
super.processEvent(request, response);
FacesContext facesContext = FacesContext.getCurrentInstance();
Event sampleEvent = request.getEvent();
if(sampleEvent.getName().toString().equals("ContactId")) {
Object sampleProcessObject = sampleEvent.getValue();
Application app = facesContext.getApplication();
ValueBinding binding = app.createValueBinding("#
{contactbean}");
ContactBean mybean =(ContactBean) binding.getValue
(facesContext);
mybean.setId(Long.parseLong(sampleProcessObject.toString()));
} |
In this listing, the code simply gets an instance of Faces managed bean, ContactBean, that you created earlier. This instance contains the contact ID that is transferred by the Deals portlet through eventing. The instance of the ContactBean, itself, is stored in a session variable so that it can be reused across the portlets, spanning different requests.
To exchange data on invocation of events, you need to wire the portlets together so that they can exchange data.
- Perform the Run on Server operation on the project as you did earlier. Figure 22 shows how the rendered portlets look in the browser.
Figure 22. Two portlets rendered in browser
- Now, to wire the portlets together, click the Edit Page option from the Actions menu, as shown in Figure 23.
Figure 23. Edit Page option
Note:
This Edit Page option is present only when the theme is the PageBuilder one that is included with IBM® WebSphere® Portal, Version 7.0. For any custom themes, you can wire together the portlets by using the portal administration console wiring tool.
- Right-click on the small arrow symbol for the drop-down menu (shown in Figure 24) at the top-left of the Deals portlet window, and choose the Edit Wiring menu item.
Figure 24. Icon to choose the Edit Wiring option
Figure 25. Edit Wiring option
- Choosing the Edit Wiring menu item brings up the Wiring interface shown in Figure 26. The interface shows the available events that can be wired together. In this interface, you will find the events that you created earlier.
- In the Wiring interface, choose the {http://DealsProject/}/ContactId publisher event under the Deals icon that appears under the Select content to send heading.
- Choose the {http://DealsProject/}/ContactId subscriber event under the ContactInformation icon that appears under the Select a widget to receive content heading
- Click Done.
Figure 26. Wiring Interface
- Click the Save & Exit button.
- Now click on the individual Contact Names in the Deals portlet, and you will see the relevant information displayed in the Contact Information portlet (Figure 27).
Figure 27. Event-enabled portlets sharing data
This concludes Part 4 of this five-part series. Part 5 shows how to create portlets that can fetch data from IBM® Connections.
| Description | Name | Size | Download method |
|---|---|---|---|
| Scenarios and use cases for the series | scenarios_use_cases_sample.zip | 5MB | HTTP |
Information about download methods
Learn
- For details, read Coordination between portlets in the JSR286 Java Portlet specification.
- Find out more about Rational Application Developer:
- Browse the Rational Application Developer for WebSphere Software page on developerWorks for links to technical articles and many related resources.
- Explore the Information Center for detailed instructions, especially the section titled "Creating portlets and portlet projects" (Developing > Developing portal and portlet applications > Portlet development overview > Developing portlets)
- Visit the Rational software area on developerWorks for technical resources and best practices for Rational Software Delivery Platform products.
- Stay current with developerWorks technical events and webcasts focused on a variety of IBM products and IT industry topics.
- Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM products and tools, as well as IT industry trends.
- Watch developerWorks on-demand demos, ranging from product installation and setup demos for beginners to advanced functionality for experienced developers.
- Improve your skills. Check the Rational training and certification catalog, which includes many types of courses on a wide range of topics. You can take some of them anywhere, any time, and many of the "Getting Started" ones are free.
Get products and technologies
- Try Rational Application Developer for WebSphere Software, free.
- Evaluate IBM software in the way that suits you best: Download it for a trial, try it online, use it in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement service-oriented architecture efficiently.
Discuss
- Check Rational Application Developer wiki to keep up with news and to contribute.
- Join the Development Tools forum to ask questions and participate in discussions.
- Rate or review Rational software. It's quick and easy. Really.
- Share your knowledge and help others who use Rational software by writing a developerWorks article. Find out what makes a good developerWorks article and how to proceed.
- Follow Rational software on Facebook, Twitter (@ibmrational), and YouTube, and add your comments and requests.
- Ask and answer questions and increase your expertise when you get involved in the Rational forums, cafés, and wikis.
- Get social about thought leadership. Join the Rational community to share your Rational software expertise and get connected with your peers.





