BlazeDS Contact Portlet

I wanted to learn how to i can use BlazeDS technology in portlet so i built a sample Contact Management application, This application has BlazeDSContactPortlet, which is used for returning a JSP that includes Flex from doView() method but once the Flex application is loaded it will directly communicate to MessageBrokerServlet inside the portlet, which is disadvantage of using BlazeDS in portlet application, you wont have portlet context in any other calls



You can download the sample code for this article from here


The BlazeDS application is nothing but a Servlet that you can add to your portlet application along with set of jars and you should be able to use it for communicating to Flex

  1. First download the blazeds.war and copy all the jars from its WEB-INF/lib to your WEB-INF/lib folder
  2. Then add HttpFlexSession listener and MessageBrokerServlet servlet to your web.xml like this

    <listener>
    <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>
    <servlet>
    <servlet-name>MessageBrokerServlet</servlet-name>
    <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
    <init-param>
    <param-name>services.configuration.file</param-name>
    <param-value>/WEB-INF/flex/services-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>MessageBrokerServlet</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>


  3. The copy flex folder from the blazeds.war to your WEB-INF folder and change services-config.xml file so that value of my-amf channel points to the MessageBrokerServlet in your environment.

    <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
    <endpoint url="http://localhost:10040/wpcert/PA_BlazeDSContact/messagebroker/amf"
    class="flex.messaging.endpoints.AMFEndpoint"/>
    </channel-definition>

    The value of url will change based on your environment, in my case base URL of the server is http://localhost:10040 and the web application that has the MessageBrokerServlet is available at http://localhost:10040/wpcert/PA_BlazeDSContact, so i add /messagebroker/amf to it and that becomes value of my-amf channel url

  4. I am more interested in using BlazeDS to make calls to Java Objects in my portlet application from my Flex code, so first i declare the Java Class that should be invoked from my Flex class by adding this code to my remoting-config.xml file like this

    <destination id="contactdao">
    <properties>
    <source>com.webspherenotes.flex.dao.ContactDAOImpl</source>
    </properties>
    </destination>

    Now i can call methods of com.webspherenotes.flex.dao.ContactDAOImpl Java Class directly from my Flex code

  5. Now inside my flex code i can add this code to call methods of the ContactDAOImpl

    [Bindable]
    public var contactList:ArrayCollection;

    public var contactDAORO:RemoteObject;
    public function getContactList():void {
    contactDAORO = new RemoteObject();
    contactDAORO.destination = "contactdao";
    contactDAORO.endpoint ="http://localhost:10040/wpcert/PA_BlazeDSContact/messagebroker/amf"
    contactDAORO.addEventListener("result",getContactListHandler);
    contactDAORO.addEventListener("fault",faultHandler);
    contactDAORO.getContactList();
    }
    public function faultHandler (event:FaultEvent):void {
    Alert.show(event.fault.faultString, 'Error');
    }
    private function getContactListHandler(event:ResultEvent):void{
    contactList = event.result as ArrayCollection;
    dgEmployees.dataProvider = contactList;
    }


    <mx:DataGrid x="10" y="174" width="460" enabled="true" editable="false"
    id="dgEmployees">
    <mx:columns>
    <mx:DataGridColumn headerText="First Name" dataField="firstName"/>
    <mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
    <mx:DataGridColumn headerText="Email" dataField="email"/>
    </mx:columns>
    </mx:DataGrid>

    My Flex code has dgEmployees as DataGrid , that displays list of contacts in spread sheet format.

    I am calling getContactList(), flex method on the application load, this method is creating object of RemoteObject and pointing to URL where MessageBrokerServlet in my application is listening, then i am setting value of contactDAORO.destination to contactdao, when i do that MessageBrokerServlet, will take value of destination and find out the java class from that is mapped to contactdao by reading remoting-config.xml, in our case it is pointing to com.webspherenotes.flex.dao.ContactDAOImpl, then i am setting getContactListHandler, as flex method which should be used to handle result of this call and faultHandler method for handling error cases in this flex call.

    The contactDAORO.getContactList() means i want to call the getContactList() method of ContactDAOImpl class, this method returns ArrayList of Contact objects, on the flex side i am converting that ArrayList to ArrayCollection and i have a Contact flex class that is mapped to the Contact java class. Inside the getContactList() method i am setting the list returned by Java code as dataprovider for the datagrid

  6. This is how my Action Script Contact class looks like

    package com.webspherenotes.flex
    {
    [Bindable]
    [RemoteClass(alias="com.webspherenotes.flex.dao.Contact")]
    public class Contact
    {
    private var _firstName:String;
    private var _lastName:String;
    private var _email:String;
    public function Contact()
    {
    }
    public function get email():String
    {
    return _email;
    }
    public function set email(value:String):void
    {
    _email = value;
    }
    public function get lastName():String
    {
    return _lastName;
    }
    public function set lastName(value:String):void
    {
    _lastName = value;
    }
    public function get firstName():String
    {
    return _firstName;
    }
    public function set firstName(value:String):void
    {
    _firstName = value;
    }
    }
    }