Site icon Pro Liferay

Inter Portlet Communication(IPC) by Event

IPC

Event based IPC was introduced in JSR 286. According to JSR 286 a portlet can generate event and another portlet can receive the event. This article explains the event based IPC between two portlets. Two portlets are downloadable at the end of this article. One portlet is responsible for event generation and another portlet is responsible for receiving the event. Both the portlets are in different WARs.


 Note1:

portlet.xml is the file where events are defined. A portlet can define more events. Similarly it can receive many events. By inspecting portlet.xml we can find out whether a portlet produces an event or it receives an event. 

Note 2:

In this article there are two portlet. The first portlet is ipc-event-generator which generates event and another portlet is ipc-event-receiver which receive the event.


 

Configure Event Definition (Producer):

We must have to tell a portlet that it should act as an event producer. Add the below lines in your portlet.xml after end of security-role-ref tag.

<supported-publishing-event>
    <qname xmlns:x="https://proliferay.com/events">x:ipc-text</qname>
</supported-publishing-event>

 

Note: Download and check portlet.xml in ipc-event-generator portlet.

Configure Event Definition (Receiver):

We must have to tell a portlet that it should act as an event receiver. Add the below lines in your portlet.xml after end of security-role-ref tag.

<supported-processing-event>
    <qname xmlns:x="https://proliferay.com/events">x:ipc-text</qname>
</supported-processing-event>
Note: Download and check portlet.xml in ipc-event-receiver portlet

Whats QName?

A QName is a qualified name. Using a QName allows events to be name-spaced properly so that no two event names are identical. This definition declares an event called x:ipc-text within the namespace https://proliferay.com/events. You also declare that your payload is a String object.

Keep in mind!!
supported-publishing-event tag is used for producing event and supported-processing-event tag is used for receiving the event.

ipc-event-generator portlet code:
view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />

<portlet:actionURL var="actionUrl" name="processEvent"/>

<aui:form method="post" action="<%=actionUrl.toString() %>">

<aui:input name="sampleText" label="Sample Text"/>

<aui:button-row>
    <aui:button type="submit" value="Send"/>
</aui:button-row>

</aui:form>

EventGeneratorPortlet.java:

package com.proliferay.demo;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;
import javax.xml.namespace.QName;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 *
 * @author Hamidul Islam
 *
 */
public class EventGeneratorPortlet extends MVCPortlet{
    
    @ProcessAction(name="processEvent")
    public void process(ActionRequest request, ActionResponse response) {
        
        /**
         * Get sample text from UI
         */
        
        String sampleText = ParamUtil.getString(request, "sampleText","");
        
        /**
         * Refer portlet.xml
         */
        QName qName = new QName("https://proliferay.com/events", "ipc-text");
        response.setEvent(qName, sampleText);
    }

}

Note1: Notice that event is triggering from process action

Note2: In event generator portlet there is a input box. This value can be sent to another portlet i.e., receiver.
ipc-event-receiver portlet code:
view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />

<%
    String sampleText = (String) renderRequest.getParameter("sampleText");
%>

<aui:form>
    <aui:input name="receivedText" label="Received Text" readOnly="true" value="<%=sampleText %>"/>
</aui:form>

EventReceiverPortlet.java:

package com.proliferay.demo;

import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.ProcessEvent;

import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 *
 * @author Hamidul Islam
 *
 */
public class EventReceiverPortlet extends MVCPortlet {

    @ProcessEvent(qname = "{https://proliferay.com/events}ipc-text")
    public void myEvent(EventRequest request, EventResponse response) {
        Event event = request.getEvent();
        //Get data from the event
        String sampleText = (String) event.getValue();
        //Set the text in response to display in UI
        response.setRenderParameter("sampleText", sampleText);
    }

}

Good to know!!!

 

ProcessEvent is additional portlet life cycle method which is added in Portlet 2.0 Specification. This method is not available in Portlet 1.0 Specification. This method is used for processing the event in receiving portlet.


Sample Output:

Download Code
Exit mobile version