5.1. Basic Bootstrapping

BlazeDS is a Java Servlet–based web application, so it will integrate and work with the Spring framework facility that addresses the web layer. Spring framework includes an MVC framework for creating web applications. The MVC framework in Spring is often simply referred to as the Spring MVC. Spring MVC, like every other part of the Spring framework, leverages dependency injection and Aspect-Oriented Programming.

Spring MVC, like most MVC frameworks, provides a separation of concerns along the three important sets of entities, namely models, views, and controllers. Models represent data and state, views provide the interface for user interaction and display, and controllers act as the intermediary between the models and the views. Controllers facilitate model updates based on user input. Spring MVC implements a number of well-known design patterns, including the famous Front Controller pattern.

The Front Controller pattern is based on a central controller intercepting all requests and delegating these requests to appropriate handlers. The DispatcherServlet class serves the purpose of a front controller in Spring MVC. It delegates requests to other controllers. Views are created from responses, and they are served back to the client.

DispatcherServlet extends the HttpServlet. As with any HttpServlet, you can map all requests that match a particular URL pattern to be handled by the DispatcherServlet. Using standard Java EE semantics, you can declare the Servlet and the URL mapping in web.xml as follows:

<web-app>
    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>*/update/*</url-pattern>
    </servlet-mapping>
</web-app>

In this case, all URLs containing the word "update" are handled by the DispatcherServlet. Each DispatcherServlet has an associated WebApplicationContext. Each of these WebApplicationContext instances inherits all the beans defined in the root WebApplicationContext. These global bean definitions can be overridden, and newer beans can be defined for the particular Servlet scope.

To define beans specific to a WebApplicationContext, specify the beans in a configuration file, named <servlet-name>-servlet.xml. This configuration file, by default, would reside in the WEB-INF folder. In the earlier code snippet a Servlet was called sample, so the associated configuration for the Spring MVC components would be named sample-servlet.xml.

Next, let's explore how the BlazeDS MessageBroker can be used with the Spring DispatcherServlet.

5.1.1. Using BlazeDS MessageBroker with the Spring DispatcherServlet

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</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>MessageBrokerServlet</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

This configuration implies the following:

  • A MessageBrokerServlet instance is declared, with WEB-INF/flex/services-config.xml passed in as the configuration file to this Servlet.

  • The flex.messaging.MessageBrokerServlet instance is identified as MessageBrokerServlet.

  • The MessageBrokerServlet loads when the application starts up.

  • All requests to resources whose URL includes the /messagebroker/* pattern are handled by the Servlet identified as MessageBrokerServlet.

If you are familiar with the Java Servlet technology, all of this would be very familiar to you.

In Spring MVC, all incoming requests are handled by the DispatcherServlet. This means you must have a way that all requests coming from the Flex client and intended to be handled by the BlazeDS instance are routed by the DispatcherServlet to the MessageBroker. The Spring BlazeDS project, which I will call "Spring BlazeDS" from now on, addresses this issue and lets you route messages to the MessageBroker from the DispatcherServlet. Therefore, you do not need to configure the MessageBrokerServlet when using Spring BlazeDS.

With Spring BlazeDS, you first need to declare the DispatcherServlet in WEB-INF/web.xml as follows:

<servlet>
    <servlet-name>springblazedstest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/web-application-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

This declares an instance of the DispatcherServlet and defines WEB-INF/config/web-application-config.xml as its configuration file, which is passed as an initialization parameter.

Next, I will configure BlazeDS using the Spring bean configuration semantics. In order to use Spring style configuration, I need to first include the namespace that defines the Spring BlazeDS XML schema. To define beans specific to the DispatcherServlet, which I called springblazedstest in the preceding code snippet, I first create a file called springblazedstest-servlet.xml in the WEB-INF folder. You may recall a discussion on this configuration file-naming convention from an earlier part of this section.

In its simplest form, the MessageBroker can be configured using Spring bean semantics in springblazedstest-servlet.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
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
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

    <flex:message-broker/>

</beans>

The element <flex:message-broker/> uses many default settings, including a default path to the services configuration file. The default path to the BlazeDS configuration file is WEB-INF/flex/services-config.xml, which is normally the case in standard BlazeDS deployments. You can explicitly define an alternative path using the services-config-path attribute of the message-broker element. The configuration would then become:

<flex:message-broker services-config-path="/WEB-INF/flex/services-config.xml"/>

The message-broker element is defined in spring-flex-1.0.xsd. This XML schema is available online at www.springframework.org/schema/flex/spring-flex-1.0.xsd. It is also bundled locally in the distribution jar, that is, org.springframework.flex-1.0.0.RELEASE, which is available in the dist folder of the spring-flex-1.0.0.RELEASE download. The spring-flex download includes the source for the spring BlazeDS project, so you could easily access the XSD directly from the projectsorg.springframework.flexsrcmainjavaorgspringframeworkflexconfigxml folder of the download as well. The XSD has all the element and attribute definitions for the Spring BlazeDS project.

Behind the scenes, a Spring MessageBrokerFactoryBean is configured, which allows the MessageBroker to be instantiated and initialized using the Spring bean configuration. This implies that you could alternatively configure the MessageBroker as follows:

<bean id="_messageBroker"
    class="org.springframework.flex.core.MessageBrokerFactoryBean" >
    <property name="servicesConfigPath" value="classpath*:services-config.xml" />
</bean>

instead of using the terse <flex:message-broker/> element in your configuration file. You may have noticed that an expression classpath*.services-config.xml was provided as the path to the BlazeDS configuration file. This expression states that the services-config.xml should be picked up from the Spring application classpath. MessageBrokerFactoryBean uses Spring's ResourceLoader and that is what makes it possible to specify expressions of the types shown in the configuration file path.

You would like all requests coming from the Flex client to the BlazeDS resources to be handled by the MessageBroker. In order for this to happen, you should create a mapping in the springblazedstest-servlet.xml as follows:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>/*=_messageBroker</value>
    </property>
</bean>

This installs a SimpleUrlHandlerMapping that directs all incoming requests that come to the DispatcherServlet to go to the MessageBroker. The assumption is that the Flex client is the only one communicating with the Spring DispatcherServlet.

Besides the URL handler mapping, you should define a MessageBrokerHandlerAdapter, which the SimpleUrlHandlerMapping uses to map of all incoming requests to the MessageBroker. A MessageBrokerHandlerAdapter can be configured as follows:

<bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter"/>

While the handler mapping and the handler adapter need to be configured to get the MessageBroker to handle all the requests from the Flex client to the BlazeDS resources, your work is extremely simplified because you actually don't need to manually configure these artifacts. The inclusion of a single line <flex:message-broker/> with the appropriate namespace declarations does all this for you.

5.1.2. Serving Flex and Non-Flex Clients

It's possible that on occasion your Spring server side may be serving clients beyond a single Flex application. For example, it could be serving a Flex client and a JavaScript-based client. In such situations, you may want to configure multiple instances of the DispatcherServlet, one for the Flex client and one for the JavaScript client. Further, you may want the core application infrastructure and the cross-cutting elements to be configured in your main application layer. A possible configuration for such architecture is:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/*-context.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>flexclient</servlet-name>

<servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>flexclient</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>javascriptclient</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>javascriptclient</servlet-name>
    <url-pattern>/jsbroker/*</url-pattern>
</servlet-mapping>

In this case, bean definitions specific to the Flex client and the JavaScript client are defined in the flexclient-servlet.xml and javascriptclient-servlet.xml configuration files, respectively.

Sometimes there is a need to create multiple client bean artifacts within a single WebApplicationContext and, therefore, have a single DispatcherServlet. In such cases, you may need to create a hierarchy within the URL patterns. So, URL patterns conforming to /spring/messagebroker/* would be directed to the BlazeDS MessageBroker, and URL patterns conforming to /spring/jsbroker/* would be directed to a JavaScript request broker, which is outside of scope of BlazeDS.

A few additional changes will be required to make such a hierarchical URL pattern work with BlazeDS. First, you will need to configure the mapping attribute in the message-broker element as follows:

<flex:message-broker>
    <flex:mapping pattern="/messagebroker/*" />
</flex:message-broker>

Second, you would need to modify the endpoint URL from http://{server.name}:{server.port}/{context.root}/messagebroker/amf to http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf. This is needed because you want everything with the URL pattern /spring/messagebroker/* to be handled by the BlazeDS MessageBroker in the hierarchical URL architecture.

A MessageBroker can be customized further, but I will skip the advanced configuration for now. In the next section, I use Spring beans as Flex remoting destinations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.78.237