Friday, July 24, 2009

Portlet IPC using JBOSS Portal


Portlet 2.0 is the recently finalized latest version of the Java portlet
specification. One of the shortcomings of the previous version of the spec
(JSR168)was lack of support for inter-portlet communication.As a result,
developers had to resort to all kinds of fancy workarounds using Dojo Toolkit

I am not going to explain what is JSR286 , but try to
achieve IPC for JBOSS Portal

Defining events

To define an event, you would have to declare it in
portlet.xml, as shown below

<event-definition>
<qname
xmlns:mycomp='http://www.something.com/jsr286'>
something:AppEvent</qname>
<value-type>com.ceon.ocp.portlet.WrapperEvent</value-type>
</event-definition>

Publishing an Event

<portlet>
....
<portlet-name>pubEventPortlet</portlet-name>
<supported-publishing-event>
<qname
xmlns:something='http://www.something.com/jsr286'>
something:AppEvent</qname>
</supported-publishing-event>
....
</portlet>

Processing an Event

<portlet>
....
<portlet-name>subEventPortlet</portlet-name>
<supported-processing-event>
<qname xmlns:something='http://www.something.com/jsr286'>
something:AppEvent</qname>
</supported-processing-event>
....
</portlet>

Each portlet definition can have both publishing and processing tags
and you could define multiple events within each portlet definition

Coding events

An Example event class

@XmlRootElement
public class WrapperEvent implements Serializable {
...
public static final QName QNAME = new QName(
"http://www.something.com/jsr286", "AppEvent");
private String name;
private String zipcode;
...
}

An event class must implement the Serializable interface and be annotated with
@XmlRootElement, I am using Java 5,so I used jaxb-api-2.1.jar from the JBoss portlet
container 2.0 lib directory

subscriber ( receiving portlet )

@Override
protected void doView(RenderRequest request, RenderResponse renderResponse)
throws PortletException, IOException {
renderResponse.setContentType(request.getResponseContentType());
// setting the attributes for JSP
request.setAttribute(QUOTE_ATTR, quoteId);
getPortletContext().getRequestDispatcher(VIEW_JSP).include(request,renderResponse);
}
@Override
public void processEvent(EventRequest eventRequest, EventResponse response)
throws PortletException, IOException {
Event event = eventRequest.getEvent();
if(event.getQName().equals(WrapperEvent.QNAME))
{
com.ceon.ocp.portlet.WrapperEvent
wrapEvent=(com.ceon.ocp.portlet.WrapperEvent)event.getValue();
quoteId = wrapEvent.getQuoteID();

}
}

Publisher( trigger event portlet)

@Override
protected void doView(RenderRequest request, RenderResponse renderResponse)
throws PortletException, IOException {
renderResponse.setContentType(request.getResponseContentType());
getPortletContext().getRequestDispatcher(VIEW_JSP)
.include(request,renderResponse);
}

@Override
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
String quoteParam = request.getParameter("QuoteID");
if ( quoteParam == null || quoteParam.trim().equals("")) {
quoteParam = "No Quote ID Specified";
}
com.ceon.ocp.portlet.WrapperEvent event
= new com.ceon.ocp.portlet.WrapperEvent();
event.setQuoteID(quoteParam);
response.setEvent(com.ceon.ocp.portlet.WrapperEvent.QNAME, event);
}

And simple JSPs for both subscriber and publisher.

reciever.jsp

<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
Here are the details of Quote State:
QuoteState: <%= renderRequest.getAttribute("QuoteID")%>

publisher.jsp

<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
Please enter Quote ID and click submit,you should
see the Quote State in OCP Portlet

<form method='post' action="<portlet:actionURL />" >
QuoteID: <input type=text name=QuoteID>
<input type=submit>
</form>

One final step is go to

After you have installed and configured the container,look at its core Web
application, called simple-portal.This Web application holds the framework that
displays the portlets. You could either add your own page by modifying
layouts/nav/main.jsp, or modify one of the existing pages. I modified one of the
existing pages, demo/demo.jsp, by removing its portlets and adding my own custom
portlets. The JBoss portlet container uses a very simple tag library to include
portlets, which requires the portlet name as defined in portlet.xml and the
context name of the portlet Web application

<xportal:portlet name="pubEventPortlet" applicationName="JbossSample"/>
<xportal:portlet name="subEventPortlet" applicationName="JbossSample"/>

As you can see, it's quite easy for a portlet to receive events from another portlet,
whether the two portlets reside in the same or separate Web applications.
You just have to make sure to include the common event classes in all portlet Web
applications.If you would like to deploy this application to an other Portlet 2.0
container, make sure to check that container's requirements for web.xml,as some
containers expect specific elements in the web.xml files of Portlet 2.0 applications

No comments:

Post a Comment

Create ElasticSearch cluster on single machine

I wanted to figure out how to create a multi-node ElasticSearch cluster on single machine. So i followed these instructions First i did...