Working with BlazeDS Remote Data Services using Flex 4.0
What is BlazeDS?
BlazeDS is a collection of data services that help and simplify building of data driven RIA. They also improve the performance of remote data access operations. BlazeDS supports and enables real time data push and asynchronous collaborative applications. BlazeDS service is deployed on Java EE web/application server.
also read:
The Flex components like Remote Object, Producer, Consumer, HTTPService and WebService interact with a BlazeDS server.
BlazeDS supports with three varieties of key services:
- The Remoting Service: This enables Flex application to directly interact and invoke methods of remote Java objects that are deployed in application server.
- The Message Service: This supports and enables Flex application to interact with publish/subscribe mechanism to publish and subscribe messages to a messaging destination. This enhances the building of application with real time data push and collaboration.
- The Proxy Service: This enables Flex application to implement secure and reliable using cross domain service requests. Flex applications can access services from different domains.
BlazeDS looks after instantiating and managing the remote objects’ life cycle configured in remoting-config.xml.
Working with Remote Objects using BlazeDS
- Step 1: BlazeDS environment setupThe BlazeDS server can be downloaded from the Adobe open source. It works with Tomcat to deploy its services.
- Step 2: Create a Java Web project FlexWebAppUpdate the web.xml with the following:
configuring web.xml
<!-- Http Flex Session by binding listener support --> <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> <!-- Servlet : MessageBroker --> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <display-name>MessageBrokerServlet</display-name> <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class> <!-- loading the services-config.xml --> <init-param> <param-name>services.configuration.file</param-name> <param-value>/WEB-INF/flex/services-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
- Step 3: Copy and paste the required jars from BlazeDS turnkey server to WEB-INF/lib folder
- Step 4: Create a folder called flex and place the xml files like messaging-config, proxy-config, remoting-config and services-config inside downloaded from BlazeDS server.The services-config.xml includes and loads the above mentioned xml files.
- Step 5: Update the remoting-config.xml with following entry:
<!—creating the destination for the StockQuoteService remote Java object <destination id="stockQuote"> <properties> <source>mypack.StockQuoteService</source> </properties> </destination>
- Step 6: Create the Flex client application as below:
<?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" minWidth="955" minHeight="600"> <!-- declaring the remote object in the declaration to connect to BlazeDS destination --> <fx:Declarations> <s:RemoteObject id="myStock" destination="stockQuote"/> </fx:Declarations> <s:Panel title="Stock Quotes" width="600" height="240" horizontalCenter="0" verticalCenter="0" backgroundColor="#7DE4D9" contentBackgroundColor="#BEBB5D" chromeColor="#6CB6B3" rollOverColor="#7792BC"> <s:layout> <s:VerticalLayout/> </s:layout> <!-- connecting the data provider of data grid to the remote onject --> <mx:DataGrid width="100%" dataProvider="{myStock.getStocks.lastResult}" /> <!-- invoking the method of the remote object --> <s:Button label="Get Stocks" click="myStock.getStocks()"/> </s:Panel> </s:Application>
Creating server side Java object
- Step 7: Create the Java application and deploy to the BlazeDS web application
package mypack; import java.util.HashMap; public class StockQuoteService { HashMap stocks[]=new HashMap[5]; public StockQuoteService() { for(int i=0;i<5;i++){ stocks[i]=new HashMap(); stocks[i].put("Stock1",Math.random()*100); stocks[i].put("Stock2",Math.random()*100); stocks[i].put("Stock3",Math.random()*100); } } public HashMap[] getStocks() { return stocks; } }
The .class of above Java class must be placed in the classes/mypack folder.
- Step 8: The J2EE BlazeDS server details need to be configured either when the Flex application is created or update in the Flex server details of flex application as follows:
- Step 9: Restart tomcat
- Step 10 : run the flex applicationThe data grid incorporates the receiving result from the remote object and prepares the grid accordingly.
Conclusion
The BlazeDS server consists of a Java EE web application. A Flex client sends a request thru a channel then the request is directed tiwards to an endpoint/post on the BlazeDS server. Then the request is further routed thru a chain of Java objects including the MessageBroker , a service, a destination, and adapter objects. The adapter checks to get the request served locally or remote server.