google.com, pub-5747754088801811, DIRECT, f08c47fec0942fa0 Skip to main content

Interproccess communication liferay

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

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

Popular posts from this blog

Disable cache content of browser access on back button after logout liferay dxp

Some time we have requirement where we do not want to allow back button after logout and show cached browser content. Liferay provide properties which with we are able to restrict to see content on back button after logout. If user click back button after logout it will show the login page. We require to provide below properties in portal-ext.properties. Restart the server once applying below properties browser.cache.signed.in.disabled=true

Liferay portlet with alloy Ui form validator example.

 Alloy Ui  is built on YUI and we can use it in our liferay portlets.  Yui provides many component which are easy to use in our portlets  Here we see basic form validator.example with liferay portlet. Create portlet AlloyUi-portlet in eclipse. Add below content in view.jsp file Here we create two link for two JSP. <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <script src="http://cdn.alloyui.com/2.5.0/aui/aui-min.js"></script> <link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet"></link> <portlet:defineObjects /> <portlet:renderURL var="basicform"> <portlet:param name="mvcPath" value="/BasicFormValidatorExample.jsp"/> </portlet:renderURL> <portlet:renderURL var="fullform"> <portlet:param name="mvcPath" value="/FullFormValidatorExample.jsp"/> </portlet:re...