與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
- 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;
- }
- /*
- <!--
- 動態(tài)生成的配置內(nèi)容
- -->
- <destination id="sampleVerbose">
- <channels>
- <channel ref="my-secure-amf" />
- </channels>
- <adapter ref="java-object" />
- <security>
- <security-constraint ref="sample-users" />
- </security>
- <properties>
- <source>my.company.SampleService</source>
- <scope>session</scope>
- <factory>myJavaFactory</factory>
- </properties>
- </destination>
- */
- 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.配置
將該類與網(wǎng)上的SpringFactory結(jié)合,即可使用. 以下為service-config.xml中關于自動導出的配置.
- <!-- 創(chuàng)建Spring RemotingDestination使用,與spring-remoting-service配合使用 -->
- <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結(jié)尾"的Spring Bean為RemotingDestination
- FlexService結(jié)尾可以通過includeEndsWithBeans變量指定
- -->
- <service id="spring-remoting-service" class="cn.org.rapid_framework.flex.messaging.services.SpringRemotingDestinationBootstrapService">
- <!-- 其它生成的RemotingDestination默認屬性 -->
- <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客戶端調(diào)用
- //簡單示例調(diào)用
- this.blogFlexService = new RemoteObject("blogFlexService");
- //這里需要指定endpoint,因為是動態(tài)的RemotingDestination,而靜態(tài)的RemotingDestination ,flex編譯器會將endpoint編譯進源代碼.
- //這個也是flex編譯器需要指定配置文件而導致使用flex經(jīng)常會犯的錯誤之一.
- this.blogFlexService.endpoint = '../messagebroker/amf';
5.結(jié)論
與SBI相比,更加簡單即可完成相同功能。并且通過AbstractBootstrapService,你可以很容易的完成將Java Bean, Or EJB3的session bean導出為destinations以供flex客戶端直接調(diào)用.
具體使用請下載rapidframework并查看flex插件