Showing posts with label blazeds. Show all posts
Showing posts with label blazeds. Show all posts

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;
    }
    }
    }


What is blazeDS

BlazeDS is a open source project, that provides some infrastructure that can be used to simplify development of J2EE backend and Flex for front end type of application

BlazeDS functionality can be divided into following three key services:

  1. The Remoting Service allows your Flex application to directly invoke methods of Java objects deployed in your application server.

  2. The Message Service provides a publish/subscribe infrastructure that allows your Flex application to publish messages and subscribe to a messaging destination, enabling the development of real-time data push and collaborative applications.

  3. The Proxy Service allows your Flex application to make cross-domain service requests in a secure and controlled manner. In other words, it allows your Flex application to access a service available on a different domain than the domain from where the application was downloaded (without having to deploy a crossdomain.xml policy file on the target domain).




If you want to build J2EE on server side and Flex for front end type of application, then you can either use JSON, XML or Flex for data exchange but using BlazeDS provides following advantages


  1. It takes care of converting Java object into Action Script object required by Flex and other way round.

  2. It is more efficient then either JSON or XML for communicating between J2EE and Flex application