The Flex Show: Episode 132

The Flex Show

An interview I did with The Flex Show, back in December 2010, is now available!

On the show, we talk about the use of Flex in higher education – specifically focused on projects I’ve been involved in at the University of Denver. We also go over the Adobe Education Leader program and chat about the wider Flex-in-education community. There is even some talk on mobile application development, the implications of tablet computing in education, and even some AIR for TV!

Toward the close of the episode; I talk about my upcoming book and give mentions to CodeBass, Emergent Collective One, 6Threads, and other projects.

Listen here: Flex in Higher Education: The Flex Show Episode 132

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!

Interview with The Flex Show

The Flex Show

Last evening, I was interviewed by Jeffry Houser and John Wilker of The Flex Show with a focus on Flex Development in Higher Education. We chatted about a general overview of Flex and education, some Flex projects I’m involved with at the University of Denver, a bit about the Adobe Education Leader program, and emerging areas like tablet computing and AIR on TV. At the closing of the interview I was able to plug a bunch of projects from this year including 6Threads and Emergent Collective One, and also talk up my upcoming book!

This episode should be available either at the end of this year or the beginning of 2011is now available!:
Joseph Labrecque Flex in Higher Education : The Flex Show Episode 132

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!

Memory Leak in Flex SDK 4.1 Spark VideoDisplay

In working on a small utility application (Adobe AIR) this week, I came across a pretty major memory leak in the beta Open Source Media Framework based Spark components in Flex 4.1 final.

The utility I am building grabs over 10,000 video clip records from our media repository and then systematically goes through and renders a still image of each clip. There’s actually a lot more to it than that and I’ll probably either write about or present on some of the neat tricks I am doing with this app soon, but for the sake of this article this is all you need to know.

I’m using s:VideoDisplay to grab data from an RTMP stream and also use the events present to manage the system method calls. Should be no big deal- but once the application hit 500-1000 out of the 10,000+ records, it would simply crash out. No good.

Using the Windows Task Manager, I could watch the memory usage in ADL skyrocket as each video was processed into a still image. Obviously something was up. Using the profiler in Flash Builder 4 I was able to isolate the problem to the org.osmf.* classes being used by the Spark VideoDisplay component.

It’s important to note here that while Flex 4.1 is certainly release quality, it (along with Flex 4.0) was released with components based on beta OSMF code. Flex 4.5, while not final by any means, does include the final OSMF 1.0 classes. I was able to download and compile the same project with Flex 4.5 and can happily say I have had no more memory leaks whatsoever. The application is running like a champ and processing thousands of videos without issue. Windows Task Manager shows that the memory used by ADL is being properly released and all is well.

As a side note, I could have written my own video display based on flash.media.Video or even used mx:VideoDisplay… but I wanted to get to the bottom of this :)