Get Up and Running with BlazeDS AMF in Spring MVC

There is a dismal lack of clear instruction for configuring BlazeDS AMF services with Spring. Many of the resources that do exist refer to older versions of the software or strict scenarios that do not apply to everyone using Spring for their projects. This document will outline the steps necessary to configure BlazeDS with Spring when using both Spring MVC, specifically, and Flex.

BlazeDS can work with Spring MVC

You can grab the latest build of Spring from:
http://www.springsource.org/

You can grab the latest build of BlazeDS from:
http://opensource.adobe.com/wiki/display/blazeds/BlazeDS


First, you will want to be sure that Spring is configured in the following way:

  1. You will already have a dispatcher servlet entry in your web.xml for Spring MVC similar to the following:
    <servlet>
    <servlet-name>spring</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    		<servlet-name>spring</servlet-name>
    		<url-pattern>*.do</url-pattern>
    </servlet-mapping>
  2. Be sure to include the following JAR packages in the /WEB-INF/lib directory of your project:
    • asm.jar
    • blazeds-common-3.0.jar
    • blazeds-core-3.0.jar
    • cglib-2.2.jar
    • com.springsource.edu.emory.mathcs.backport-3.0.0.jar
    • com.springsource.flex.messaging.services.remoting-3.2.0.3978.jar
    • org.springframework.flex-1.0.3.RELEASE.jar
    • commons-codec-1.3.jar
    • commons-httpclient-3.0.1.jar
    • flex-messaging-common.jar
    • flex-messaging-core.jar
    • flex-messaging-opt.jar
    • flex-messaging-proxy.jar
    • flex-messaging-remoting.jar
    • flex-rds-server.jar
    • xalan.jar
    • commons-logging.jar
    • cfgatewayadapter.jar
    • spring-flex-1.0.1.RELEASE.jar

To get BlazeDS integrated with Spring, complete the following steps:

  1. Add the following lines to web.xml:
    <servlet-mapping>
    	<servlet-name>spring</servlet-name>
    	<url-pattern>/messagebroker/amf</url-pattern>
    </servlet-mapping>
  2. Add the following lines to your servlet XML file:
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <flex:message-broker>
    	<flex:mapping pattern="/messagebroker/amf" />
    </flex:message-broker>
  3. Create a file called “services-config.xml” and add this to /web-inf/flex:
    <?xml version="1.0" encoding="UTF-8"?>
    <services-config>
    	<services>
    		<default-channels>
    			<channel ref="my-amf" />
    		</default-channels>
    	</services>
    	<channels>
    		<channel-definition id="my-amf"
    			class="mx.messaging.channels.AMFChannel">
    			<endpointurl=http://{server.name}:{server.port}/{context.root}/messagebroker/amf class="flex.messaging.endpoints.AMFEndpoint" />
    			<properties>
    				<polling-enabled>false</polling-enabled>
    			</properties>
    		</channel-definition>
    	</channels>
    	<logging>
    		<target class="flex.messaging.log.ConsoleTarget" level="info">
    			<properties>
    				<prefix>[BlazeDS]</prefix>
    				<includeDate>false</includeDate>
    				<includeTime>false</includeTime>
    				<includeLevel>true</includeLevel>
    				<includeCategory>false</includeCategory>
    			</properties>
    			<filters>
    				<pattern>Endpoint.*</pattern>
    				<pattern>Service.*</pattern>
    				<pattern>Configuration</pattern>
    			</filters>
    		</target>
    	</logging>
    </services-config>
  4. Import the RemotingDestination class and add the following annotations to any Java classes that will be used by Flash:
    import org.springframework.flex.remoting.RemotingDestination;
    @Service("flexService")
    @RemotingDestination(value="flexService",channels={"my-amf"})
  5. If you are using Spring security, add the following line to whichever xml you are using to define security parameters so we can access message broker:
    <security:intercept-url pattern="/messagebroker/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />

Now, to get Flex talking to Java through BlazeDS, we must configure our application as such:

  1. Import the remoting event packages so that we can send data over AMF:
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
  2. Now, in your declarations tag, set up a RemoteObject structure similar to this:
    <fx:Declarations>
    		<s:RemoteObject id="ro" endpoint="http://website.root/messagebroker/amf" destination="flexService" result="resultAMF(event)" fault="faultAMF(event)">
    			<s:method name="methodNameFromJavaClass">
    				<s:arguments>
    					<arg1>{arg1}</arg1>
    					<arg2>{arg2}</arg2>
    					<arg3>{arg3}</arg3>
    				</s:arguments>
    			</s:method>
    		</s:RemoteObject>
    </fx:Declarations>

    If you are not passing any arguments, you can simplify the setup as such:

    <fx:Declarations>
    	<s:RemoteObject id="ro" endpoint=" http://website.root/messagebroker/amf" destination="flexService" result="resultAMF(event)" fault="faultAMF(event)"/>
    </fx:Declarations>
  3. Create methods to handle the result and fault events reported through our remote service:
    private function resultAMF(e:ResultEvent):void {}
    private function faultAMF(e:FaultEvent):void {}
  4. To invoke a remoting call through BlazeDS AMF, simply invoke the specific method off of our remote object instance:
    ro.methodNameFromJavaClass.send();

That should be all there is to it.

If you want to test this quickly, here is some quick MXML to do so:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   width="600" height="400">

	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;

			protected function testAMF(e:MouseEvent):void {
				ro.helloWorld.send();
			}
			private function resultAMF(e:ResultEvent):void {
				Alert.show("BlazeDS Worked...", "Hot Shit!");
			}
			private function faultAMF(e:FaultEvent):void {
				Alert.show(e.fault.faultString, "Duck and Cover!!!");
			}
		]]>
	</fx:Script>

	<fx:Declarations>
		<s:RemoteObject id="ro" endpoint="http://website.root/messagebroker/amf" destination="flexService" result="resultAMF(event)" fault="faultAMF(event)"/>
	</fx:Declarations>

	<s:Button width="375" height="120" label="Test BlazeDS" click="testAMF(event)" fontSize="36" fontWeight="bold" horizontalCenter="0" verticalCenter="0"/>

</s:Application>

Special thanks to Carrie Lorenz for figuring out all the Spring MVC bits and assisting with this documentation.

Note that this document was compiled from our internal experiences integrating BlazeDS and Spring MVC. This is what worked for us… we do not claim to be experts in this area – if you have any feedback, please let us know!

IIS7 URL Rewrite QuickTip: Allow Flash Gateway

We all know that ColdFusion allows Flash Remoting (AMF) communication to occur via two separate URL strings:

  • http://my.site.com/flex2gateway
  • http://my.site.com/flashservices/gateway

I recently came across the problem where the URL Rewrite rules in IIS7 were blocking both of these URLs from being passed on and correctly processed by ColdFusion. The solution is to add the following two rules to your site definition and move them both to the top of the stack, above any other rules you may have. This will effectively allow both of these URLs to process normally and stop and subsequent rules from executing if the pattern matches.

You can also edit your site definition web.config file to add the rules there:

1
2
3
4
5
6
7
8
9
10
<rule name="flashservices/gateway" stopProcessing="true">
	<match url="flashservices/gateway" />
	<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
	<action type="None" />
</rule>
<rule name="flex2gateway" patternSyntax="ExactMatch" stopProcessing="true">
	<match url="flex2gateway" />
	<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
	<action type="None" />
</rule>

Again, be sure they appear before any other rules and you should be good to go! Flash on!

Remoting through Flex with Coldfusion

I’m used to setting the ObjectEncoding to AMF0 when working with Flash Media Server 2, but haven’t realized till now that I also am required to do this when communicating with Coldfusion 8 through remoting:

1
2
3
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

The error “Unknown object type tag (17)” was being generated by CF8 as I attempted to pass an Object in AS3 over remoting to CF8 interpreted as a Structure. Apparently, there is also the need to wrap any such Object within a container Object for it to be properly read by the CFC:

1
2
3
4
var wrapper:Object = new Object();
wrapper.submissionObject = submissionObject;
submissionResponder = new Responder(onSubmissionResult, onSubmissionError);
testConnection.call("some.cfc.Test", submissionResponder, wrapper);

The CFC function expects a Structure named “submissionObject” in this case.

I hope this is helpful for someone- I had a hell of a time digging up this information.