Developing Cooperative Portlets

Starting from JSR 286, the POrtlet Specification defines standard way in which portlet can communicate with each other. This system is called eventing.

You can implement support for eventing by following these steps

  • Define the event that your portlet can either publish or consume in the portlet.xml like this. Add this section to both source and target portlet's portlet.xml

    <event-definition>
    <description>Sample Event</description>
    <name>SampleEvent</name>
    <value-type>java.lang.String</value-type>
    </event-definition>

    In this section we are saying that Sample Event is event of java.lang.String type.

  • Change your source portlet to define that it can publish Sample Event by adding <supported-publishing-event> element to it.

    <portlet>
    <portlet-name>SourcePortlet</portlet-name>
    <display-name xml:lang="en">SamplePortlet</display-name>
    <display-name>SourcePortlet</display-name>
    <portlet-class>com.ibm.sampleportlet.SamplePortlet</portlet-class>
    <expiration-cache>0</expiration-cache>
    <supports>
    <mime-type>text/html</mime-type>
    <portlet-mode>view</portlet-mode>
    </supports>
    <supported-locale>en</supported-locale>
    <resource-bundle>com.ibm.sampleportlet.nl.SamplePortletResource</resource-bundle>
    <portlet-info>
    <title>SourcePortlet</title>
    <short-title>SourcePortlet</short-title>
    <keywords>SourcePortlet</keywords>
    </portlet-info>
    <supported-publishing-event>
    <name>SampleEvent</name>
    </supported-publishing-event>
    </portlet>



  • Add your target portlet to add <supported-processing-event> element like this.

    <portlet>
    <portlet-name>TargetPortlet</portlet-name>
    <display-name xml:lang="en">TargetPortlet</display-name>
    <display-name>TargetPortlet</display-name>
    <portlet-class>com.ibm.sampleportlet.TargetPortlet</portlet-class>
    <init-param>
    <name>wps.markup</name>
    <value>html</value>
    </init-param>
    <expiration-cache>0</expiration-cache>
    <supports>
    <mime-type>text/html</mime-type>
    <portlet-mode>view</portlet-mode>
    </supports>
    <supported-locale>en</supported-locale>
    <resource-bundle>com.ibm.sampleportlet.nl.TargetPortletResource</resource-bundle>
    <portlet-info>
    <title>TargetPortlet</title>
    <short-title>TargetPortlet</short-title>
    <keywords>TargetPortlet</keywords>
    </portlet-info>
    <supported-processing-event>
    <name>SampleEvent</name>
    </supported-processing-event>
    </portlet>



  • Source portlet can publish event either during Action or Event phase. We will support publishing of event in the Action phase by implementing processAction() method like this

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
    System.out.println("Inside SamplePortlet.processAction. Sending Event");
    java.lang.String sampleObject = new java.lang.String();
    response.setEvent("SampleEvent", "EventFromSourcePortlet- processAction");
    }


  • Whenever a event is published the portlet container will pass control to processEvent() method of the target portlet.

    public void processEvent(EventRequest request,
    EventResponse response) throws
    PortletException, java.io.IOException {
    System.out.println("Inside TargetPortelt.sampleEvent");
    Event sampleEvent = request.getEvent();
    System.out.println("Event Name " +sampleEvent.getName().toString());
    if(sampleEvent.getName().toString().equals("SampleEvent")) {
    Object sampleProcessObject = sampleEvent.getValue();
    request.getPortletSession().setAttribute("sampleEvent", sampleEvent.getName());
    }
    }

    The Event object contains both name of the event as well as the pay load. Use it to find out more information about the event.


You can download the sample code from here

No comments: