Event based IPC (Inter portlet communication using Event)
Create one liferay plugin project with any name as per your requirement naming convention, in my case i have given
InterportletCommunication after this create two portlet, in my case i have given name as follows sender and receiver.
1> Define Event definition in portlet.xml before </portlet-app> tag ends.
<event-definition xmlns:x='http://liferay.com'>
<qname>x:sharingValues</qname>
<value-type>java.lang.String</value-type>
</event-definition>
*qname represents qualified name and in value-type tag we are giving what type of object it is, in this case it is String object.
2> Define supported publishing event in portlet.xml for sender portlet before </portlet>
<supported-publishing-event xmlns:x='http://liferay.com'>
<qname>x:sharingValues</qname>
</supported-publishing-event>
3> Define supported processing event in portlet.xml for receiver portlet before </portlet>
<supported-processing-event xmlns:x='http://liferay.com'>
<qname>x:sharingValues</qname>
</supported-processing-event>
4> Paste below code in sender portlet view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<p style="color: green; border: 1px solid green;">This is Sender portlet, this will send/share parameter value with Receiver Portlet.</p>
<portlet:actionURL var="publishEventUrl" name="publishEvent"/>
<aui:form method="post" action="<%=publishEventUrl.toString() %>">
<aui:input name="publishText" value="" label="Publish Text"/>
<aui:button type="submit" value="Send"/>
</aui:form>
5> paste below code in sender portlet java class Sender.java
package com.test;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.xml.namespace.QName;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class Sender
*/
public class Sender extends MVCPortlet {
public void publishEvent(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String publishText = ParamUtil.getString(actionRequest, "publishText");
System.out.println("value got from sender jsp:::"+publishText);
//NOTE: creating qname object please keep in my mind to import "import javax.xml.namespace.QName;"
QName qName = new QName("http://liferay.com", "sharingValues");
actionResponse.setEvent(qName, publishText);
}
}
6> paste below code in receiver portlet java class Receiver.java
package com.test;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.ProcessEvent;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class Receiver
*/
public class Receiver extends MVCPortlet {
@ProcessEvent(qname = "{http://liferay.com}sharingValues")
public void myEvent(EventRequest request, EventResponse response) {
//NOTE: Creating Event object please keep in mind importing "javax.portlet.Event;"
Event event = request.getEvent();
//Get data from the event
String valueReceived = (String) event.getValue();
System.out.println("value got in receiver portlet:::"+valueReceived);
response.setRenderParameter("sharingValues", valueReceived);
}
}
7>Paste below code in receiver portlet view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<portlet:defineObjects />
<p style="color: green; border: 1px solid green;">This is Receiver Portlet, this will receive parameter value send/share by sender portlet.</p>
<%
String finalValueReceived = (String) renderRequest.getParameter("sharingValues");
%>
<p style="color:green;">Value received from sender portlet:</p><%=finalValueReceived%>