Christophe has posted 2 posts at DZone. View Full User Profile

An Introduction to Spring BlazeDS Integration

04.02.2009
| 152701 views |
  • submit to reddit

A Simple Example

To set up a simple Spring BlazeDS Integration application, you will need to:

  1. Configure Spring in web.xml.
  2. Configure the BlazeDS message broker as a Spring-managed bean in the application context configuration file.
  3. Configure your beans and expose them as remote objects in the application context configuration file.

Note: Depending on the DispatcherServlet mapping defined in web.xml, you may also need to adjust the default channel configuration in the BlazeDS services-config.xml file.

In web.xml, you configure the DispatcherServlet to bootstrap the Spring WebApplicationContext as usual. In this simple configuration, you then map all the /messagebroker requests to the DispatcherServlet.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<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/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

</web-app>



In web-application-config.xml, you first configure the BlazeDS message broker as a Spring-managed bean using the simple message-broker tag. This will bootstrap the BlazeDS message broker. When you use the message-broker tag without mapping child elements, all incoming DispatcherServlet requests are mapped to the MessageBroker. You can add mapping child elements if you need more control.

With the message-broker in place, you then configure your Spring beans as usual, and you expose the beans you want to make available for remote access using the remote-service tag.

web-application-config.xml
<?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/>

<bean id="productService" class="my.package.ProductDAO" >
<flex:remote-service />
<constructor-arg ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/sprinflexdemodb/sprinflexdemodb" />
</bean>

</beans>



The above configuration allows a Flex application to invoke the public methods of the productService bean. Here is a minimalist Flex application that populates a DataGrid with a list of products obtained by remotely invoking the findAll() method of the ProductService class.

Note that this is the same code that was used to connect to a plain BlazeDS installation: The client-side code is isolated from the specific server-side implementation.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:RemoteObject id="ro" destination="productService"/>

<mx:DataGrid dataProvider="{ro.findAll.lastResult}" width="100%" height="100%"/>

<mx:Button label="Get Data" click="ro.findAll()"/>

</mx:Application>

 

Integrating Security

A full description of Spring security is outside the scope of this article. You can refer to the Spring documentation for more information. Here is a basic web.xml file configured to use Spring security:

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<display-name>Spring BlazeDS Integration Samples</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/web-application-config.xml
/WEB-INF/config/web-application-security.xml
</param-value>
</context-param>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>



For the purpose of this introduction, a basic authentication provider can be defined in web-application-security.xml as follows:

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

<http auto-config="true" session-fixation-protection="none"/>

<authentication-provider>
<user-service>
<user name="john" password="john" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="guest" password="guest" authorities="ROLE_GUEST" />
</user-service>
</authentication-provider>

</beans:beans>


In your web-application context, you can then set security as usual. The example below specifies that a user needs to be authenticated and belong to the ROLE_USER role to access the "find*" methods.

    <bean id="productService" class="flex.spring.samples.product.ProductDAO" >
<flex:remote-service/>
<constructor-arg ref="dataSource"/>
<security:intercept-methods>
<security:protect method="find*" access="ROLE_USER" />
</security:intercept-methods>
</bean>


If you now run the client application, you will get an Access Denied exception unless you have authenticated outside the Flex application.

The Spring security integration also allows you to authenticate from inside the Flex application using the standard channelSet.login() API. In the following version of the application, I added a simple user interface and the basic infrastructure to authenticate using the ChannelSet API.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="applicationCompleteHandler()">

<mx:Script>
<![CDATA[

import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;

private function applicationCompleteHandler():void
{
var channel:AMFChannel = new AMFChannel("my-amf", "http://localhost:8080/messagebroker/amf");
var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(channel);
ro.channelSet = channelSet;
}

private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error accessing RemoteObject");
}

private function login():void
{
ro.channelSet.login(userId.text, password.text);
}

private function logout():void
{
ro.channelSet.logout();
}

]]>
</mx:Script>

<mx:RemoteObject id="ro" destination="productService" fault="faultHandler(event)"/>

<mx:Form>
<mx:FormItem label="User Id">
<mx:TextInput id="userId"/>
</mx:FormItem>
<mx:FormItem label="Password">
<mx:TextInput id="password" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button label="Login" click="login()"/>
<mx:Button label="Logout" click="logout()"/>
</mx:FormItem>
</mx:Form>

<mx:DataGrid dataProvider="{ro.findAll.lastResult}" width="100%" height="100%"/>

<mx:Button label="Get Data" click="ro.findAll()"/>

</mx:Application>

 

Status and Roadmap

The Spring  BlazeDS integration project is still a work in progress, but the Milestone 2 build that is currently available already provides very valuable features. The next milestone will provide integration with the BlazeDS Message Service. Integration with the data management services in LiveCycle Data Services ES is also on the roadmap.

Summary

The Spring BlazeDS Integration project enables you to seamlessly integrate Flex, BlazeDS, and Spring to build expressive, high-performance, and well-architected Rich Internet Applications. If you are already a Spring developer, you can leverage your existing infrastructure and simply expose your beans for remote access by Flex clients via BlazeDS remoting. If you are an existing Flex and BlazeDS developer, you can use Spring BlazeDS Integration to easily leverage the many powerful features of the Spring IoC container and other components of the Spring Framework.

Resources

 

Published at DZone with permission of its author, Christophe Coenraets.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Roger Marin replied on Thu, 2009/04/02 - 9:56am

Awesome tutorial, any chance we can get the source code?

Jan Vissers (ak... replied on Thu, 2009/04/02 - 12:28pm

Sorry but I don't quite get what this Spring integration offers more that the old SpringFactory way. We used that about half a year ago without any shortcomings. Basically we had one (Spring security enabled) security service exposed via BlazeDS that took care of psuedo single sign-on for us. Every other top-level business module was fronted by a facade, which was exposed via a simple BlazeDS RO Service entry - much like a stateless session bean.


Ryan Heaton replied on Thu, 2009/04/02 - 8:02pm in response to: Jan Vissers (aka Pojo)

Agreed. What's the news here? We've had full Spring+BlazeDS support in Enunciate now for almost a year using the plain-ol' SpringFactory.

ismav ikathry replied on Mon, 2009/04/06 - 11:58pm

Hi christophe,

                        I am a new bee to the spring and flex, I had seen many tutorials and i understood how they work but my doubt is how do i start my own project from scratch. Like is there any basic file so that I can import into Myeclipse and start building my app?Hope you understood my problem.

 by the way thanks for your tutorials which give gave a good overall idea. 

 

Vamsi

Tom Celis replied on Fri, 2009/04/24 - 3:20am

Nice tutorial! But I got stuck on a problem... When I call a remote object from flex (for example: product) I get the following errormessage:

[RPC Fault faultString="[MessagingError message='Destination 'product' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']" faultCode="InvokeFailed" faultDetail="Couldn't establish a connection to 'product'"]

 I've configured it exactly the way you did:

...
  <flex:message-broker />
    <bean id="product" class="product.ProductDaoImpl">
        <flex:remote-service />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
        scope="singleton">
        <property name="configLocation" value="WEB-INF/hibernate.cfg.xml" />
    </bean>
...

Do you have any clue?

 

thx!

Jonny Langford replied on Fri, 2009/05/08 - 5:32am

I got this error when making my remote call:

[RPC Fault faultString="Destination 'mortgageService' not accessible over channel 'my-amf'." faultCode="Server.Processing" faultDetail="null"]

 A bit of digging and turns out you need a default channel defined in the services-config.xml (and you don't need an include for remoting-config.xml). So services-config.xml should start like :

    <services>
        <service-include file-path="proxy-config.xml" />
        <service-include file-path="messaging-config.xml" />
        <default-channels>
            <channel ref="my-amf" />
        </default-channels>
    </services>

Hope this helps..

Dima Ponomarev replied on Fri, 2009/05/29 - 11:49am in response to: Tom Celis

May be you need to define a channel in tag <flex:remote-service />.

See a working example at  http://dima-vp.livejournal.com/573.html.

Note that I'm using a newer version of Spring-Flex library so the tag looks like <flex:remoting-destination channels="my-amf"/>.

Chetan Minajagi replied on Mon, 2009/08/10 - 10:39am

Hi,

I have been to follow the steps outlined here.I have accordingly created a new webapp in the turnkey blazeds tomcat and come up with the required configurations as outlined in this tutorial.However when i startup tomcat and try to access this link like http://localhost:<port>/<my-webapp> .I get a Requested resource is not available.

 

I don't see any errors on the tomcat logs also to help me figure out where i am going wrong.

can  you please put up a downloadable of this tutorial that we can drop into the webapps folder and then follow with the instructions in this tutorial.

 

I have spent more than a day in getting this right but i have no clue abt where/what needs to be done.

 

Regards,

Chetan

Chetan Minajagi replied on Tue, 2009/08/11 - 9:29am in response to: Chetan Minajagi

Hi,

 

I have made some progress now but not entirely met with success though.

I have created a bean id by name 'greetingService' in a spring-config file and referring that file from web-application-context.xml

 

I am now facing this error

 

Error creating bean with name 'org.springframework.flex.remoting.RemotingDestinationExporter#0': Cannot resolve reference to bean 'greetingService' while setting bean property 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'greetingService' is defined

 

Can somebody help me resolve this.

 

Regards,

Chetan

 

 

 

Damaraju Vittal replied on Mon, 2009/08/24 - 6:34am

Hi ,

Create a Default channel in service-configt.xml . Add following code in 

  <default-channels>
            <channel ref="my-amf" />
        </default-channels>   

 

Vittal.

Christopher Grant replied on Mon, 2009/10/19 - 8:39am in response to: Chetan Minajagi

Hey Chetan, I had the same problem for awhile. It's because flash isn't loading the config. I don't know why some people have the issue and some don't be heres how I fixed it. The first way is to simply add an endpoint parameter to your remoteobject tag. The more correct way is to utilize the config file. To tell flash where and which config file to use, make sure your flex compiler has the -compiler.services option added for the value, point it to the physical lcoation of your service-config.xml file. This will tell flex where to lad the service config from and allow your app to locate the server/service Cheers -Christopher http://cgrant.wordpress.com

Martijn Bruinenberg replied on Fri, 2009/10/23 - 2:14am

See my Paddle generator http://paddle.devoorkant.nl.

I implemented a client-server example and a messaging service example as well.

-- Martijn

Logon Smith replied on Fri, 2010/03/05 - 11:47pm

I think the core features of the Spring Framework can be used by any Java application, but there are extensions for building web applications on top of the Java Enterprise platform.I learned these things during my exam preparation. Although the Spring Framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.

Carla Brian replied on Wed, 2012/07/18 - 8:16am

I haven't heard about this one yet. I need more resources on this. I want to learn how to do this one. - Mercy Ministries

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.