Just finished configuring BlazeDS with Spring! 🙂
We have been thinking to include Flex in our J2EE application for a few months now. We believe integrating Flex as a client end application will vastly improve user experience and also improve on the code separation if we can design properly. BlazeDS is a server-based Java remoting and web messaging technology that enables developers to easily connect to back-end distributed data and push data in real-time to Flex applications. Both BlazeDS and Flex are open source products from Adobe.
After running the samples provided with BlazeDS download, I decided to have a go at it by trying to integrate it with Spring. Unfortunately I could not find any straight forward tutorial that could have guided me step by step. The closest I got was this tutorial that talks about integration of Spring with Adobe LiveCycle Data Service. Although I had to customize in few places, the tutorial was really helpful for my purpose.
BlazeDS download comes with a configured tomcat ready to run, which is pretty cool by the way! 🙂 Anyway, I chose to integrate Spring in the already deployed blazeds application under {blazeds}/tomcat/webapps. I followed all the steps of Example 1 in that tutorial considering my context root is {blazeds}/tomcat/webapps/blazeds. For example, when it asked me to copy jar files of Spring, I copied them to {blazeds}/tomcat/webapps/blazeds/WEB-INF/lib directory.
When it was time to run the application, as mentioned in Step 4, that didn’t work. The step asks to directly invoke the mxml file and assumes the Data Services application will compile it. I don’t think BlazeDS was configured for that feature yet. Therefore I had to take the help of Flex Builder 3 to compile the mxml file. I took the help of this tutorial to configure and create a J2EE project in Flex Builder. I followed all the 14 steps mentioned at the bottom of the page to create the project. I had to start my tomcat server to successfully validate the project configuration. Once that was done, rest was a piece of cake! The run button in Flex Builder automatically deploys the compiled swf file and opens it in browser.
Once my initial experiment was successful, I decided to create a formal project where I can work and use Ant to build and deploy my work. This is where things started to get rough! More on that later. So my first task was to write an Ant script that would deploy the necessary files in web server so that BlazeDS is deployed along with my project. I took the help of this nice and simple page to quickly get started with Ant. Then I had to install and configure Flex Ant Tasks and use it inside my build.xml to compile the mxml files. Once my script was ready, I thought I was done. But alas! the application was not working anymore. Remoting from my swf failed, it could not connect to the Spring service bean. After couple of hours of hair pulling time, I finally figured that the only difference between my current work and previous work (which ran successfully) is the compiled swf. Since we have to show the data service while creating a data service project, the Flex Builder must be doing something while compiling that my simple compile command was not doing. Once the problem was found, I looked into the compile command (mxmlc) in detail and found that I had to add -services and -context-root parameters to my mxmlc command. -services points to the services-config.xml file while context-root is the name of the context root, in my case – /blazeds. Then I used another build in ant task named html-wrapper that creates an html file for the corresponding swf, which makes it easier to view when deployed in web server. My Ant tasks now looks like this –
<target name=”compileflex”>
<mxmlc
file=”${web.dir}/mxml/main.mxml”
output=”${build.dir}/main.swf”
services=”${web.dir}/WEB-INF/flex/services-config.xml”
context-root=”/blazeds”
keep-generated-actionscript=”true”>
</mxmlc>
</target><target name=”wrapper” depends=”compileflex”>
<html-wrapper
title=”Welcome to My Flex App”
height=”300″
width=”400″
bgcolor=”red”
application=”app”
swf=”main”
version-major=”9″
version-minor=”0″
version-revision=”0″
history=”true”
template=”express-installation”
output=”${build.dir}”/>
</target>
Flex is cool to work with and I can’t wait to dive in deeper! Next stop is to find out how BlazeDS handles authentication and how can I get access to HttpSession.
Hi,
Thanks for this article. I’m trying to follow the steps and not able to make it work.
I have user Spring 2.0.8 and getting the error message on server startup
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.IllegalArgumentException: Class [org.apache.xbean.spring.context.v2.XBeanNamespaceHandler] does not implement the NamespaceHandler interface
Caused by: java.lang.IllegalArgumentException: Class [org.apache.xbean.spring.context.v2.XBeanNamespaceHandler] does not implement the NamespaceHandler interface
What version of spring you have used in this case?
Thanks
Vimal
Hi Silent,
I found your article very interesting, mainly because you integrated Spring into Blaze instead of the other way round.
I am a big Spring fan and think that this could be taken one step further by integrating Blaze into Spring MVC, thus implementing a form of remoting similar to how Hessian and alike are used within Spring at the moment.
I see that the Blaze code hasn’t been released yet, but when it does I would be very interesting in working with someone to help make Blaze a component which could run inside of Spring, thus allowing developers to continue using Spring MVC for all of their activities and reducing the complexity of set-up.
Excellent work, more to come I hope.
Keep us up to date
Josh
Hi there,
I was just wondering if you used the new type of Flex projects: WTP + LCDS (in your case we can use the flex.war from BlazeDS) – this type of projects are available in Flex Builder 3 if you have WTP (Web Tools Project) installed. They are pretty cool because you have all WTP features to take care the deploy of the project on the J2EE Server.
Hi, guys, thanks for stopping by. I really appreciate your comments.
Vimal, I am using Spring 2.5. The application context is pretty straight forward and I don’t think your error has anything to do with the version. But that reminds me, the tutorial asks to copy Spring.jar into lib. But copying only this jar didn’t work for me. Then I copied more Spring jars – spring-binding-1.0.2.jar, spring.jar, spring-mock.jar, spring-webmvc.jar. I am not sure if we need all of them for just integration but I didn’t wanna try out one by one once I failed first time! Hope this solves your problem.
Josh, it would be great if Spring can provide such component. Last night I integrated BlazeDS to our current Spring/Struts project by copying necessary files into our project. A component of Spring for such purpose would be really beneficial no doubt.
Mihai corlan, I didn’t use WTP. I am actually pretty new into Flex and still exploring. So far I have just used the blazeds web application that came with BlazeDS download, built a simple mxml file and called a service method of a Spring bean from that mxml using remoting. Thats it! I will look into your suggestion. Hope to post more soon!
Thanks again to all.
Thanks for the reply. I have tried everything I could, still not working.
If you don’t mind please email me a zip file of your BlazeDS installation to vimal.nair@tideworks.com
Thanks
Many thanks for your article. I was having exactly the same issue as you with the ant mxmlc task. You saved my day.
Vimal,
I had the same problem. The cause of the error was spring.jar is present in tomcat\lib\activemq4.1.1 directory. I removed the spring.jar and things worked fine.
Thanks
Hi,
I have been trying to follow the steps and am facing a few issues:
1. When I try to download the Flex DS, what I get to download is “lcds251-lin.bin” file. I believe that Flex DS is code named “Blaze” and hence I tried to work that one. The difference been that “flex.war” is not “blaze.war”.
But, when I put my SpringFactory, I get a compilation error which is “The method getServletConfig() is undefined for the type ServletConfig” at line 120 in the source.
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
2. Then i tried to integrate with an existing BlazeDS project and found that there are references for “blazeds-samples” while the download does not provide any such thing.
P.S.: I downloaded BlazeDS from http://www.adobe.com/cfusion/entitlement/index.cfm?e=labs_blazeds
Hey,
Just wanted to share I successfully got my project going with Flex3 – BlazeDS and Spring 2.5
I personally found it a little difficult to put spring into a Flex project and instead went the other way and put flex into an existing Spring MVC project (agree with Josh K).
Flex Builder, by default (at least I could not work it out) does not include all of the Java Builders usually needed to compile and deploy your standard spring app. in the of the .project file it only has the “flexbuilder”, so by starting with a spring app and then right clicking and adding “Flex Project Nature” seemed to help heaps
Cheers
Matt T
[…] Here is another blog entry from Silent Walker […]
Hey, i am having problem using mxmlc in ant. could you send your build file? thx.
Hello,
Did you follow up with:
Next stop is to find out how BlazeDS handles authentication and how can I get access to HttpSession.
Did you succeed?
Hi, Yes I did. It was pretty simple actually. You can get the request/session object from a static method, I forgot the name. Its been a long time and I don’t have the code right now…
Hi. Found this extremely useful, thanks. I extended it to work in Eclipse too — http://www.blackpepper.co.uk/black-pepper-blog/Saddling-Blaze-Adventures-with-BlazeDS-part-1-Eclipse-.html
Please have a try with maven-blazeds-spring-archetype at http://code.google.com/p/maven-blazeds-spring-archetype/
This project aims to create the basic project structure for you to start with maven2, blazeds, spring stacks
The project created from the archetype has strong integration with Eclipse, Flex Builder and WTP
To get the http session, you can use static methods provided in FlexContext
Tricky thing is when you write test cases that use FlexContext, you need to use AspectJ to intercept to FlexContext and recompile it into a library to be used in your testcases
Hi,
I got the same error that Maverick got. I use myeclipse and I added web project capablities and the compile errors were gone. but when I tried to restart my server I got an errror which says
javax.servlet.UnavailableException: An error occurred trying to construct FlexFactory ‘flex.samples.factories.SpringFactory’. The underlying cause is: ‘java.lang.Error: Unresolved compilation problems:
The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required .class files
The type javax.servlet.ServletConfig cannot be resolved. It is indirectly referenced from required .class files
The method getServletContext() is undefined for the type ServletConfig
‘.
at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:170)
Any idea why?
I got the same error that Venkat and Maverick reported.
Help is much appreciated!
I have been struggling with this for literally days. Thank you so much for this “from the ground up” writeup. I do not have Flex Builder and I was struggling with getting my applications to connect to the AMF providers. I though I tried everything. I even set the context-root to no avail! Apparently I did not have my syntax correct.
After looking at your examples I was able to get it to connect properly and I am off and running!
Thank you, Thank you, Thank you,
Ben
Venkat, JettyFan and Maverick
You guys are missing servlet-api.jar. In order for you to see FlexContext.getServletConfig you have to add servlet-api.jar in your web app library.
Regards,
Ken
A zipfile of your entire flex-demo project would go great with that Ant file sample…
Hope you help with this interesting error msg, iam completely clueless
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/web-application-config.xml]; nested exception is java.lang.AbstractMethodError: org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser.getBeanClass(Lorg/w3c/dom/Element;)Ljava/lang/Class;
Caused by: java.lang.AbstractMethodError: org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser.getBeanClass(Lorg/w3c/dom/Element;)Ljava/lang/Class;
at org.springframework.beans.factory.xml.AbstractSingleBeanDe