Monday, June 7, 2010

Event Handling in JSR - 286

As we all know about event handing. We perform some action on a source which is targeted to other source. When we talk about in an application, this task is very easy for us since we have certain methods through which we can pass data from one source to another source. In other way, you can pass your data from one source to another source of same application. Along with we talk about different applications (different WAR’s) which are running on same container. Can we pass data from one application to another application?

Yes, this is possible in JSR-286 where we can pass the data from one application to another application. In this process, we perform an action from one application which passes the data to another application.

There are some steps which describe how we can do it in Portlet applications. The steps are,

1) Let us take two portlet applications. Sender portlet application generates the event and receiver portlet application receives the event data from sender application.

2) Firstly, you need to add some configuration on portlet.xml file. What you need to do is you have to add event-definition information in sender application's portlet.xml file and receiver application's portlet.xml file. This is as,

<event-definition>
<qname xmlns:x="http://ndilip.com/events">x:ndilipevent</qname>
<value-type>java.lang.String</value-type>
</event-definition>

3) Now, you need to add 'supported-publishing-event' tag information under portlet tag of portlet.xml file for sender application. This is as,

<supported-publishing-event>
<qname xmlns:x="http://ndilip.com/events">x:ndilipevent</qname>
</supported-publishing-event>

These are the steps which are required in sender portlet application.

4) Now, you need to know, how to create event from sender application. The codes are,

QName qName = new QName("http://ndilip.com/events", "ndilipevent");
response.setEvent(qName, paramStr);

You have to add this code at processAction method.

5) Now you need to understand how you can handle this event on receiver portlet application.

6) You need to add 'supported-processing-event' tag information under portlet tag of portlet.xml file for receiver application. This is as,

<supported-processing-event>
<qname xmlns:x="http://ndilip.com/events">x:ndilipevent</qname>
</supported-processing-event>

7) Now, you need to know how you can utilize the event which is created by sender portlet application. You have to add processEvent method which handles the event created by sender. The codes are,

public void processEvent(EventRequest request, EventResponse response) {
Event event = request.getEvent();
String value = (String) event.getValue();
String valueString=value.toString();
}

valueString is the event string value comes from sender application.

3 comments:

andy said...

Good Explanation.

One small note: In IBM WebSphere Portal, You must create Page wires also apart from other steps.

chetan kapoor said...

Hi did the same didn't worked for me even other examples too didn't worked i am using IBM websphere portal can u tell me how to create wires?

chetankpr@gmail.com

dilip gupta said...

Hi Chetan, thanks for your comment. I wrote a general solution in this post. In case of IBM, you can come across the link below,

http://www.ibm.com/developerworks/websphere/library/techarticles/0809_hepper/0809_hepper.html

This link helps you to create wires on IBM.

Please let me know in case you need more help on this.