Friday, March 28, 2008

BlazeDS for Java-Flex communication

BlazeDS is a server-based Java remoting and web messaging technology that enables communication between back-end Java applications and Adobe Flex applications running in the browser. In this post, I describe a way (may not be the best) I was able to successfully to build a simple application using BlazeDS and Flex. The application is build using eclipse and ant, rather than using FlexBuilder. Following are the main steps that you have to follow to implement the example.
  1. Install tomcat
  2. Install Flex sdk
  3. Download BlazeDS web application.
  4. Create tomcat user with manager permisison
  5. Create dynamic web project in eclipse
  6. Create java file
  7. Create mxml file
  8. Update the config files in the WEB-INF/flex directory of the web application
  9. Copy flexTasks.tasks to the root directory.
  10. Create build file
  11. Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.
  12. Build application using ant
Skip to Sample Code: Moving the mouse over the bolded code parts shows additional information
  1. Install tomcat: This example was implemented on Tomcat 6.0.16
  2. Install Flex sdk: You can download the flex sdk from here The example was implemented on Flex 3.0.0
  3. Download BlazeDS:Download the BlazeDS web application from here
  4. Create tomcat user with manager permisison: In the TOMCAT_HOME/tomcat-users.xml, add the following line
    <role rolename="manager"/>
    <user username="abhi" password="abhi" roles="manager"/>
  5. Create dynamic web project in eclipse by importing the BlazeDS Web application war file.
  6. Create java fileThe java class simply echoes the data input in the textbox shown in the browser.
    package hello;
    public class HelloWorld {

    public String sayHelloTo(String str) {
    System.out.println("Hello " + str);
    return "Hello " + str;
  7. Create mxml fileThe mxml file simply shows a text box and a submit button. A label will be displayed below the textbox with the word "Hello" appended to the input text.
    <?xml version="1.0" encoding="utf-8"?> 
    <mx:Application xmlns:mx="" viewSourceURL="srcview/index.html">


    private var helloResult:String;
    private function sayHelloTo():void {
    private function resultHandler(event:ResultEvent):void

    helloResult = event.result as String;

    <mx:RemoteObject id="ro" destination="helloworld" result="resultHandler(event)" />
    <mx:HBox width="100%">
    <mx:TextInput id="inputText"/>
    <mx:Button label="Submit" click="sayHelloTo()"/>
    <mx:Label text="{helloResult}"/>
    Note: Defining the remote object enables you to make calls to the remote Java classes as if they are local action script classes.
  8. Update the configuration files For this example, you only have to update one config file, WEB-INF/flex/remoting-config.xml, to add a new destination, the HelloWorld class.
    <?xml version="1.0" encoding="UTF-8"?>
    <service id="remoting-service"

    <adapter-definition id="java-object" class="" default="true"/>

    <channel ref="my-amf"/>

    <destination id="helloworld">

  9. Copy flexTasks.tasks to the root directory.
  10. Create build fileThe ant build file uses two flex specific tasks, mxmlc to compile the mxml file and html-wrapper to create a html wrapper that contains the created swf file.
    <project name="helloWorldServer" default="build" basedir=".">

    <!-- ===================== Property Definitions =========================== -->

    <property file="" />
    <property file="${user.home}/" />

    <!-- ==================== File and Directory Names ======================== -->

    <property name="" value="helloWorldServer" />
    <property name="app.path" value="/${}" />
    <property name="app.version" value="0.1-dev" />
    <property name="build.home" value="${basedir}/build" />
    <property name="catalina.home" value="C:/unzipped/apache-tomcat-6.0.16" />
    <property name="dist.home" value="${basedir}/dist" />
    <property name="docs.home" value="${basedir}/docs" />
    <property name="manager.url" value="http://localhost:8080/manager" />
    <property name="src.home" value="${basedir}/src" />
    <property name="web.home" value="${basedir}/WebContent" />
    <property name="manager.username" value="abhi" />
    <property name="manager.password" value="abhi" />
    <property name="FLEX_HOME" value="C:/Adobe/Flex" />
    <property name="APP_ROOT" value="${basedir}/WebContent" />

    <!-- ==================== External Dependencies =========================== -->

    <!-- ==================== Compilation Classpath =========================== -->

    <path id="compile.classpath">
    <fileset dir="${catalina.home}/bin">
    <include name="*.jar" />
    <pathelement location="${catalina.home}/lib" />
    <fileset dir="${catalina.home}/lib">
    <include name="*.jar" />


    <!-- ================== Custom Ant Task Definitions ======================= -->

    <taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="compile.classpath" />
    <taskdef resource="flexTasks.tasks" classpath="c:/ant/lib/flexTasks.jar" />

    <!-- ==================== Compilation Control Options ==================== -->

    <property name="compile.debug" value="true" />
    <property name="compile.deprecation" value="false" />
    <property name="compile.optimize" value="true" />

    <!-- ==================== All Target ====================================== -->

    <target name="all" depends="clean,build" description="Clean build and dist directories, then compile" />

    <!-- ==================== Clean Target ==================================== -->

    <target name="clean" description="Delete old build and dist directories">
    <delete dir="${build.home}" />
    <delete dir="${dist.home}" />

    <!-- ==================== Compile Target ================================== -->

    <target name="build" depends="prepare" description="Compile Java sources">

    <!-- Compile Java classes as necessary -->
    <mkdir dir="${build.home}/WEB-INF/classes" />
    <javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}">
    <classpath refid="compile.classpath" />

    <!-- Copy application resources -->
    <copy todir="${build.home}/WEB-INF/classes">
    <fileset dir="${src.home}" excludes="**/*.java" />


    <!-- ==================== Dist Target ===================================== -->

    <target name="dist" depends="build,javadoc" description="Create binary distribution">

    <!-- Copy documentation subdirectories -->
    <mkdir dir="${dist.home}/docs" />
    <copy todir="${dist.home}/docs">
    <fileset dir="${docs.home}" />

    <!-- Create application JAR file -->
    <jar jarfile="${dist.home}/${}-${app.version}.war" basedir="${build.home}" />

    <!-- Copy additional files to ${dist.home} as necessary -->


    <!-- ==================== Flex targets ==================================== -->

    <target name="appcompile" depends="install">
    <mxmlc file="${APP_ROOT}/helloWorld.mxml" context-root="/helloWorldServer" keep-generated-actionscript="true" services="${web.home}/WEB-INF/flex/services-config.xml" output="${catalina.home}/webapps/${}/helloWorld.swf">
    <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
    <include name="${web.home}/WEB-INF/lib/" />
    <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml" />
    <source-path path-element="${FLEX_HOME}/frameworks" />


    <target name="createHtmlWrapper" depends="appcompile">
    <html-wrapper application="${APP_ROOT}/helloWorld.mxml" output="${catalina.home}/webapps/${}" swf="helloWorld" />

    <!-- ==================== Install Target ================================== -->

    <target name="install" depends="build" description="Install application to servlet container">

    <deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" localWar="file://${build.home}" />


    <!-- ==================== Javadoc Target ================================== -->

    <target name="javadoc" depends="build" description="Create Javadoc API documentation">

    <mkdir dir="${dist.home}/docs/api" />
    <javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*">
    <classpath refid="compile.classpath" />


    <!-- ====================== List Target =================================== -->

    <target name="list" description="List installed applications on servlet container">

    <list url="${manager.url}" username="${manager.username}" password="${manager.password}" />


    <!-- ==================== Prepare Target ================================== -->

    <target name="prepare">

    <!-- Create build directories as needed -->
    <mkdir dir="${build.home}" />
    <mkdir dir="${build.home}/WEB-INF" />
    <mkdir dir="${build.home}/WEB-INF/classes" />

    <!-- Copy static content of this web application -->
    <copy todir="${build.home}">
    <fileset dir="${web.home}" />

    <!-- Copy external dependencies as required -->
    <mkdir dir="${build.home}/WEB-INF/lib" />

    <!-- Copy static files from external dependencies as needed -->


    <!-- ==================== Reload Target =================================== -->

    <target name="reload" depends="build" description="Reload application on servlet container">

    <reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />


    <!-- ==================== Remove Target =================================== -->

    <target name="remove" description="Remove application on servlet container">

    <undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />

  11. Addtional ant configuration: Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.
  12. To build and install the file, simply run the createHtmlWrapper target in ant
  • The ant build file was created using the base build.xml file available on apache tomcat website.
  • The config files are used as is available with the BlazeDS service.

Popular Posts