注:后面使用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
- 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.配置
將該類與網上的SpringFactory結合,即可使用. 以下為service-config.xml中關于自動導出的配置.
-
- <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" />
-
- <!--
- 自動導出包含"@RemoteObject標注及以FlexService結尾"的Spring Bean為RemotingDestination
- FlexService結尾可以通過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客戶端調用
-
- this.blogFlexService = new RemoteObject("blogFlexService");
-
-
-
- this.blogFlexService.endpoint = '../messagebroker/amf';
5.結論
與SBI相比,更加簡單即可完成相同功能。并且通過AbstractBootstrapService,你可以很容易的完成將Java Bean, Or EJB3的session bean導出為destinations以供flex客戶端直接調用.
具體使用請下載rapidframework并查看flex插件