Getting Started With Blazed S
Getting Started With Blazed S
Although it's mentioned upfront that BlazeDS helps connect Flash platform
applications to Java, it's important to define it a bit further. Therefore both its
behavioral and structural aspects are tersely listed in this section.
Behavioral Definition
BlazeDS enables and facilitates:
Pushing data from the server to the client on the server's initiative and
not as a response to a request.
Structural Definition
BlazeDS is a:
With the definition of BlazeDS firmly in place, it's worthwhile to explore Flex
client and Java server integration in the larger context of combining the two
platforms. Look at the next two figures for some insight into the context.
INSTALLING BLAZEDS
Installing BlazeDS is as simple or as complex as deploying a web application
in a Java servlet container. BlazeDS works seamlessly in most mainstream
open source and commercial Java servlet containers, including JBoss, Jetty,
Tomcat, Oracle Weblogic and IBM Websphere.
You can get both compiled and source versions of the software. In addition,
one of the binary versions comes in the form of a turnkey distribution that
includes a configured copy of the Apache Tomcat Servlet container within the
bundle. For beginners, it's convenient, appropriate and advisable to get the
latest release version of the binary turnkey distribution.
CONFIGURING BLAZEDS
Simply put, at the heart of BlazeDS is a Servlet that bootstraps the
infrastructure that intercepts all calls between a Flex client and the BlazeDS
instance. This Servlet, called MessageBrokerServlet, uses artifacts like
channels, endpoints, and adapters to enable proxy, remoting, and messaging
services. A default configuration file, called services-config. xml, which lies in
the WEB-INF/flex folder of the BlazeDS installation, defines these artifacts
and their relationships in the context of the MessageBrokerServlet.
Channels and endpoints connect a Flex client to a BlazeDS server. They are
the primary components that enable communication between these two
entities. Endpoints reside at the BlazeDS end. Flex clients use channels to
connect to these endpoints.
The BlazeDS endpoints are Servlet-based endpoints. Each endpoint defines a
type and format of communication. For example, endpoints exist for simple
AMF data exchange, polling AMF, and AMF data streaming.
Analogously, the Flex client defines a set of channels that vary depending on
the type and format of communication. For example, the HTTPChannel
facilitates communication over non-binary AMF format, AMFX (AMF in XML),
and the AMFChannel enables standard binary AMF-based communication.
Matching endpoints and channels are paired, and that's when a Flex client
and BlazeDS server talk to each other. The binding of channels and
endpoints to their implementation classes and their pairing is done in the
services-config.xml configuration file.
In addition to the endpoints, BlazeDS includes adapters that provide the
critical compatibility between the core BlazeDS Servlet that talks to Flex and
a server-side resource such as a JMS resource, a persistent data store, or an
Object-Relational mapping layer. Adapters are also configured in servicesconfig. xml.
The configuration file services-config.xml, in BlazeDS, is logically split into
four configuration files.
The top level and the first of these four is services-config.xml itself. The other
three are as follows:
remoting-config.xml
proxy-config.xml
messaging-config.xml
With these brief configuration examples in place, let's explore BlazeDS's pullbased (or request-response based) communication abilities. The following
sections include a few more in context configuration illustrations.
PULL-BASED COMMUNICATION
Off the shelf, the Flex framework includes three methods of pull-based
communication and data interchange with external data sources:
HTTP request-response
Web services
COMMUNICATING IN REAL-TIME
Data pushing in a web application context implies the server sending data to
a browser-based client without the client necessarily requesting it.
Long polling can provide near real-time data push by waiting till the response
from the server is ready to be dispatched. It's not scalable though, as it
blocks connections.
Direct connectivity over Real Time Messaging Protocol (RTMP) provides a
better scalable connectivity over long polling but RTMP is not available with
BlazeDS yet! The RTMP specification was proprietary until the beginning of
this year and has most recently been opened up to public. Its inclusion into
future versions of BlazeDS is anticipated.
Usage of Java NIO provides for scalable connections as blocking connections
are replaced by non-blocking asynchronous counterparts. BlazeDS does not
include Java NIO implementations for its communication channels but it's not
JMSAdapters
ActionScriptAdapters
The JMSAdapter connects with JMS server side messaging systems. The
ActionScriptAdapter helps route messages between Flex clients via the
server. Therefore JMSAdapter comes handy when Flex clients are wired up to
send and receive messages to and from enterprise systems that use JMS,
whereas ActionScriptAdapter is useful for building systems like chat
applications to help communicate between two Flex clients.
For effective near real-time messaging use one of the following channels:
AMFChannel/Endpoint with Polling
The client sends a recurring request to the server at a predefined frequency.
Polling is very resource and network intensive.
AMFChannel/Endpoint with Long Polling
The channel issues polls to the server to fetch data but if no data is available
it waits until data arrives for the client or the configured server wait interval
elapses. For high frequency updates this configuration has the overhead of a
poll roundtrip for every pushed message and therefore messages can be
queued between polls. The Servlet API, prior to its current version 3, utilizes
blocking I/O in which case long polling soon exhausts connections as multiple
concurrent clients utilize the channel.
Streaming AMFChannel/Endpoint
This channel opens an HTTP connection between the server and client, over
which the server sends an unending response of messsages. This channel
avoids the overhead of polling and keeps the connection open for the entire
scope of communication between the client and the server. Like AMFChannel
with long polling, the constraints of blocking Servlet I/O can exhaust
connections as multiple concurrent clients utilize the channel.
The Spring BlazeDS integration project, also called Spring Flex, is a joint
effort between SpringSource and Adobe to create a version of BlazeDS that
works seamlessly with the Spring Framework. Prior to the existence of this
project developers relied on custom factories to integrate Spring and
BlazeDS.
BlazeDS is a Java Servlet based web application, so it integrates and works
with the Spring framework facility that addresses the web layer. The core
Spring framework web layer implements an MVC framework and is called
Spring MVC. Spring BlazeDS integrates smoothly with Spring MVC.
A MessageBroker component sits at the heart of BlazeDS. All messages in
BlazeDS are routed through the MessageBroker. In standard BlazeDS
deployments, the MessageBrokerServlet is declared in WEB-INF/web.xml as
follows:
<servlet>
<servlet-name>MessageBrokerServlet</servlet-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>
<init-param>
<param-name>flex.write.path</param-name>
<param-value>/WEB-INF/flex</param-value>
</init-param>
<load-on-startup>1
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
EXTENDING BLAZEDS
BlazeDS exposes a public Java API. You can leverage that API to customize
the behavior of BlazeDS resources.
The flex.messaging.services.ServiceAdapter abstract class sits at the root of
the hierarchy. All built-in adapter classes inherit from the ServiceAdapter
abstract class. Abstract classes not only define a contract like interfaces do
but also define behavior through partial method implementations. The
ServiceAdapter class states the base behavioral characteristics of a BlazeDS
adapter.
You can create custom BlazeDS adapters by extending either the
ServiceAdapter or one of its subclasses like the JavaAdapter or the
MessagingAdapter classes.
Concurrent contracts
Cross-domain utility layer
Functional decomposition
Rules centralization
State repository
Caching
If the accessed data is not changing during the course of its reuse it always
makes sense to cache it. Caching is a timetested way of increasing
performance by avoiding data fetches across the network and using prefetched local data instead.
Resource Pooling
Many of these external systems and libraries, such as messaging
infrastructure, database connections and stateless business services, lend
themselves to pooling. When resources are pooled, they are shared over
multiple clients. BlazeDS has acces to all resource pooling strategies that any
Java EE web application running in an application server has.
Workload distribution
Distributing work optimally between a client and its server is an important
challenge when architecting RIA. BlazeDS remoting services optimally
combine a Flex client and a Java server and allow a developer to distribute
workload across the wire in ways without necessarily imposing the overheads
that loose coupling like XML based interactions over HTTP and web services
do.
CONCLUSION
With no upfront costs and a great set of features BlazeDS is a compelling
piece of software for most serious Rich Internet Application (RIA) stacks that
involve Flex and Java.