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

spring blazeDS Integration简单介绍

阅读更多

1. 什么是BlazeDS?
简单的说,BlazeDS是一种服务端使用java进行web通信的技术,其可以使开发人员方便地连接到后端分布式数据,推送数据到Flex或AIR应用,为RIA(rich Internet application)提供更好的体验。
很多的RIA(rich Internet application)应用需要更好的数据连接、通信模式。BlazeDS中的远程调用,可以重用服务端已有的java代码,通过配置实现Flash客 户端和服务端的Java方法通信。并且,使用AMF(active message format)二进制通信格式,提高了执行效率,比以往的基于文本格式(如:XML、SOAP)的快了10倍。
BlazeDS的具体功能,可以查看Adobe的wiki:
http://opensource.adobe.com/wiki/display/blazeds/Features
blazeDS下载

2. 什么是Spring BlazeDS Integration?
简单的理解,就是把spring的bean暴露成BlazeDS服务。
在Spring BlazeDS Integration之前,BlazeDS要访问spring的bean,得自己写代码。
通过Spring BlazeDS Integration,当Flash来访问服务端时,spring会把请求转发给Flex message broker。同时,我们把spring中的一些bean暴露成Flex remoting service,那么Flash就能调用到spring中暴露成Flex remoting service的bean。很像spring暴露Hessian。
Spring BlazeDS Integration下载

3. 运行Spring BlazeDS Integration的要求
Java 5 或以上
Spring 2.5 或以上
Adobe BlazeDS 3.2 或以上

4.  如何配置(极大部分参考spring官方文档)
a. 首先,我们得配置Spring DispatcherServlet。这个类在spring-webmvc.jar中。
在web.xml文件中,加入如下servlet:
<!– 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>
我们将把spring bean配置在web-application-config.xml文件里面。

b.接着,我们得把MessageBroker 配置到spring里面。如下:
<!– Bootstraps and exposes the BlazeDS MessageBroker –>
<bean id=”mySpringManagedMessageBroker” class=”org.springframework.flex.messaging.MessageBrokerFactoryBean” />
MessageBrokerFactoryBean在Spring WebApplicationContext中必须声明成一个bean,上面的是最简单的形式。
MessageBrokerFactoryBean将会去寻找BlazeDS的配置文件(默认位置为/WEB-INF/flex/services-config.xml),当然我们可以用servicesConfigPath这个属性去重置这个值,如:
<!– Bootstraps and exposes the BlazeDS MessageBroker –>
<bean id=”mySpringManagedMessageBroker” class=”org.springframework.flex.messaging.MessageBrokerFactoryBean” >
<property name=”servicesConfigPath” value=”classpath:flex/services-config.xml” />
</bean>

c. 把请求映射到MessageBroker。

在web.xml文件中,加入如下配置。所有messagebroker下的请求都会被映射。
<!– 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>

在web-application-config.xml中,加入:
<!– Maps request paths at /* to the BlazeDS MessageBroker –>
<bean class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
<property name=”mappings”>
<value>
/*=mySpringManagedMessageBroker
</value>
</property>
</bean>

<!– Dispatches requests mapped to a MessageBroker –>
<bean class=”org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter”/>

而在spring中,HandlerMapping通过MessageBrokerHandlerAdapter把请求映射到Spring-managed MessageBroker。

注意:在BlazeDS配置文件services-config.xml中,配置的url不能错,下面就有messagebroker。
<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>
具体关于BlazeDS的配置,可以看BlazeDS documentation(http://livedocs.adobe.com/blazeds/1/blazeds_devguide/)。

d. 暴露spring bean为Flex remoting。
配置remoting-config.xml,这个文件的位置可以在services-config.xml中配置。
如:
<services-config>
……
<services>
<service-include file-path=”remoting-config.xml” />
</services>
……
</services-config>

remoting-config.xml的最简单配置如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<service id=”remoting-service”
class=”flex.messaging.services.RemotingService”>

<adapters>
<adapter-definition id=”java-object” class=”flex.messaging.services.remoting.adapters.JavaAdapter” default=”true”/>
</adapters>

<default-channels>
<channel ref=”my-amf”/>
</default-channels>
</service>
(注意:在未来的Spring BlazeDS Integration版中,将不会用到remoting-config.xml这个配置文件。)

使用FlexRemotingServiceExporter
现在假设我们有一个bean
<bean id=”productService” class=”flex.samples.product.ProductServiceImpl” />
要暴露成Flex remoting。
则可以这样做:
<!– Expose the productService bean for BlazeDS remoting –>
<bean id=”product” class=”org.springframework.flex.messaging.remoting.FlexRemotingServiceExporter”>
<property name=”messageBroker” ref=”mySpringManagedMessageBroker”/>
<property name=”service” ref=”productService”/>
</bean>
这里的id,将会默认被作为Flex remoting destination的id,所以在Flash客户端中调用时,假如没有被重置,那么得用spring bean的id的值来作为Flash调用时的 destination的值。

5. 上面就是整个服务器端的配置。附件里面是一个非常简单的demo,包括Flex项目和java web项目(web项目没有jar包,得自己导入)。

分享到:
评论
1 楼 wcy5211678 2012-03-10  
不错 ,附件在哪?

相关推荐

Global site tag (gtag.js) - Google Analytics