Documentation‎ > ‎Portlets‎ > ‎

Using Events

Sample Code for Portlets that use Events to Share Data

This article provides sample code on how to implement portlet coordination using events. The sample portlet application contains two portlets located in same WAR file. The first portlet SendingEvent publishes event and the second portlet ReceivingEvent receives event and extracts payload. The payload in this sample is of type Person object.

1. portlet.xml:
The event definition, the reference from SendingEventPortlet to publish event and the reference from ReceivingEventPortlet to process event are highlighted.

<?xml version='1.0' encoding='UTF-8' ?>
<portlet-app xmlns='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' version='2.0'>
    <portlet>
        <description>SendingEvent</description>
        <portlet-name>SendingEvent</portlet-name>
        <display-name>SendingEvent</display-name>
        <portlet-class>com.softwareorbit.eventsample.SendingEventPortlet</portlet-class>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
        </supports>
        <resource-bundle>com.softwareorbit.eventsample.messages</resource-bundle>
        <portlet-info>
            <title>SendingEvent</title>
            <short-title>SendingEvent</short-title>
        </portlet-info>
        <supported-publishing-event xmlns:x='http://softwareorbit.com'>
            <qname>x:personEvent</qname>
        </supported-publishing-event>

    </portlet>
    <portlet>
        <description>ReceivingEvent</description>
        <portlet-name>ReceivingEvent</portlet-name>
        <display-name>ReceivingEvent</display-name>
        <portlet-class>com.softwareorbit.eventsample.ReceivingEventPortlet</portlet-class>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
        </supports>
        <resource-bundle>com.softwareorbit.eventsample.messages</resource-bundle>
        <portlet-info>
            <title>ReceivingEvent</title>
            <short-title>ReceivingEvent</short-title>
        </portlet-info>
        <supported-processing-event xmlns:x='http://softwareorbit.com'>
            <qname>x:personEvent</qname>
        </supported-processing-event>

    </portlet>
    <event-definition xmlns:x='http://softwareorbit.com'>
        <qname>x:personEvent</qname>
        <value-type>com.softwareorbit.eventsample.Person</value-type>
    </event-definition>

</portlet-app>

2.SendingEventPortlet: The following code shows implementation of processAction method. In this method, portlet creates payload object and calls ActionResponse.setEvent method to publish the event.

public class SendingEventPortlet extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
        try {
            QName qName = new QName("http://softwareorbit.com", "personEvent", "x");
            Person personEvent_Data = new Person();
            String firstName = request.getParameter("firstname");
            String lastName = request.getParameter("lastname");
            String emailid = request.getParameter("emailid");

            response.setRenderParameter("firstName", firstName);
            response.setRenderParameter("lastName", lastName);
            response.setRenderParameter("emailid", emailid);


            personEvent_Data.setFirstName(firstName);
            personEvent_Data.setLastName(lastName);
            personEvent_Data.setEmail(emailid);

            response.setEvent(qName, personEvent_Data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
..
}

3.ReceivingEventPortlet:
The extraction of payload object from the event is highlighted.

 @javax.portlet.ProcessEvent(qname = "{http://softwareorbit.com}personEvent")
    public void handleProcesspersonEventEvent(javax.portlet.EventRequest request, javax.portlet.EventResponse response) throws javax.portlet.PortletException, java.io.IOException {
        javax.portlet.Event event = request.getEvent();
        com.softwareorbit.eventsample.Person value = (com.softwareorbit.eventsample.Person) event.getValue();
        response.setRenderParameter("fname", value.getFirstName());
        response.setRenderParameter("lname",value.getLastName());
        response.setRenderParameter("email", value.getEmail());

    }


4. Person: The following is deffinition of payload object. It should implement Serializable inteface.

public class Person implements Serializable{
private String firstName;
    private String lastName;
    private String email;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}



ċ
EventSample.war
(11k)
Sukumar Konduru,
Aug 24, 2009, 8:20 PM
Comments