Flex

Setting up BlazeDS

Introduction

I put this document together to collect all my actions and endeavors as I built out a Flex/BlazeDS client and a Java/BlazeDS server. I found the BlazeDS documentation to be very confusing and hard to follow, so you can look at this documentation as a sort of “Getting Started with BlazeDS” in 10 pages or less. Another goal of this BlazeDS endeavor was to avoid using the BlazeDS turnkey system. I wanted to start a Java backend from scratch, using IntelliJ IDEA to build the project and then add in the BlazeDS bits as I needed them. Thus this journey will go about building up a brand new BlazeDS Java project from the ground up and it will not use the blazeds.war as a template.

Personally, I find using a war file as a blank template a bad practice. You should know how all the pieces fit together, and if you need to distribute a template war file to get everything to work correctly, then your solution is probably too complex in the first place. I will say that the BlazeDS system seems to take XML configuration to a whole new level, and that's not a good thing. In a development world that is moving toward convention over configuration, it seems that BlazeDS is a blast from the past. Hopefully in future iterations, we'll see the BlazeDS folks use more Java annotations to configure some of this stuff.


Download the BlazeDS turnkey system

I realize that I said that we wouldn't be using the BlazeDS turnkey system, but we will use bits and pieces of it to build up our own project. So go download it and extract it to a new directory.  It seems that the ds-console.war file, which is invaluable for development of your BlazeDS services, is only distributed in this turnkey system distribution.


Installing the BlazeDS monitoring console application

The BlazeDS turnkey system contains a few WAR files in the root directory. One of the WAR files, ds-console.war, is a Flex-based BlazeDS monitoring system that you can use to debug your own BlazeDS project with. When dropped into the Tomcat server that you will eventually deploy to, the BlazeDS console will automagically discover your BlazeDS configuration and provide various monitoring statistics. It was invaluable to me as I was building my own BlazeDS system, so I mention here so others might find it useful and use it.

  1. Copy the ds-console.war file from the BlazeDS turnkey distribution to your Tomcat webapps directory. The ds-console.war contains a simple BlazeDS monitoring application that will be invaluable for you while you build out BlazeDS applications.
  2. Start Tomcat to explode the ds-console.war.
  3. Navigate to the /ds-console URI on your Tomcat server. For instance, if you're running Tomcat on the default port 8080, navigate a web browser to http://localhost:8080/ds-console. The SWF should start and you should see a tabbed monitoring console, similar to the screen shot below:
Click on the image to view in original size


Create your BlazeDS web application

Set the web application CLASSPATH

Create a standard Java web application in your IDE of choice.  The following libraries from the BlazeDS distribution need to be added to the web application classpath:

  • backport-util-concurrent.jar
  • cfgatewayadapter.jar
  • commons-codec-1.3.jar
  • commons-httpclient-3.0.1.jar
  • commons-logging.jar
  • flex-messaging-common.jar
  • flex-messaging-core.jar
  • flex-messaging-opt.jar
  • flex-messaging-proxy.jar
  • flex-messaging-remoting.jar
  • xalan.jar

Set up the web.xml

Edit your web.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>My BlazeDS server</display-name>
    <description>My BlazeDS Server Application</description>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <!-- MessageBroker Servlet -->
    <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>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Bolded items are BlazeDS-specific.


BlazeDS configuration files

BlazeDS has its own collection of XML configuration files.  Since I'm only interested in the BlazeDS remoting services in this example, I edited two files: services-config.xml and remoting-config.xml.

services-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <services>
        <service-include file-path="remoting-config.xml"/>
    </services>
    <channels>
        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
                      class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>
        <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
            <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure"
                      class="flex.messaging.endpoints.SecureAMFEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>
        <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling"
                      class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>true</polling-enabled>
                <polling-interval-seconds>4</polling-interval-seconds>
            </properties>
        </channel-definition>
    </channels>
    <logging>
        <target class="flex.messaging.log.ConsoleTarget" level="Error">
            <properties>
                <prefix>[BlazeDS]</prefix>
                <includeDate>false</includeDate>
                <includeTime>false</includeTime>
                <includeLevel>false</includeLevel>
                <includeCategory>false</includeCategory>
            </properties>
            <filters>
                <pattern>Endpoint.*</pattern>
                <pattern>Service.*</pattern>
                <pattern>Configuration</pattern>
            </filters>
        </target>
    </logging>
    <system>
        <redeploy>
            <enabled>false</enabled>
        </redeploy>
    </system>
</services-config>


remoting-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" 
    class="flex.messaging.services.RemotingService">
    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>
    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>
   <destination id="loginService">
        <properties>
            <source>myblazedsapp.services.LoginService</source>
        </properties>
    </destination>
    <destination id="searchService">
        <properties>
            <source>myblazedsapp.services.SearchService</source>
        </properties>
    </destination>
</service>