How to send and recieve events in JSR 286 portlets?

One of the important features of JSR 286 API is the ability to send and receive events from one portlet to another. From events we mean, enabling a portlet to send and receive events and perform state changes or send further events as a result of processing an event. Following configuration and code changes are required to support events in Portlets.

The portlet from where we have to set the object and publish it. Make the following configuration changes in portlet.xml file.


<portlet-app .....>
<portlet>
....
<supported-publishing-event>
         <qname xmlns:x="http:com.example/events">x:Address</qname>
</supported-publishing-event>
</portlet>

<event-definition>
        <qname xmlns:x="http:com.example/events" >x:Address</qname>
        <value-type>com.example.Address</value-type>
</event-definition>
</portlet-app>

For publishing we have set the object in processAction. Make the following changes in the portlet class of source portlet.


public void processAction (ActionRequest request, ActionResponse response) throws PortletException,IOException {
        QName qname = new QName("http:com.example/events" , "Address");
        String value = request.getParameter("personName");
        Address address= new Address ();
        address.setName(value);
        response.setEvent(qname, address);
}

The portlet from where we have to get the object and process it.


<portlet-app ...>
<portlet>
...
<supported-processing-event>
       <qname xmlns:x="http:com.example/events">x:Address</qname>
</supported-processing-event>
</portlet>
</portlet-app>

Java code to get the object value in our portlet API


public void processEvent(EventRequest request, EventResponse response) {
Event event = request.getEvent();
     if(event.getName().equals("Address")) {
       Address address= (Address)event.getValue();
       response.setRenderParameter("address", payload.getName());
   }
}

Address.java


public class Address implements Serializable {

private String name;
private String address;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address= address;
}
}

No comments:

Post a Comment