We have requirement to communicate between two portal one solution is interproccess communication with help of the event.
Today we will develop inter proccess communication portlets example.
You can find source code for sender portlet and receiver portlet below link.
1 Create portlet Sender.
2 Create jsp inside Folder docroot -> html -> sender -> view.jsp
put below content inside it.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@page import="javax.portlet.PortletSession"%>
<portlet:actionURL name="addform" var="updateURL">
</portlet:actionURL >
<aui:form action="<%=updateURL%>" method="post" name="fm" >
<h1>IPC: Sender portlet</h1>
<aui:input label="User Name" name="username" type="text"/><img title="Please Enter the User Name" style="margin-left:74px;position:relative;top:-43px" src="/html/themes/control_panel/images/portlet/help.png"/>
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:form>
3 Create class SenderPortletAction in package com.liferay.portlet. Put below content inside it.
This action useful to invoke event and pass parameter inside it.
package com.liferay.portlet;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.xml.namespace.QName;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class SenderPortletAction extends MVCPortlet {
public void addform(ActionRequest actionRequest, ActionResponse actionResponse){
QName qName = new QName("http://liferay.com", "userDetails", "x");
String username=ParamUtil.getString(actionRequest, "username");
actionResponse.setEvent(qName, username);
}
}
4 Add below content inside portlet.xml. Content marked with yellow provide capability to publish event.
<?xml version="1.0"?>
<portlet-app
version="2.0"
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
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>sender</portlet-name>
<display-name>Sender Portlet</display-name>
<portlet-class>com.liferay.portlet.SenderPortletAction</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/sender/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>Sender Portlet</title>
<short-title>Sender Portlet</short-title>
<keywords>Sender Portlet</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-publishing-event>
<qname xmlns:x="http://liferay.com">x:userDetails</qname>
</supported-publishing-event>
</portlet>
</portlet-app>
5 Create new portlet receiver
6 Create jsp inside Folder docroot -> html -> receiver -> view.jsp
put below content inside it.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@page import="javax.portlet.PortletSession"%>
<%
String username= renderRequest.getParameter("USERNAME");
%>
<h1>IPC: Receiver portlet</h1>
The UserName is: <%=username %>
7 Create ReceiverPortletAction class inside com.liferay.portlet package
put below content inside it. Content marked with yellow where event received.
package com.liferay.portlet;
import java.io.IOException;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class ReceiverPortletAction extends MVCPortlet {
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {
Event event = request.getEvent();
response.setRenderParameter("USERNAME", (String) event.getValue());
}
}
8 Add below content inside the portlet.xml of Receiver portlet. Content marked with yellow provide capability to receive particular event in action.
<?xml version="1.0"?>
<portlet-app
version="2.0"
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
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>receiver</portlet-name>
<display-name>Receiver</display-name>
<portlet-class>com.liferay.portlet.ReceiverPortletAction</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/receiver/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>Receiver</title>
<short-title>Receiver</short-title>
<keywords>Receiver</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-processing-event xmlns:x="http://liferay.com">
<qname>x:userDetails</qname>
</supported-processing-event>
</portlet>
</portlet-app>
9 Build Sender portlet.
10 Build Receiver portlet
11 Add sender portlet inside page.
12 Add receiver portlet inside page.
Comments
Post a Comment