create IPC portlet in Liferay 6.1.1 ga2. Liferay development is not at all an simpley task as it requires a expetise with vast experience. Attune Infocom is one of the fastest growing Liferay development firms, which has an extremely talented pool of liferay developers who can develop custom liferay portals for you.
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Create IPC Portlet in Liferay 6.1.1 ga2
1. Create IPC Portlet in Liferay 6.1.1 ga2
The first version of the portlet specification, JSR-168/portlet1.0, did not include any
support for Inter Portlet Communication. The second version, JSR-286/ portlet2.0,
which is supported for IPC Mechanism.
IPC is made easy with JSR-286 to share the data between two portlets. Using IPC
mechanisms, we can share the data from ACTION to VIEW phase and VIEW-VIEW
Phase.
Public Render Parameter: IPC (Inter Portlet Communication):
In JSR 168, the render parameters set in processAction is only available in the render of the
same portlet. With the Public Render Parameters feature, the render parameters set in the
processAction of one portlet will be available in render of other portlets also.
By adding the following property in portlet-ext, we can enable portlets to share render
states with other portlets that are on different pages:
portlet.public.render.parameter.distribution=ALL_PORTLETS
Follow Steps to create IPC Portlet in Liferay 6.1.1 ga2
Step 1: Create new demoipc-portlet
Give first portlet name:sender
Step 2: Create second portlet in same project
Give portlet name:receiver
Step 3: In package com.test make two java class file
Sender.java
2. package com.test;
import java.io.IOException;
import java.util.Random;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.xml.namespace.QName;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class sender
*/
public class sender extends MVCPortlet {
@ProcessAction(name="pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) {
String pitchType = null;
System.out.println("pitch call");
// Send an Event that the ball has been pitched.
Random random = new Random(System.currentTimeMillis());
int pitch = random.nextInt(3) + 1;
switch (pitch) {
case 1:
pitchType = "Fast Ball";
break;
case 2:
pitchType = "Curve Ball";
break;
case 3:
pitchType = "Slider";
break;
// should never print
default:
pitchType = "Screw Ball";
}
pitchType=pitchType+"Hai You have received Event Data sent from Sender Portlet";
javax.xml.namespace.QName qName =
new QName("http://liferay.com", "ipc.pitch", "x");
response.setEvent(
qName,
pitchType);
System.out.println("call here");
3. }
}
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 {
@javax.portlet.ProcessEvent(qname = "{http://liferay.com}ipc.pitch")
public void handleProcessempinfoEvent(
javax.portlet.EventRequest request, javax.portlet.EventResponse
response)
throws javax.portlet.PortletException, java.io.IOException {
javax.portlet.Event event = request.getEvent();
String value = (String) event.getValue();
System.out.print("value in process event>>>>>>>>>" + value);
response.setRenderParameter("ipc.pitch", value);
}
}
Step 4: Edit the portlet.xml
-Add below attribute in “Sender-Portlet”
-Before closing of </portlet>
<supported-processing-event xmlns:x='http://liferay.com'>
<qname>x:ipc.pitch</qname>
</supported-processing-event>
</portlet>
<event-definition xmlns:x='http://liferay.com'>
<qname>x:ipc.pitch</qname>
<value-type>java.lang.String</value-type>
</event-definition>
</portlet-app>
Note: We can declare a list of public paramters for a portlet application.
-Add below attribute in “recevier-Portlet”
4. -Before closing of </portlet>
<supported-processing-event xmlns:x='http://liferay.com'>
<qname>x:ipc.pitch</qname>
</supported-processing-event>
</portlet>
<event-definition xmlns:x='http://liferay.com'>
<qname>x:ipc.pitch</qname>
<value-type>java.lang.String</value-type>
</event-definition>
</portlet-app>
In my example I have created both portlet in same project so here I have add
both changes in same portlet.xml file
Portlet.xml
<?xml version="1.0"?>
<portlet-app 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" version="2.0">
<portlet>
<portlet-name>sender</portlet-name>
<display-name>sender</display-name>
<portlet-class>com.test.sender</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>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>sender</title>
<short-title>sender</short-title>
<keywords></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>
6. Step 5: A portlet can read public render parameter using following
method
request.getPublicParameterMap()
Note: Public render parameters are merged with regular parameters so can also be
read using
request.getParameter(“id1”);
Step 6: A portlet can remove a public render parameter by invoking
following methods
response.removePublicRenderParameter(“id1”)
Step 7: Make view.jsp for sender and receiver
Make view.jsp in html/sender/view.jsp
View.jsp
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />
vbjhbnj
<p>Click the link below to pitch the ball. </p>
<a href="<portlet:actionURL name="pitchBall"></portlet:actionURL>">Pitch!</a>
Make view.jsp in html/recevier/view.jsp
View.jsp
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />
<%
String pitch = (String)renderRequest.getParameter("ipc.pitch");
%>
<p>And the pitch is....</p>
<p>
<% if (pitch!=null) { %>
<%=pitch %>!
<% } else { %>
... waiting for pitch.
<% } %>
Step 8: now deploy it and check it
Liferay development is not at all an simpley task as it requires a expetise with vast
experience. Attune Infocom is one of the fastest growing Liferay development firms,
which has an extremely talented pool of liferay developers who can develop custom
liferay portals for you. Attuene Infocom offer outsource Liferay Portal development
solution at worldwide.