badqiu

XPer
随笔 - 46, 文章 - 3, 评论 - 195, 引用 - 0
数据加载中……

与Spring BlazeDS Integration相比,更简单的实现来调用spring bean

注:后面使用SBI替代Spring BlazeDS Integration

 

1.介绍:

为了使flex客户端能够直接调用服务端的spring bean,SBI提供的此种功能,SBI使用DispatchServlet代理转发MessageBrokerServlet的请求,增加了一些无用的类及相关配置,

而其实完成相同的功能,最简只需两个类即可.

 

2.扩展实现

 

BlazeDS本身提供一个AbstractBootstrapService的类用于扩展,该类主要是在BlazeDS初始化时用于动态创建 services, destinations, and adapters. rapid扩展了该类,用于将spring applicationContext的bean自动导出为destination,以供flex客户端调用. SpringRemotingDestinationBootstrapService 自动导出包含"@RemoteObject标注及以FlexService结尾"的Spring Bean为RemotingDestination


Java代码 
  1. public class SpringRemotingDestinationBootstrapService extends AbstractBootstrapService {  
  2.   
  3.     public static final String DEFAULT_INCLUDE_END_WITH_BEANS = "FlexService";  
  4.       
  5.         private String destChannel;  
  6.         private String destSecurityConstraint;  
  7.         private String destScope;  
  8.         private String destAdapter;  
  9.         private String destFactory;  
  10.           
  11.         private String serviceId;  
  12.           
  13.         private String includeEndsWithBeans;  
  14.   
  15.     public void initialize(String id, ConfigMap properties)  
  16.     {  
  17.         serviceId = properties.getPropertyAsString("service-id""remoting-service");  
  18.           
  19.                 destFactory = properties.getPropertyAsString("dest-factory""spring");  
  20.                 destAdapter = properties.getProperty("dest-adapter");  
  21.                 destScope = properties.getProperty("dest-scope");  
  22.                 destSecurityConstraint = properties.getProperty("dest-security-constraint");  
  23.                 destChannel = properties.getPropertyAsString("dest-channel","my-amf");  
  24.                   
  25.                 includeEndsWithBeans = properties.getPropertyAsString("includeEndsWithBeans",DEFAULT_INCLUDE_END_WITH_BEANS);  
  26.                   
  27.                 Service remotingService = broker.getService(serviceId);  
  28.                 if(remotingService == null) {  
  29.                         throw createServiceException("not found Service with serviceId:"+serviceId);  
  30.                 }  
  31.           
  32.         createSpringDestinations(remotingService);  
  33.     }  
  34.   
  35.         private ServiceException createServiceException(String message) {  
  36.                 ServiceException ex = new ServiceException();  
  37.                 ex.setMessage(message);  
  38.                 return ex;  
  39.         }  
  40.   
  41.         private void createSpringDestinations(Service remotingService) {  
  42.                 WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(broker.getInitServletContext());  
  43.                 List<String> addedBeanNames = new ArrayList();  
  44.                 for(String beanName : wac.getBeanDefinitionNames()) {  
  45.                         Class type = wac.getType(beanName);  
  46.                           
  47.                         boolean isCreateSpringDestination = type.isAnnotationPresent(RemotingObject.class)   
  48.                                                                                 || beanName.endsWith(includeEndsWithBeans)   
  49.                                                                                 || isCreateDestination(beanName,type);  
  50.                           
  51.                         if(isCreateSpringDestination) {  
  52.                                 createSpringDestination(remotingService, beanName);  
  53.                                 addedBeanNames.add(beanName);  
  54.                         }  
  55.                 }  
  56.                 System.out.println("[Auto Export Spring to RemotingDestination],beanNames="+addedBeanNames);  
  57.         }  
  58.   
  59.         protected boolean isCreateDestination(String beanName,Class type) {  
  60.                 return false;  
  61.         }  
  62.   
  63.     /* 
  64.     <!-- 
  65.         动态生成的配置内容 
  66.     --> 
  67.     <destination id="sampleVerbose"> 
  68.         <channels> 
  69.             <channel ref="my-secure-amf" /> 
  70.         </channels> 
  71.         <adapter ref="java-object" /> 
  72.         <security> 
  73.             <security-constraint ref="sample-users" /> 
  74.         </security> 
  75.         <properties> 
  76.             <source>my.company.SampleService</source> 
  77.             <scope>session</scope> 
  78.             <factory>myJavaFactory</factory> 
  79.         </properties> 
  80.     </destination>      
  81.      */  
  82.         protected void createSpringDestination(Service service, String destinationId) {  
  83.                 flex.messaging.services.remoting.RemotingDestination destination = (flex.messaging.services.remoting.RemotingDestination)service.createDestination(destinationId);  
  84.           
  85.         destination.setSource(destinationId);  
  86.         destination.setFactory(destFactory);  
  87.           
  88.         if(destAdapter != null)   
  89.                 destination.createAdapter(destAdapter);  
  90.         if(destScope != null)   
  91.                 destination.setScope(destScope);  
  92.         if(destSecurityConstraint != null)  
  93.                 destination.setSecurityConstraint(destSecurityConstraint);  
  94.         if(destChannel != null)  
  95.                 destination.addChannel(destChannel);  
  96.           
  97.         service.addDestination(destination);  
  98.         }  
  99.   
  100. }  
 

3.配置

将该类与网上的SpringFactory结合,即可使用. 以下为service-config.xml中关于自动导出的配置.

 

 

Xml代码 
  1.     <!-- 创建Spring RemotingDestination使用,与spring-remoting-service配合使用 -->  
  2.     <factories>  
  3.             <factory id="spring" class="cn.org.rapid_framework.flex.messaging.factories.SpringFactory"/>  
  4.     </factories>  
  5.       
  6. <services>  
  7.     <service-include file-path="remoting-config.xml" />  
  8.     <service-include file-path="proxy-config.xml" />  
  9.     <service-include file-path="messaging-config.xml" />  
  10.       
  11.     <!--   
  12.             自动导出包含"@RemoteObject标注及以FlexService结尾"的Spring Bean为RemotingDestination  
  13.             FlexService结尾可以通过includeEndsWithBeans变量指定  
  14.     -->  
  15.     <service id="spring-remoting-service" class="cn.org.rapid_framework.flex.messaging.services.SpringRemotingDestinationBootstrapService">  
  16.             <!-- 其它生成的RemotingDestination默认属性 -->  
  17.             <properties>  
  18.                     <!--   
  19.                     <service-id></service-id>  
  20.                     <dest-factory></dest-factory>  
  21.                     <dest-adapter></dest-adapter>  
  22.                     <dest-scope></dest-scope>  
  23.                     <dest-channel></dest-channel>  
  24.                     <dest-security-constraint></dest-security-constraint>  
  25.                     <includeEndsWithBeans></includeEndsWithBeans>  
  26.                      -->  
  27.             </properties>  
  28.     </service>  
  29.               
  30. </services>  

 

4.flex客户端调用

 

Java代码 
  1. //简单示例调用  
  2. this.blogFlexService = new RemoteObject("blogFlexService");  
  3.   
  4. //这里需要指定endpoint,因为是动态的RemotingDestination,而静态的RemotingDestination ,flex编译器会将endpoint编译进源代码.  
  5. //这个也是flex编译器需要指定配置文件而导致使用flex经常会犯的错误之一.  
  6. this.blogFlexService.endpoint = '../messagebroker/amf';  

 

5.结论

 

 

与SBI相比,更加简单即可完成相同功能。并且通过AbstractBootstrapService,你可以很容易的完成将Java Bean, Or EJB3的session bean导出为destinations以供flex客户端直接调用.

具体使用请下载rapidframework并查看flex插件



posted on 2009-10-14 22:04 badqiu 阅读(3010) 评论(2)  编辑  收藏

评论

# re: 与Spring BlazeDS Integration相比,更简单的实现来调用spring bean  回复  更多评论   

blogFlexService 这个是在哪里配置的? 是在Spring 里配置 还是在remoting-config.xml 里面配置???
2009-12-22 14:45 | QQ:345728984

# re: 与Spring BlazeDS Integration相比,更简单的实现来调用spring bean[未登录]  回复  更多评论   

不需要配置,只要是spring容器里面的bean.

然后满足如下其中一个条件即可:

. SpringRemotingDestinationBootstrapService 自动导出包含"@RemoteObject标注及以FlexService结尾"的Spring Bean为RemotingDestination
2009-12-22 15:29 | badqiu

只有注册用户登录后才能发表评论。


网站导航: