This article outlines the 13 reasons why Java programmers should learn Flex and BlazeDS. It talks about why Flex with BlazeDS is one of the best choices for developing rich Internet applications (RIAs)—from highly interactive websites to enterprise applications with Java back ends. Most importantly, it shows the high return on investment (ROI) that this combination provides, both for developers and for enterprises.
In outlining the 13 reasons why Java programmers should learn BlazeDS, I use a fictitious soda dispenser to show how to morph an existing Java program into an RIA. Through the example, I explain the different ways you can use BlazeDS, either with your existing Java applications or with new Java applications.
Reason 1: It's open source
The core Flex software development kit (SDK) is an open source framework used for building and maintaining RIAs that look and feel the same across different browsers and operating systems. Flex is released under the Mozilla Public License. Compiled Flex applications run in the proprietary Adobe Flash platform.
For connecting Flex to Java, BlazeDS is an open source technology for remoting and messaging. It runs on Java application servers as a servlet, allowing you to use it in any standard Java web application. BlazeDS is licensed under the Lesser GNU Public License (LGPL). Along with the release of BlazeDS, Adobe published the ActionScript Message Format (AMF) specification, which allows BlazeDS and Java to communicate with the Flex client using a compact binary format.
Reason 2: It has great community support
Flex has a very active community, with a lot of community-contributed projects. Flex.org, an Adobe site with community news, sees almost daily community contributions, and the Flex users group on Yahoo! has over 11,000 members.
For example, the FlexLib project on Google Code has contributed a large amount of open source user interface (UI) components. The Swiz and Mate projects have contribute a framework to help handle events. And Gorilla Logic has contributed Flex Monkey for automated UI testing.
Reason 3: You can have a job in another six months
According to James Ward, a Flex evangelist for Adobe, there is still a large need for senior Flex developers. Learning Flex gives you marketable skills that will put you ahead of the competition.
Reason 4: Higher business ROI
In general, developing enterprise web applications has been notoriously difficult. Flex and BlazeDS provide not only higher-powered tools but are also easier to develop with. The increase in developer productivity allows organizations to get to market more quickly. Flex and Flash also create a more engaging user experience, which can increase traffic and conversion rates.
An ideal example of this is the Borders book store chain, which recently introduced a new website with a "Magic Shelf." This site uses a Flash interface to simulate the experience of browsing books. Borders found that this greatly increased conversion rates: "Customers who use this Flash-driven interface—which allows visitors to view the covers of books, DVDs, and CDs—convert at about 62 percent higher than those who do not".
Reason 5: Flex was the first language designed for creating UIs
Most languages add support for building UIs as an afterthought. This was particularly true for Java with Swing. As a result, simple things in Swing—like wiring data—can be painful and require using Swing-specific data models. The largest problem with Swing is that you have to dig very deep into the application programming interface (API) to be productive.
Flex is the exact opposite: It was designed from the ground up for building web UIs. As Bruce Eckel likes to say, Flex is the first domain-specific language (DSL) for UI development. So, it's much easier to create UIs in Flex than in other technologies, such as JavaServer Pages (JSP), JavaServer Faces (JSF), and Swing. Data binding, event handling, component layout, and other, common UI development techniques are built into the language, which allows you to be productive even when you know very little about the language.
Reason 6: It has a programming model similar to Java
You can develop with the existing tools used to develop Java. You can also develop Flex applications with the free command-line tools included in the SDK, with Adobe Flex Builder (an Eclipse plug-in), or most recently, with IntelliJ IDEA 8.
Flex provides a stateful environment in which data is loaded from the client. The programming model is more like developing a desktop client than Hypertext Markup Language (HTML) programming, which makes the programming model feel familiar to anyone who has done Java Swing programming.
Flex is programmed using a combination of MXML, a UI markup language similar to Extensible Markup Language (XML), and Adobe ActionScript, an object-oriented scripting language. This combination makes programming Flex similar to programming with Java, because you can use the familiar object-oriented concepts.
An ideal way to set up your development environment is to have the Flex application built right in your web deployment directory. That way, you do not need to re-deploy your application after every build: A simple browser refresh reflects the latest changes. Developing with Flex and BlazeDS is fairly productive as a result.
Reason 7: BlazeDS runs in any Java application server
Multiple versions of BlazeDS are available, including a turnkey version that includes a version of Apache Tomcat configured with BlazeDS. For this article, I used the binary distribution, which comes as a web archive (WAR), to show how you can deploy to any application server. Otherwise, you can extract the Java archive (JAR) files and use them in your project. Visit the BlazeDS wiki for information about the various options for installing BlazeDS.
This example uses BlazeDS with an existing project—a simple soda dispenser. You just need to add the JAR files to your project, then you can use BlazeDS from within the application, which allows you to deploy it anywhere you can deploy the application.
To add BlazeDS to your project, complete these steps:
- Extract the contents of the BlazeDS WAR file jar xvf blazeds.war.
- Copy the JAR files to your project lib directory: cp -R WEB-INF/lib /sodaSample.
Reason 8: You can use it with existing Java applications
For the soda example, assume that you want to expose your existing soda service so that remote Flex applications can connect to it. The basic steps to set up BlazeDS with an existing application are:
- Edit the BlazeDS configuration files in the WEB-INF/flex directory.
- Define
MessageBrokerServlet
and a session listener in the application web.xml.
After you set up BlazeDS, you need to add your soda service to the BlazeDS remoting configuration file, which allows the soda service to be called from a Flex client. You make this addition by defining a destination and one or more channels to transport the data. The basic AMF Channel is defined in the services.xml file. Use this definition to identify your destination in remoting-config.xml:
<destination id="sodaService" channels="my-amf"> <properties> <source>com.gorillalogic.sodaSample.SodaService</source> </properties> </destination>
By defining endpoints in the remoting configuration file, you can call any basic Java service from a Flex client.
To pass your Java data model to your Flex client, define a mapping between the two in your ActionScript class:
[Bindable] [RemoteClass(alias="com.gorillalogic.sodaSample.SodaModel")]
This code tells Flex that when the results from a remote service call return your Java SodaModel, map it to your Flex SodaModel
. The Flex client in the sample shows how to call this Java service. The call returns a SodaModel
, filled with your order:
public function callSodaService():void { var sodaType:String = type.text; var sodaCount:int = parseInt(cnt.text); var flag:Boolean = preOpen.selected; remoteObject.getSoda(sodaType, sodaCount, flag); } private function resultHandler(event:ResultEvent):void { var sodaModel:SodaModel = event.result as SodaModel; }
Flex returns your results in the generic result
variable, which you map into your SodaModel
. I won't go into the details here, but one tip is to specify services-config.xml in the compiler configuration, like so:
-locale en_US -services=/nsource/sodaSample/web/WEB-INF/flex/services-config.xml -context-root /
If not, your Flex client will not be able to find the Java services. In the same manner, you can pass an object from the client back to the server. For example, you could pass the empty soda model back to the client.
Reason 9: You can extend BlazeDS and modify it with Java
Suppose you decide that you want to add special logging anytime the soda service is called. You can extend the standard Java adaptor to add logging.
First, add a Java class that extends the JavaAdapter
:
import flex.messaging.services.remoting.adapters.JavaAdapter. public class TimingJavaAdapter extends JavaAdapter {
Then, overload the invoke() method:
public Object invoke(Message message) { RemotingMessage remotingMessage = (RemotingMessage) message; String operation = remotingMessage.getOperation(); String destination = remotingMessage.getDestination(); Logger.info("calling " + operation + " on destination " + destination); Object data = super.invoke(message); return data; }
In this method, you can see the incoming operation and the destination for the call. You can use this simple hook for other things, as well—for example, timing how long calls to the server are taking.
Reason 10: You can call BlazeDS from HTML and JSP
You can make this call in several different ways, such as through the Browser Manager and flashVars
. The flashVars
can be set by the HTML page, then read by the Flex application.
For example, suppose you wanted to pass your user name and the type of soda you want to order in from your HTML page. You could set the flashVars
in your HTML page like so:
<object id='SodaSample' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab' height='100%' width='100%'> <param name='src' value='SodaSample.swf'/> <param name='flashVars' value='username=ryan&type=coke'/> <embed name='mySwf' src='SodaSample.swf' pluginspage='http://www.adobe.com/go/getflashplayer' height='100%' width='100%' flashVars='username=ryan&type=coke'/> </object>
Then, in your Flex application, you can read these variables by getting the application parameters:
var username:String; if (Application.application.parameters.hasOwnProperty("username")) { username = Application.application.parameters.username; }
Reason 11: Flex and BlazeDS data transfer outperform other Ajax solutions
By default, all the remote procedure calls (RPCs) in use employ the AMF Binary Protocol. An open standard, AMF is extremely fast. James Ward put together a sample that shows a comparison with other remoting solutions. Although other Asynchronous JavaScript and XML (Ajax) solutions like Dojo struggle with a couple of hundred rows, Flex with BlazeDS can easily handle thousands of rows. (See James Ward's census for a benchmark of different RIA data loading technologies.)
Reason 12: You can call BlazeDS directly from Java clients
The latest release of BlazeDS includes a Java AMF class that you can use to make a Java client call the BlazeDS server. Being able to call BlazeDS this way is extremely useful for unit testing and load testing.
Reason 13: It works with Spring
Adobe and Spring have teamed up to bring closer integration between the two projects, and the initial release of the Spring–BlazeDS integration shows a lot of promise. By allowing a Spring Bean to become a remote service, duplicate configuration files will be eliminated. For more information, visit the project's home page.
Conclusion
With BlazeDS being open source and based in Java, it is an ideal choice for new and existing Java server projects. With high-performance remote communication and object mapping between Flex and Java, the Flex and BlazeDS technologies are an ideal choice for RIA development. Java developers new to Flex and BlazeDS will find the development process highly productive and easy to learn.
Flex with BlazeDS is also an ideal choice for large-scale Java enterprise applications. In a previous project, my team wrote an application that contained over 50 different screens and would regularly transfer several thousand rows of data between the server and client. This type of application would have been nearly impossible using traditional Ajax technologies. With Flex and BlazeDS, we where able to release an initial version in under a year. See what this dynamic duo can do for your application-development projects.
Note: You should have some basic background in these technologies. Several tutorials are available to get you started with BlazeDS, including "Building Web and Desktop Applications with BlazeDS and AMF" at and "BlazeDS 30-minute test drive".