badqiu

          XPer
          隨筆 - 46, 文章 - 3, 評論 - 195, 引用 - 0
          數(shù)據(jù)加載中……

          與Spring BlazeDS Integration相比,更簡單的實現(xiàn)來調(diào)用spring bean

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

           

          1.介紹:

          為了使flex客戶端能夠直接調(diào)用服務端的spring bean,SBI提供的此種功能,SBI使用DispatchServlet代理轉(zhuǎn)發(fā)MessageBrokerServlet的請求,增加了一些無用的類及相關配置,

          而其實完成相同的功能,最簡只需兩個類即可.

           

          2.擴展實現(xiàn)

           

          BlazeDS本身提供一個AbstractBootstrapService的類用于擴展,該類主要是在BlazeDS初始化時用于動態(tài)創(chuàng)建 services, destinations, and adapters. rapid擴展了該類,用于將spring applicationContext的bean自動導出為destination,以供flex客戶端調(diào)用. SpringRemotingDestinationBootstrapService 自動導出包含"@RemoteObject標注及以FlexService結(jié)尾"的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.         動態(tài)生成的配置內(nèi)容 
          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.配置

          將該類與網(wǎng)上的SpringFactory結(jié)合,即可使用. 以下為service-config.xml中關于自動導出的配置.

           

           

          Xml代碼 
          1.     <!-- 創(chuàng)建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結(jié)尾"的Spring Bean為RemotingDestination  
          13.             FlexService結(jié)尾可以通過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客戶端調(diào)用

           

          Java代碼 
          1. //簡單示例調(diào)用  
          2. this.blogFlexService = new RemoteObject("blogFlexService");  
          3.   
          4. //這里需要指定endpoint,因為是動態(tài)的RemotingDestination,而靜態(tài)的RemotingDestination ,flex編譯器會將endpoint編譯進源代碼.  
          5. //這個也是flex編譯器需要指定配置文件而導致使用flex經(jīng)常會犯的錯誤之一.  
          6. this.blogFlexService.endpoint = '../messagebroker/amf';  

           

          5.結(jié)論

           

           

          與SBI相比,更加簡單即可完成相同功能。并且通過AbstractBootstrapService,你可以很容易的完成將Java Bean, Or EJB3的session bean導出為destinations以供flex客戶端直接調(diào)用.

          具體使用請下載rapidframework并查看flex插件



          posted on 2009-10-14 22:04 badqiu 閱讀(3027) 評論(2)  編輯  收藏

          評論

          # re: 與Spring BlazeDS Integration相比,更簡單的實現(xiàn)來調(diào)用spring bean  回復  更多評論   

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

          # re: 與Spring BlazeDS Integration相比,更簡單的實現(xiàn)來調(diào)用spring bean[未登錄]  回復  更多評論   

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

          然后滿足如下其中一個條件即可:

          . SpringRemotingDestinationBootstrapService 自動導出包含"@RemoteObject標注及以FlexService結(jié)尾"的Spring Bean為RemotingDestination
          2009-12-22 15:29 | badqiu

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 化州市| 都兰县| 图木舒克市| 南部县| 建宁县| 鄢陵县| 赫章县| 龙陵县| 屯门区| 新津县| 泸溪县| 荆州市| 枣庄市| 泗水县| 墨江| 古蔺县| 武陟县| 唐山市| 手游| 潢川县| 泰州市| 镇巴县| 阳城县| 盐池县| 根河市| 夹江县| 红河县| 阳高县| 天长市| 秭归县| 武冈市| 开阳县| 崇左市| 五指山市| 崇州市| 深圳市| 嘉鱼县| 伊川县| 潜江市| 漠河县| 盘锦市|