This Refcard assumes that you are already familiar with Spring and Flex. If you need an introduction or refresher to either, check out the Very First Steps in Flex and/or Spring Configuration DZone Refcardz.
To use BlazeDS, the server-side application could be any Java application that deploys as a WAR file. This Refcard uses the Eclipse IDE to create and edit the Java project. This Refcard walks you through the following steps:
- Set up a server-side Java project with BlazeDS and the Spring Framework
- Configure the Java project with a basic Spring bean for use in BlazeDS
- Write Flex application to use the Spring/BlazeDS service
BlazeDS provides simple two-way communication with Java back-ends. Adobe Flash Player supports a serialization protocol called AMF that alleviates the bottlenecks of text-based protocols and provides a simpler way to communicate with servers. AMF is a binary protocol for exchanging data that can be used over HTTP in place of text-based protocols that transmit XML. Applications using AMF can eliminate an unnecessary data abstraction layer and communicate more efficiently with servers. To see a demonstration of the performance advantages of AMF, see the Census RIA Benchmark at: http://www.jamesward.org/census.The specification for AMF is publicly available, and numerous implementations of AMF exist in a variety of technologies including Java, .Net, PHP, Python, and Ruby.
The open source BlazeDS project includes a Java implementation of AMF that is used for remotely communicating with server-side Java objects as well as for a publish/subscribe messaging system. The BlazeDS remoting technology allows developers to easily call methods on Plain Old Java Objects (POJOs), Spring services, or EJBs. Developers can use the messaging system to send messages from the client to the server, or from the server to the client. BlazeDS can also be linked to other messaging systems such as JMS or ActiveMQ. Because the remoting and messaging technologies use AMF over HTTP, they gain the performance benefits of AMF as well as the simplicity of fewer data abstraction layers. BlazeDS works with a wide range of Javabased application servers, including Tomcat, WebSphere, WebLogic, JBoss, and ColdFusion.
To follow along with this tutorial you will need:
First, set up the server-side Java web project in Eclipse by creating a web application from the blazeds.war file (found inside the blazeds zip file).
- Import the Blazeds.war file to create the project:
- Choose File > Import
- Select the WAR file option. Specify the location of the blazeds.war file. For the name of the web project, type dzone-server
- Click Finish
Now you can create a server that will run the application:
- Select File > New > Other
- Select Server > Server
- Click Next
- Select Apache > Tomcat v6.0Server
- Click Next
- Specify the location where Tomcat is installed and select the JRE (version 5 or higher) to use
- Click Next
- Select dzone-server in the Available Projects list
- Click Add to add it to the Configured Projects list
- Click Finish
Next, in the dzone-server project create the basic Java classes to be used by BlazeDS and Spring:
public class MyEntity {
private String firstName;
private String lastName;
private String emailAddress;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
Listing 1: Java entity to be passed between Java and Flex
import java.util.List;
public interface MyService {
public List<MyEntity> getMyEntities();
}
Listing 2: Java Service Interface
import java.util.ArrayList;
import java.util.List;
public class MyServiceImpl implements MyService {
public List<MyEntity> getMyEntities() {
List<MyEntity> list = new ArrayList<MyEntity>();
MyEntity entity = new MyEntity();
entity.setFirstName(“Hello”);
entity.setLastName(“World”);
entity.setEmailAddress(“hello@world.com”);
list.add(entity);
MyEntity entity2 = new MyEntity();
entity2.setFirstName(“Hello”);
entity2.setLastName(“Space”);
entity2.setEmailAddress(“hello@space.com”);
list.add(entity2);
MyEntity entity3 = new MyEntity();
entity3.setFirstName(“Hello”);
entity3.setLastName(“Neighbor”);
entity3.setEmailAddress(“hello@neighbor.com”);
list.add(entity3);
return list;
}
}
Listing 3: Java Example Service Implementation
Listings 1, 2, and 3 are very basic Java classes that you’ll use as examples for this tutorial. In a real-world application, the service implementation would likely connect to one or more enterprise services for data, such as a relational database. In this case, it simply returns a hard-coded set of entities as an ArrayList.
The basic Java web project with the BlazeDS dependencies is now complete.
Next, configure the Java project with a basic Spring bean for the MyService interface:
- Copy the Spring libraries, the Spring BlazeDS Integration Library, and the ANTLR library to the project dzoneserver/ WebContent/WEB-INF/lib directory
- Create a basic Spring Configuration File:
- Right Click WebContent/WEB-INF and then choose New > File
- For the file name, type application-config.xml
- Click Finish
- Copy and paste the text from Listing 4 into the file
<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>
<!-- Spring Beans’s -->
<bean id=”myService” class=”MyServiceImpl” />
</beans>
Listing 4: Basic Spring Configuration
Those familiar with Spring should recognize this as a basic Spring configuration for creating a simple bean from the MyServiceImpl class. Later in this tutorial you will be using this bean through BlazeDS.
At this point, you have a basic Java web project with a default BlazeDS configuration. Now, you’ll change the default BlazeDS configuration to use the newly created Spring bean.
To begin configuring Spring BlazeDS Integration, update the web.xml file by removing the default BlazeDS configuration and replacing it with the code from Listing 5.
<>?xml version=”1.0” encoding=”UTF-8”?>
<>web-app>
<>display-name>dzone-server<>/display-name>
<>servlet>
<>servlet-name>Spring MVC Dispatcher Servlet<>/servlet-name>
<>servlet-class>
org.springframework.web.servlet.DispatcherServlet
<>/servlet-class>
<>init-param>
<>param-name>contextConfigLocation<>/param-name>
<>param-value>/WEB-INF/application-config.xml<>/param-value>
<>/init-param>
<>load-on-startup>1<>/load-on-startup>
<>/servlet>
<>!-- Map /spring/* requests to the DispatcherServlet -->
<>servlet-mapping>
<>servlet-name>Spring MVC Dispatcher Servlet<>/servlet-name>
<>url-pattern>/spring/*<>/url-pattern>
<>/servlet-mapping>
<>/web-app>
Listing 5: web.xml
The web.xml contents in Listing 5 create a servlet filter from Spring that will process all BlazeDS requests at: http://localhost:8080/dzone-server/spring This will be the base URL for accessing the BlazeDS endpoint. Also, you should notice that this is a standard DispatcherServlet for Spring.
Now that you have Spring wired into the Java web application, you will update the basic Spring configuration from Listing 4 so that it will work with BlazeDS. Add the highlighted section from Listing 6 to your application-config.xml file.
<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:flex=”http://www.springframework.org/schema/flex”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd”>
<!-- Spring Beans’s -->
<bean id=”myService” class=”MyServiceImpl” />
<!-- Simplest possible message broker -->
<flex:message-broker />
<!-- exposes myService as BlazeDS destination -->
<flex:remote-service ref=”myService” />
</beans>
Listing 6: Advanced Spring Configuration for BlazeDS
Listing 6 exposes the MyServiceImpl class as a BlazeDS destination. First, the Flex® namespace is added to the configuration. Note that the XSD will not be published from Spring until the final 1.0 release, and until then you will have to add it manually to your XML catalog. With the Flex namespace added, the configuration uses the messagebroker tag to create the MessageBrokerFactoryBean. Since there is no additional configuration information provided, the MessageBroker will be created with “sensible defaults,” assuming that the service-config.xml is in WEB-INF/flex/ services-config.xml. The remote-service tag creates a destination from existing Spring beans.
In Spring BlazeDS Integration release 1.0.0M2, the standard BlazeDS configuration file (servicesconfig. xml) is still used for configuration of the communication channels.
Next, update the default BlazeDS services-config.xml file (found in the WebContent/WEB-INF/flex folder) to reflect the Spring URL defined in the web.xml file. Replace the contents of the file with the code in Listing 7.
<?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”>
<endpoint
url=”http://{server.name}:{server.port}/{context.root}/spring/
messagebroker/amf”
class=”flex.messaging.endpoints.AMFEndpoint”/>
</channel-definition>
<channel-definition id=”my-polling-amf”
class=”mx.messaging.channels.AMFChannel”>
<endpoint
url=”http://{server.name}:{server.port}/{context.root}/spring/
messagebroker/amfpolling”
class=”flex.messaging.endpoints.AMFEndpoint”/>
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>4</polling-interval-seconds>
</properties>
</channel-definition>
</channels>
</services-config>
Listing 7: Update channel definition in services-config.xml
Note that the endpoint URL for the my-amf and my-polling-amf channels in Listing 7 include “spring” after the context.root parameter. This is the only configuration change you need to make in the BlazeDS default configuration files. All the remote destinations are configured in the Spring application-config. xml file.
You are now done configuring the server-side Spring / BlazeDS Java application. You may want to start up the Tomcat server to verify that your configuration is correct.
Now you can build the Flex application to use the Spring service remotely. Follow these steps to create the Flex project:
- Select File > New > Other
- In the Select A Wizard dialog box, select Flex Project
- In the New Flex Project box, type in a project name: dzone-flex
- Use the default location (which will already be checked)
- Select Web Application (Runs In Flash Player)
- Select None as the Application Server Type
- Click Next
- Specify the Output folder to be the location of the dzoneserver’s WebContent directory such as: C:\workspace\dzone-server\WebContent\
- Click Finish
Your project will open in the MXML code editor and you’ll see a file titled main.mxml. Open the file and add the Flex® application code from Listing 8. This code accesses the MyServiceImpl class in Java and returns the results to Flex.
<?xml version=”1.0” encoding=”utf-8”?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
creationComplete=”srv.getMyEntities()”>
<mx:AMFChannel id=”myamf”
uri=”/dzone-server/spring/messagebroker/amf”/>
<mx:ChannelSet id=”channelSet” channels=”{[myamf]}”/>
<mx:RemoteObject id=”srv”
destination=”myService” channelSet=”{channelSet}”/>
<mx:DataGrid dataProvider=”{srv.getMyEntities.lastResult}”/>
</mx:Application>
Listing 8: Final main.mxml source file for accessing the Spring service
The code in Listing 8 sets up the AMFChannel for accessing the Spring service. Note that the destination “flexMyService” is the same as the bean you defined in the application-config. xml Spring configuration file. Also, you might have noticed that none of the Flex code contains anything specific to Spring. The Flex code doesn’t have to change, as the client code has no knowledge of the fact that Spring is being used on the server.
To get the dzone-server to update the deployed web application you may need to right-click the dzone-server project and select Refresh.
With all steps of the tutorial completed, you can start the Tomcat server in Eclipse and access the application at the following URL: http://localhost:8080/dzone-server/main.html
Figure 1: The running application
To allow the Flex application to be launched in Run or Debug mode from Eclipse:
- Right-click the dzone-flex project
- Select Properties, then Flex Build Path
- For the Output Folder URL, type http://localhost:8080/dzone-server/
- Click OK to update the project properties
Now you can right-click the main.mxml file and select Run As > Flex Application or Debug As > Flex Application.
The running application displays the data that was hard coded in the MyServiceImpl Java class, as seen in Figure 1. Now you have a complete sample application using Spring, BlazeDS, and Flex
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}