`
keren
  • 浏览: 1585168 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring + BlazeDS RC1集成指南

    博客分类:
  • Flex
阅读更多
Spring + BlazeDS RC1集成指南
参考:《spring_flex_refenence.pdf》
Spring BlazeDS Integration
Version 1.0.0.RC1 Spring BlazeDS Integration ii
作者: Jeremy Grelle
版本: 1.0.0.RC1

内容:
1. 项目背景和运行环境
2. 用spring来配置 和使用 BlazeDS MessageBroke
3. 为flex RPC声明 bean
4. 结合spring的安全机制来设置 BlazeDS的Destinations
5. 集成BlazeDS 消息服务

【1. Spring BlazeDS Integration Overview】
【1.1. Background】 --背景接收,大家都知道spring的厉害,就不说了。
Spring has always aimed to be agnostic to the client technologies being used to access its core services,
intentionally leaving options open and letting the community drive the demand for any new first-class
integration solutions to be added to the Spring project portfolio. Spring BlazeDS Integration is an answer
to the commmunity demand for a top-level solution for building Spring-powered Rich Internet
Applications using Adobe Flex for the client-side technology.
BlazeDS is an open source project from Adobe that provides the remoting and messaging foundation for
connecting a Flex-based front-end to Java back-end services. Though it has previously been possible to
use BlazeDS to connect to Spring-managed services, it has not been in a way that feels "natural" to a
Spring developer, requiring the extra burden of having to maintain a separate BlazeDS xml configuration.
Spring BlazeDS Integration turns the tables by making the BlazeDS MessageBroker a Spring-managed
object, opening up the pathways to a more extensive integration that follows "the Spring way".
【1.2. 运行软件需求】
Java 5 or higher
Spring 2.5 or higher
Adobe BlazeDS 3.2 or higher
【1.3. Where to get support】
Professional from-the-source support on Spring BlazeDS Integration is available from SpringSource, the
company behind Spring.

【2. 配置 spring + blazeDS 开始】
【2.1. 介绍】
主要的BlazeDS组件是MessageBroker. Flex端传入的http信息将会通过Spring的DispatcherServlet来触发Spring-managed MessageBroker.
也就是说spring用DispatcherServlet来管理BlazeDS的MessageBroker,用过BlazeDS的都知道,在web.xml里面有:
    <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>
        <load-on-startup>3</load-on-startup>
    </servlet>

    现在spring,把这个BlazeDS的MessageBrokerServlet代理了,用org.springframework.web.servlet.DispatcherServlet.
【2.2. 配置spring的 DispatcherServlet】
在web.xml里面,相应的位置加入:
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<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>

【2.3. 在spring的bean工厂里面配置 MessageBroker】
在spring 的XML配置文件头部加入MessageBroker的命名空间namespace。如下:
	<?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">
	...
	</beans>

详细的标签信息在 spring-flex-1.0.xsd里面。
首要的就是要配置 MessageBrokerFactoryBean 在application-config.xml文件里面,在系统启动的时候通过这个 MessageBrokerFactoryBean来启动 BlazeDS的MessageBroker,
这样通过 MessageBrokerHandlerAdapter 和 HandlerMapping(通常是SimpleUrlHandlerMapping)来把flex端的消息路由到spring里面。

在application-config.xml里面,用<flex:message-broker/>标签来配置 MessageBrokerFactoryBean,并且需要指定BlazeDS的 services-config.xml作为参数,举例如下:
通常BlazeDS 配置的XML文件在(/WEB-INF/flex/services-config.xml)。
	<bean id="_messageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean" >
	<property name="servicesConfigPath" value="classpath*:services-config.xml" />
	</bean>


【注意】
Note especially that with the message-broker tag, it is not necessary to assign a custom id to the
MessageBroker, and it is in fact discouraged so that you won't have to continually reference it later. The
only reason you would ever need to provide a custom id is if you were bootstrapping more than one
MessageBroker in the same WebApplicationContext.

【2.4. 把Flex端通过BlazeDS的请求转到 spring 来接管】
为了把请求路由到 spring-managed MessageBroker,需要做3个地方的配置:
1. 在web.xml里面配置 DispatcherServlet
2. 在spring bean工厂里面配置 HandlerMapping
3. 在BlazeDS services-config.xml里面配置 Channel 的 definitions

===>在web.xml里面:
<!-- Map all /messagbroker requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>


===>
用了spring来接管BlazeDS的 messagebroker后,就会产生一个缺省的SimpleUrlHandlerMapping来接收所有通过DispatcherServlet传来的 message请求。
这个缺省的SimpleUrlHandlerMapping将会被覆盖,当有您的系统配置了多个类似这样的mapping累。当你需要自定义HandlerMapping时,
您必须在配置 message-broker时,通过 disable-default-mapping 属性来屏蔽掉那个缺省的 SimpleUrlHandlerMapping。
当您的应用中配置了多个 handlermapping时,mapping-order属性可以指定handlermapping的顺序。
以下的配置是 SimpleUrlHandlerMapping接收所有的flex端的message请求:
<!-- Maps request paths at /* to the BlazeDS MessageBroker -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*=_messageBroker
</value>
</property>
</bean>
<!-- Dispatches requests mapped to a MessageBroker -->
<bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter"/>

===>
Channel definitions设置: 使用BlazeDS,该文件路径在 services-config.xml,
下面的配置正好符合上面那个haddlermapping的策略;
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>


应用系统可以为flex端配置不通的 hadlermapping策略,比如有的handlermapping用来处理 rpc请求,有的用来处理HTTPService请求,
那么这个时候,就要类似在web.xml里面配置servlet mapping一样,为不同的 handlermapping指定不同的pattern,来识别不同的请求。
比如:
pattern=/spring/*  指向 DispatcherServlet
pattern=/messagebroker/*  指向 Spring-managed MessageBroker
应用必须按照如下来修改mapping策略:
	<flex:message-broker>
	<flex:mapping pattern="/messagebroker/*" />
	</flex:message-broker>

然后配置 一个channel definitions如下:
	<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
	<endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf"
	class="flex.messaging.endpoints.AMFEndpoint"/>
	<properties>
	<polling-enabled>false</polling-enabled>
	</properties>
	</channel-definition>

【2.5. flex端使用 Spring-managed Destinations】
Client
Explicit channel definition is a requirement when using dynamic destinations (meaning any destination
that is added programmatically and not defined in the BlazeDS services-config.xml, i.e. the destinations
created by the remoting-destination tag and the various *-message-destination tags).
See Adobe's documentation here for more detail:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/runtimeconfig_5.html#194376
The only way you don't have to explicitly define the ChannelSet on the client is if

1. you are using explicitly defined destinations in services-config.xml (i.e, not dynamic destinations)
AND you compile your flex client against that file
2. your destination is using the application-wide default channel AND you compile your flex client
against that file
Even if you weren't using dynamically created destinations it is debatable whether it is a good idea to ever
compile your client against services-config.xml, thus coupling your client to your server configuration. It
is often desirable to keep your flex client and your server side code as two distinct modules, but
compiling against services-config.xml blurs the lines between those modules.

Our recommendation is that it is generally cleaner to keep the client-side configuration of ChannelSets
explicitly contained within the client module. An excellent way to do this without having to hard-code the
URLs in your client code is to use an ActionScript DI framework such as Spring ActionScript (a Spring
Extensions project, formerly known as Prana).
If you choose to go the route of compiling your client against services-config.xml, note that you can at
least keep the URL information out of the client code by using ServerConfig.getChannel as described in
the referenced BlazeDS documentation.

【2.6. 高级的MessageBroker配置】
The initialization of the MessageBroker by the MessageBrokerFactoryBean logically consists
of two phases:
1. Parsing the BlazeDS XML configuration files and applying their settings to a newly created
MessageBroker
2. Starting the MessageBroker and its services
A special MessageBrokerConfigProcessor callback interface is provided that allows custom
processing to be done on the newly created MessageBroker after each phase, before it is made available
for request processing. This interface is used internally by Spring BlazeDS Integration, but is also
available for general use in advanced programmatic introspection and customization of the
MessageBroker. A custom MessageBrokerConfigProcessor can be configured as a Spring
bean and then registered with the MessageBrokerFactoryBean via the config-processor tag.
For example, given a trivial implementation to log some additional info about the MessageBroker:
package com.example;

import org.springframework.flex.messaging.config.MessageBrokerConfigProcessor;
import flex.messaging.MessageBroker;
import flex.messaging.services.RemotingService;

public class MyDestinationCountingConfigProcessor implements
		MessageBrokerConfigProcessor {
	public MessageBroker processAfterStartup(MessageBroker broker) {
		RemotingService remotingService = (RemotingService) broker
				.getServiceByType(RemotingService.class.getName());
		if (remotingService.isStarted()) {
			System.out.println("The Remoting Service has been started with "
					+ remotingService.getDestinations().size()
					+ " Destinations.");
		}
		return broker;
	}

	public MessageBroker processBeforeStartup(MessageBroker broker) {
		return broker;
	}
}


注册这个Processor:
<flex:message-broker>
<flex:config-processor ref="myConfigProcessor" />
</flex:message-broker>
<bean id="myConfigProcessor" class="com.example.MyDestinationCountingConfigProcessor" />

【2.7. 向Flex端传送后台执行时的exception错误信息】
BlazeDS集成j2ee时,flex端通过rpc httpservice等调用时,当产生遇到错误抛出exception时,会抛出flex.messaging.MessageException.
但是BlazeDS不会把这个exception传到flex端,而只告诉flex端 "Server.Processing" error.
通过实现org.springframework.flex.core.ExceptionTranslator这个接口,并在application-config.xml里面配置后,就可以达到这个目的,举例如下:
<!-- Custom exception translator configured as a Spring bean -->
<bean id="myExceptionTranslator" class="com.foo.app.MyBusinessExceptionTranslator"/>
<flex:message-broker>
<flex:exception-translator ref="myExceptionTranslator"/>
</flex:message-broker>


[未完待续]
3
3
分享到:
评论
2 楼 zhanghaiyangruijie 2013-03-26  
兄弟:
    我想知道一下,如果spring mvc集成flex以后。还能继续使用spring mvc吗。比如:我现在想像以前一样,调用.do之类的请求?
1 楼 skydazzle 2012-05-24  
有flex 和 spring security 集成吗 ?

相关推荐

    spring+blazeDS+flex4 例子

    在“spring+blazeDS+flex4 例子”项目中,我们看到一个完整的集成示例。该项目可以直接运行,所有必要的jar包都已包含。首先,我们需要在Spring配置中定义BlazeDS的通道配置和服务接口。然后,在Flex4的MXML或AS3...

    flex+spring+blazeds在MyEclipse下非常详细的结合配置

    flex+spring+blazeds在MyEclipse下非常详细的结合配置

    flex+spring+blazeds消息推送

    在Spring+BlazeDS+Flex的应用中,Quartz可能被用来定期触发服务器端的任务,例如发送通知、更新数据等,然后通过BlazeDS将这些消息推送到Flex客户端。 至于压缩包内的“说明.txt”,这可能包含了项目的配置步骤、...

    flex+Spring+Hibernate+Cairngorm+BlazeDS+Spring BlazeDS Integration整合框架

    使用flex 4.5 + Spring 3.0 + Hibernate 3.3 + Cairngorm 2.2.1 + BlazeDS 3.3.0 + Spring BlazeDS Integration 1.0.3整合出的一个登录的小demo·

    PureMVC+Flex+BlazeDS+Spring+Hibernate.doc

    标题中的“PureMVC+Flex+BlazeDS+Spring+Hibernate.doc”指的是一项整合了多种技术的Web应用开发方案,这些技术包括PureMVC、Flex、BlazeDS、Spring和Hibernate。这篇文档可能是指导读者如何将这些技术结合在一起...

    flex+spring +BlazeDs+spring+ibatis+Cairngorm整合教程(以前下的,忘了来源了,感激这位大神...)

    标题中的“flex+spring +BlazeDs+spring+ibatis+Cairngorm整合教程”涉及到的是一个基于Adobe Flex前端、Spring后端服务、BlazeDS作为数据通信中间件、Spring框架进行服务管理以及Ibatis作为持久层操作的典型企业级...

    flex4+spring+blazeds整合技术

    《Flex4+Spring+BlazeDS整合技术详解》 在当今的Web开发中,富互联网应用程序(Rich Internet Applications,简称RIA)已经成为一种趋势,而Flex作为Adobe提供的RIA开发框架,结合Spring的优秀企业级服务管理和...

    Flex4.X+BlazeDS+Spring3L实战开发在线书店四

    【标题】"Flex4.X+BlazeDS+Spring3实战开发在线书店四"涉及的核心技术栈是Adobe Flex 4.6、BlazeDS、Spring 3框架以及Java相关的JPA和Hibernate,配合MySQL数据库实现一个在线书店的完整系统。下面将详细阐述这些...

    MyEclipse7.5+flex4+spring3.0.5+struts2.2.1+hibernate3.6.0+blazeds4.0.0.14931完美整合方案

    本方案提供了一种集成化的开发环境,即"MyEclipse7.5+flex4+spring3.0.5+struts2.2.1+hibernate3.6.0+blazeds4.0.0.14931完美整合方案",它将多个流行的技术框架整合在一起,为Web应用程序开发提供了一个强大的平台...

    WEB项目-集成Flex3+BlazeDS3.2+Spring2.5.6集成方式二

    【集成Flex3+BlazeDS3.2+Spring2.5.6的另一种方式】\n\n在上一讲中,我们介绍了如何将Flex3、BlazeDS3.2和Spring2.5.6集成在一起,构建一个强大的WEB项目。本讲我们将探讨集成方式二,该方法采用Spring的侦听配置...

    flex+blazeds+spring

    标题“flex+blazeds+spring”表明我们要探讨的是如何将Flex前端与BlazeDS中继层和Spring后端框架结合起来,实现完整的数据交互和应用程序逻辑。 在Flex与Spring集成的环境中,Flex作为用户界面展示层,负责与用户...

    flex4.5+java+spring+blazeds通信

    ### Flex4.5 + Java + Spring + BlazeDS 通信整合详解 #### 一、概述 在现代企业级应用开发中,前后端分离架构已经成为主流趋势之一。本文将详细介绍如何使用Flex 4.5、Java、Spring框架以及BlazeDS进行前后端之间...

    flex4+blazeds+spring+hibernate集成

    Flex4、BlazeDS、Spring和Hibernate是四个在IT领域中关键的技术组件,它们的集成为构建高效、灵活且功能丰富的Web应用程序提供了强大的基础。下面将详细解释这些技术及其集成方式。 **Flex4** 是Adobe Flex框架的一...

    Flex4.X+BlazeDS+Spring3L实战开发在线书店二

    在本课程中,我们将深入探讨如何使用Flex 4.6、BlazeDS、Spring 3 和其他相关技术来构建一个实际的在线书店应用。Flex 4.6 是Adobe Flex框架的一个重要版本,它提供了强大的富互联网应用程序(RIA)开发工具,使...

    flex+java+spring+hibernate+blazeds整合

    《Flex+Java+Spring+Hibernate+BlazeDS整合详解》 在现代的Web开发中,Flex作为客户端的富互联网应用程序(RIA)框架,与Java、Spring、Hibernate等后端技术结合,可以构建出高效、交互性强的Web应用。本文将详细...

    flex4+blazeDS+spring+ibatis开发教程

    ### Flex4+BlazeDS+Spring+iBatis 开发教程详解 #### 一、富互联网应用(RIA)概念与背景 ##### RIA定义 富互联网应用(Rich Internet Application,简称RIA)是一种新兴的应用程序形式,它结合了客户端与服务器端...

    Myeclipse6.5+flex3+Blazeds+spring+hibernate完美整合

    Myeclipse6.5+flex3+Blazeds+spring+hibernate完美整合,写的非常详细

    flex + spring + BlazeDS + google App JDO 实现一个CRUD.

    在服务器端部署BlazeDS,设置与Spring的集成,确保AMF请求能正确路由到Spring Bean。 4. **Google App Engine设置**: 创建GAE项目,配置JDO环境,定义数据模型类,设置持久化策略。 5. **实现CRUD操作**: ...

    Flex+BlazeDS+Spring+Hibernate

    Flex+BlazeDS+Spring+Hibernate 是一个经典的前端与后端集成方案,广泛应用于构建富互联网应用程序(Rich Internet Applications,RIA)。在这个框架组合中,Flex 作为用户界面层,提供了丰富的交互体验;BlazeDS ...

Global site tag (gtag.js) - Google Analytics