Hello Portlet Events

The Portlet Specification 2.0 has concept of Events, that can be used for portlet to portlet communication. I built this sample HelloEventsPortlet to demonstrate how to create simple portlet events communication

The HelloEventsPortlet application has two portlets SourcePortlet, which will display Action URL to the user in VIEW mode and when user clicks on the ActionURL it will set hello event in the processAction method, the TargetPortlet will act as consumer for the event and in the processEvent method it will read value of the event and set it as render parameter and display that value to the user in VIEW mode markup. Follow these steps to create events portlet


  • First create SourcePortlet.java like this


    package com.webspherenotes.portlet.events;

    import java.io.IOException;

    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.GenericPortlet;
    import javax.portlet.PortletException;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;

    public class SourcePortlet extends GenericPortlet{

    protected void doView(RenderRequest request, RenderResponse response)
    throws PortletException, IOException {
    System.out.println("Entering SourcePortlet.doView()");
    response.setContentType("text/html");
    getPortletContext().getRequestDispatcher("/source.jsp").include(request, response);
    System.out.println("Exiting SourcePortlet.doView()");
    }

    public void processAction(ActionRequest request, ActionResponse response)
    throws PortletException, IOException {
    System.out.println("Entering SourcePortlet.processAction()");
    response.setEvent("hello", "Hello event from the SourcePortlet");
    System.out.println("Exiting SourcePortlet.processAction()");
    }

    }

    The SourcePortlet has doView() and processAction() methods, in doView() method it is forwarding control to source.jsp for generating markup, the source.jsp generates a simple ActionURL.
    In the processAction() method it is calling response.setEvent("hello", "Hello event from the SourcePortlet"), which is used for publishing hello event.


  • This is how my TargetPortlet.java looks like

    package com.webspherenotes.portlet.events;

    import java.io.IOException;

    import javax.portlet.Event;
    import javax.portlet.EventPortlet;
    import javax.portlet.EventRequest;
    import javax.portlet.EventResponse;
    import javax.portlet.GenericPortlet;
    import javax.portlet.PortletException;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;

    public class TargetPortlet extends GenericPortlet implements EventPortlet{

    protected void doView(RenderRequest request, RenderResponse response)
    throws PortletException, IOException {
    System.out.println("Entering TargetPortlet.doView()");
    response.setContentType("text/html");
    response.getWriter().println("Hello from TargetPortlet.doView() " + request.getParameter("helloEvent"));
    System.out.println("Exiting TargetPortlet.doView()");
    }

    public void processEvent(EventRequest request, EventResponse response)
    throws PortletException, IOException {
    System.out.println("Entering TargetPortlet.processEvent");
    Event event= request.getEvent();
    System.out.println("Event Name " + event.getName());
    String eventValue =(String)event.getValue();
    System.out.println("Event Value " + eventValue);
    response.setRenderParameter("helloEvent", eventValue);

    System.out.println("Exiting TargetPortlet.processEvent");
    }

    }

    The TargetPortlet is implementing EventPortlet to indicate that it can act as consumer of event. The TargetPortlet has doView() and processEvent() methods, in doView() it is simply reading value of helloEvent render parameter and displaying it to user.

    The portlet container will call processEvent() method to give TargetPortlet chance to consume the event, it will get called before the render phase. You can call EventRequest.getEvent()method inside the processEvent() method to get the Event object and once you have the event object you can call its getName() and getValue() methods to read name and value of the event. After reading the value of event i am setting it as render parameter for the portlet.


  • This is how my portlet.xml file looks like

    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    version="2.0" 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">
    <portlet>
    <portlet-name>SourcePortlet</portlet-name>
    <display-name>Events Source Portlet</display-name>
    <portlet-class>com.webspherenotes.portlet.events.SourcePortlet</portlet-class>
    <expiration-cache>0</expiration-cache>
    <supports>
    <mime-type>text/html</mime-type>
    <portlet-mode>view</portlet-mode>
    <portlet-mode>edit</portlet-mode>
    </supports>
    <portlet-info>
    <title>Event Source Portlet</title>
    <short-title>Event Source Portlet</short-title>
    <keywords>Event Source Portlet</keywords>
    </portlet-info>
    <supported-publishing-event>
    <name>hello</name>
    </supported-publishing-event>

    </portlet>
    <portlet>
    <portlet-name>TargetPortlet</portlet-name>
    <display-name>Events Target Portlet</display-name>
    <portlet-class>com.webspherenotes.portlet.events.TargetPortlet</portlet-class>
    <expiration-cache>0</expiration-cache>
    <supports>
    <mime-type>text/html</mime-type>
    <portlet-mode>view</portlet-mode>
    <portlet-mode>edit</portlet-mode>
    </supports>
    <portlet-info>
    <title>Event Target Portlet</title>
    <short-title>Event Target Portlet</short-title>
    <keywords>Event Target Portlet</keywords>
    </portlet-info>
    <supported-processing-event>
    <name>hello</name>
    </supported-processing-event>

    </portlet>
    <default-namespace>http://wpcertification.blogspot.com</default-namespace>
    <event-definition>
    <name>hello</name>
    <value-type>java.lang.String</value-type>
    </event-definition>

    </portlet-app>

    There are three components in the portlet.xml

    1. event-definition: Element is declared at the portlet application level and it defines name of the event and the type of the event. In my case name of the event is hello and type is java.lang.Style

    2. supported-publishing-event: Element is used for declaring what all events can be published by this portlet

    3. supported-processing-event: Element is used for defining what all events can be consumed by this portlet



  • After developing this portlet deploy it in WebSphere Portal server and add them to a portal page

  • Now create a wire between consumer and producer using Portlet Wiring Portlet like this




Now you can test this portlet by clicking on the Action URL in the Source Portlet, you will notice that the TargetPortlet is displaying the value of event to user

1 comment:

Anonymous said...

Hi , can we call portal events from Web application