注:后面使用SBI替代Spring BlazeDS Integration
1.介紹:
為了使flex客戶(hù)端能夠直接調(diào)用服務(wù)端的spring bean,SBI提供的此種功能,SBI使用DispatchServlet代理轉(zhuǎn)發(fā)MessageBrokerServlet的請(qǐng)求,增加了一些無(wú)用的類(lèi)及相關(guān)配置,
而其實(shí)完成相同的功能,最簡(jiǎn)只需兩個(gè)類(lèi)即可.
2.擴(kuò)展實(shí)現(xiàn)
BlazeDS本身提供一個(gè)AbstractBootstrapService的類(lèi)用于擴(kuò)展,該類(lèi)主要是在BlazeDS初始化時(shí)用于動(dòng)態(tài)創(chuàng)建 services, destinations, and adapters. rapid擴(kuò)展了該類(lèi),用于將spring applicationContext的bean自動(dòng)導(dǎo)出為destination,以供flex客戶(hù)端調(diào)用. SpringRemotingDestinationBootstrapService 自動(dòng)導(dǎo)出包含"@RemoteObject標(biāo)注及以FlexService結(jié)尾"的Spring Bean為RemotingDestination
- public class SpringRemotingDestinationBootstrapService extends AbstractBootstrapService {
-
- public static final String DEFAULT_INCLUDE_END_WITH_BEANS = "FlexService";
-
- private String destChannel;
- private String destSecurityConstraint;
- private String destScope;
- private String destAdapter;
- private String destFactory;
-
- private String serviceId;
-
- private String includeEndsWithBeans;
-
- public void initialize(String id, ConfigMap properties)
- {
- serviceId = properties.getPropertyAsString("service-id", "remoting-service");
-
- destFactory = properties.getPropertyAsString("dest-factory", "spring");
- destAdapter = properties.getProperty("dest-adapter");
- destScope = properties.getProperty("dest-scope");
- destSecurityConstraint = properties.getProperty("dest-security-constraint");
- destChannel = properties.getPropertyAsString("dest-channel","my-amf");
-
- includeEndsWithBeans = properties.getPropertyAsString("includeEndsWithBeans",DEFAULT_INCLUDE_END_WITH_BEANS);
-
- Service remotingService = broker.getService(serviceId);
- if(remotingService == null) {
- throw createServiceException("not found Service with serviceId:"+serviceId);
- }
-
- createSpringDestinations(remotingService);
- }
-
- private ServiceException createServiceException(String message) {
- ServiceException ex = new ServiceException();
- ex.setMessage(message);
- return ex;
- }
-
- private void createSpringDestinations(Service remotingService) {
- WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(broker.getInitServletContext());
- List<String> addedBeanNames = new ArrayList();
- for(String beanName : wac.getBeanDefinitionNames()) {
- Class type = wac.getType(beanName);
-
- boolean isCreateSpringDestination = type.isAnnotationPresent(RemotingObject.class)
- || beanName.endsWith(includeEndsWithBeans)
- || isCreateDestination(beanName,type);
-
- if(isCreateSpringDestination) {
- createSpringDestination(remotingService, beanName);
- addedBeanNames.add(beanName);
- }
- }
- System.out.println("[Auto Export Spring to RemotingDestination],beanNames="+addedBeanNames);
- }
-
- protected boolean isCreateDestination(String beanName,Class type) {
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected void createSpringDestination(Service service, String destinationId) {
- flex.messaging.services.remoting.RemotingDestination destination = (flex.messaging.services.remoting.RemotingDestination)service.createDestination(destinationId);
-
- destination.setSource(destinationId);
- destination.setFactory(destFactory);
-
- if(destAdapter != null)
- destination.createAdapter(destAdapter);
- if(destScope != null)
- destination.setScope(destScope);
- if(destSecurityConstraint != null)
- destination.setSecurityConstraint(destSecurityConstraint);
- if(destChannel != null)
- destination.addChannel(destChannel);
-
- service.addDestination(destination);
- }
-
- }
3.配置
將該類(lèi)與網(wǎng)上的SpringFactory結(jié)合,即可使用. 以下為service-config.xml中關(guān)于自動(dòng)導(dǎo)出的配置.
-
- <factories>
- <factory id="spring" class="cn.org.rapid_framework.flex.messaging.factories.SpringFactory"/>
- </factories>
-
- <services>
- <service-include file-path="remoting-config.xml" />
- <service-include file-path="proxy-config.xml" />
- <service-include file-path="messaging-config.xml" />
-
- <!--
- 自動(dòng)導(dǎo)出包含"@RemoteObject標(biāo)注及以FlexService結(jié)尾"的Spring Bean為RemotingDestination
- FlexService結(jié)尾可以通過(guò)includeEndsWithBeans變量指定
- -->
- <service id="spring-remoting-service" class="cn.org.rapid_framework.flex.messaging.services.SpringRemotingDestinationBootstrapService">
-
- <properties>
- <!--
- <service-id></service-id>
- <dest-factory></dest-factory>
- <dest-adapter></dest-adapter>
- <dest-scope></dest-scope>
- <dest-channel></dest-channel>
- <dest-security-constraint></dest-security-constraint>
- <includeEndsWithBeans></includeEndsWithBeans>
- -->
- </properties>
- </service>
-
- </services>
4.flex客戶(hù)端調(diào)用
-
- this.blogFlexService = new RemoteObject("blogFlexService");
-
-
-
- this.blogFlexService.endpoint = '../messagebroker/amf';
5.結(jié)論
與SBI相比,更加簡(jiǎn)單即可完成相同功能。并且通過(guò)AbstractBootstrapService,你可以很容易的完成將Java Bean, Or EJB3的session bean導(dǎo)出為destinations以供flex客戶(hù)端直接調(diào)用.
具體使用請(qǐng)下載rapidframework并查看flex插件