Pages

Thursday, May 6, 2010

BlazeDS

Share it Please

Adobe Flex, one of the most used tool for developing RIA [Rich Internet Applications]. Flex is not only a User interface provider but a robust framework that handles enterprise integration and communication. BlazeDS is a open source software that provides effective integration for flex and java.
What is BlazeDS?
BlazeDS is open source software that helps in integrating Flex with java.BlazeDs enables message interchange and remote procedure calls thus providing a strong integration between 2 technologies.
What exactly is BlazeDS?
We gave a brief introduction for BlazeDS, but let’s see in detail

BlazeDS is a
  • Java web application that leverages servlet specification.
  • Web application that runs in a servlet container.
  • Set of services that can be managed by jmx agents.
  • A program that intercepts all interaction between a flex application and java.
BlazeDS allows us to
  • Allows to access remote java methods from flex application.
  • Translation of java objects returned from server to action script objects read by flex.
  • Translation of flex action script objects to java objects needed by java server to process.
  • Management of communication channels, connection types between java and flex.
  • Message passing between flex application and java server.
  • Pushing data to the client on server’s initiative and not as a response to a request.
Installing BlazeDS
BlazeDS can be downloaded from http://opensource.adobe.com/wiki/display/blazeds/download+blazeds+3
BlazeDS works on all open sources and commercial servlet containers. BlazeDS includes a turnkey version which contains a internal tomcat server configured which is a best choice for beginners.

What does BlazeDS contains?
BlazeDS contains 3 key services,
  • Remoting Service allows your flex applications to invoke java method deployed on servers
  • Message Service provides us a publish/subscribe infrastructure which allows the flex application to publish messages and subscribe to a message destination. This allows us to develop data push technology.
  • Proxy service allows the flex application to access data available in other domains
Configuring BlazeDS
The heart of blazeDS contains a Servlet which intercepts all calls between flex application and java. The servlet is called MessageBrokerSerlvet which uses channels, endpoints and adapters to enable proxy, remote and message services. An xml file called “services-config.xml” which lies in WEB-INF/flex defines the relationship for the services.
Channels and end point are the important things in allowing a connection between java and flex. A flex client can communicate with channels. These channels will communicate with end point that resides with the BlazeDS.
These BlazeDS end point are servlet based end point and each end point defines a type and format for communication. The type of communication varies from simple AMF data exchange, polling data exchange and streaming data exchange.
The channels and endpoints are paired when a communication is done from flex to BlazeDS.The binding of channels and endpoint is done in “services-config.xml” file.
In addition to channels and endpoints, BlazeDS contains adapters which allow the BlazeDS to send messages from the end point to here where from here they can connect to a back end system or a jms server.

Explaining Channels and Service Points
To send a message the client application uses channels .channels use different formats of message, translate them into a network specific format and send them to the end point which is on server side.
Channels maintain an order of the messages being sent and the same order is applied in receiving the responses. This helps for a proper communication between both parties.
The endpoint takes the messages, translates them from the network specific manner to the form in which java can understand and sends them to messsageBrokerServlet. The servlet then determines where to send the message.
Architecture 

BlazeDS application contains 2 sides
Client - The client side uses a messaging framework provided by the blazeDS in interacting with the server. Channels are used on client side to make this connectivity. Channels are grouped together as channel sets .the main functionality of channel set is to provide connectivity between flex and server. The flex application tried to connect to the first channel in case if it is not success it goes to next channel in the channel set. Channels are the low level ends of client application. They are mainly responsible for connecting flex application to the end point of blazeDS on server side.
Selection of channels in channel set depends on various factors like type of application we are constructing. If we are working with a non-binary data, we will go for Http Channel or if we construct binary data we go for AMFChannel [Action Message Format].
Server - The BlazeDS server is contained in a web application. a flex client makes a request to the channels from where the request goes to end point on the BlazeDS side. From BlazeDS it moves to MessageBorker. From MessageBroker to Service Object [ Remote Service,Http Service] and from service Object to Destination [ Remoting Destination .. ] and then finally to adapters where the adapter objects may connect to Database or perform any operations locally and send back the data to client.
We will have a move over the server side objects
End Point – BlazeDS end points are inside the servlet container.these end points are bootstrapped by the MessageBrokerServlet which is defined the web.xml.in addition to this declaration , we also have a HttpSessionListener is configured so that the blazeDs has a session listener attribute and a binding listener.
Client applications use channels to communicate with endpoints. It is very important that message format should be identical in communication. If a client uses AMF Message, then he connects to AMFChannel which connect AM Endpoint on server side.
MessageBorkerServlet- MessageBrokerServlet extracts the message from the request once the request is processed at end point and identifies the service it is requesting and sends the message to the service.
Services and Destinations – services are the targets for the client applications where as destinations are services configured in some way. Consider when we work on a remoting application , we use a RemoteObject to communicate with the Remoting Service.we need to provide some more details like destinationID that refers to the remoting destination that refers a destination in which we need to perform some actions like destination would be a java object to access methods.
The mapping between client-side Flex components and BlazeDS services is as follows:
·         HTTPService and WebService communicate with HTTPProxyService/HTTPProxyDestination
·         RemoteObject communicates with RemotingService/RemotingDestination
·         Producer and Consumer communicate with MessageService/MessageDestination
You can configure services and their destinations in the services-config.xml file, but it is best practice to put them in separate files as follows:
·         RemotingService configured in the remoting-config.xml file
·         HTTPProxyService configured in the proxy-config.xml file
·         MessageService configured in the messaging-config.xml file
Adapters – the last one in the chain are adapters. When a message arrives at the correct destination, it is passed to an adapter that fulfills the request either locally or by contacting a backend system or a remote server such as a JMS server

Messages
All communication between flex and java are done using messages of different types.flex components use several messages to communicate with blazeDS.there are both action Script version and java version classes in both client side and server side , since messages are serialized and de-serialized on both sides.
In other situations, you do not write code for constructing and sending messages. For example, you simply use a RemoteObject component to call the remote method from the Flex application. The RemoteObject component creates a RemotingMessage to encapsulate the RemoteObject call. In response it receives an AcknowledgeMessage from the server. The AcknowledgeMessage is encapsulated in a ResultEvent in the Flex application.
Sometimes you must create a message to send to the server. For example, you could send a message by creating an AsyncMessage and passing it to a Producer.
Message Patterns: BlazeDS uses 2 types of patterns Request/Reply and Publisher/Subscriber. HTTPService, and WebService components use the request/reply message pattern, in which the Flex component makes a request and receives a reply to that request. Producer and Consumer components use the publish/subscribe message pattern. In this pattern, the Producer publishes a message to a destination defined on the BlazeDS server. All Consumers subscribed to that destination receive the message.


Configuration Files
The services-config.xml file initially references three other configuration files
o    remoting-config.xml along with services-config.xml files are used when we work with remoting.
o    messaging-config.xml along with services-config.xml files are used when we work with messaging.
o    proxy-config.xml along with services-config.xml files are used when we work with proxied HTTP Service or proxied Web Service.

Set up BlazeDS in Tomcat
Step 1 - Download latest release of BlazeDS Turnkey.
Step 2 – Download Tomcat and install
Step 3 - Extract the content in blazeds.war file into a folder called blazeds. Now we have downloaded and have the required files extracted to setup BlazeDS for a web application. Let’s create a web application.
Step 4 -  In your Tomcat installation directory, you will find a folder named webapps. Usually it is at this location on Windows Operating System “C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps”
Create a web application named samplewebapp. You do this by just creating folder named samplewebapp under webapps folder.
Step 5 - Copy all .jar files from blazeds/WEB-INF/lib to samplewebapp/WEB-INF/lib
Step 6 - Copy blazeds/WEB-INF/flex folder to samplewebapp/WEB-INF
This folder (blazeds/WEB-INF/flex) contains BlazeDS configuration files. Use these files to configure Remoting/Messaging/Proxy services.
Step 7- Now we will add Servlet mapping for BlazeDS Servlet named MessageBrokerServlet, so that BlazeDS is invoked when you send request for a Remoting/Messaging/Proxy destination using any of the channels supported.
Copy blazeds/WEB-INF/web.xml to samplewebapp/WEB-INF
If you already have a web.xml configured, then you can just copy the Servlet mapping for MessageBrokerServlet and the session listener. You can either copy the content below or copy it from the blazeds/WEB-INF/web.xml

<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<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>

That’s it you have BlazeDS setup for your web application.
We will see some of the examples of integrating flex and java using BlazeDS in the next article.

No comments :

Post a Comment