Friday, October 16, 2015

Inter Portlet Communication (IPC) using Events in Liferay


We already know IPC using public render parameters  .Today we will see how to do IPC using Events. Events based IPC was introduced in JSR 286 or we can say in Portlet 2.0. According to this a new method processEvent introduced in Life Cycle of Portlet.Here one Portlet can Produce a event which can be consumed by other portlets.

We take a simple scenario where user can select food type from one portlet and second portlet shows food items on the basis of first portlet. This is a Sender, Receiver kind of scenario but for events there may be one sender and multiple receivers.Here we create two project and each project contain one portlet.

So lets start this step by step:-


For Sender

Step 1:-Create Liferay Project and Portlet
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as event_sender and click finish. After that Right Click on your project ie event_sender and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.

Note:-You can check the snapshots in my  blog on Portlet Configuration Page in Liferay.


Step 2:-Change portlet.xml
Open portlet.xml and enter supported publishing event as:-


Explanation:-
Focus on these lines:-

<supported-publishing-event>
                <qname xmlns:food="http://aditya/events">                                                 food:foodType  
               </qname>
</supported-publishing-event>

Here Supported Publishing event tell that this portlet can generate event with name foodType. The Qualified name(qname) is used so that parameter name space properly to resolve conflict in case where portlet have two events with same name.



Step 3:-Change view.jsp
Open view.jsp and paste this content:-

Note:- We are not using namespaces, so set required namespace parameter property is false in liferay-portlet.xml.


Step 4:-Change the Controller
Open your Controller and paste this:- 



Explanation:-
Here we fetch the Qname from portlet.xml and produce a event.



For Receiver

Step 1:-Create Liferay Project and Portlet
Similarly as in Sender Create project with name event-receiver and create a portlet in it.


Step 2:-Change portlet.xml
Similarly as in Sender change  portlet.xml .After Security-role-ref tag paste this:-

<supported-processing-event>
     <qname xmlns:food="http://aditya/events">                                                 food:foodType
        </qname>
 </supported-processing-event>

Note:- In Sender we use supported publishing event but in receiver we use supported processing event.


Step 3:-Change the Controller
Open your Controller and paste this:-

Explanation:-
1)By using the annotation @ProcessEvent we can create method with any name otherwise we have to use default processEvent method.
2)Here First we get the event and than value from the event which is set by sender portlet .
3)Finally we set value in request so that it can be get in view.jsp


Step 4:-Change the jsp
Open view.jsp and paste this :-

Explanation:-
Here we get the value that is set in Controller and on the basis of condition show different messages.

Note:- We are using jstl tage here so provide entry in liferay-plugin-package.property file as:-






For Output


Now deploy both sender and receiver portlet and add to same page .



Now Select Vegetarian and click submit



Select Non-Vegetarian and click submit



Note:-Now if you want to add the portlet on different pages . Better you use session or Public render Parameters for IPC because when processEvent method completes in receiver it called doView.
So when portlet is on some other page and you click on navigation bar to change the page only doView of receiver portlet is called and not processEvent.

No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!