`
kv0002
  • 浏览: 8696 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

用Annotation方式发布Flex-Bds服务

阅读更多
引用
Spring最近(08-12-16)发布了支持BDS的类库:spring-flex-1.0.0.M1.jar,但还是没有提供Annotation发布方式,于是趁周末无聊的时间,让它简化到底吧(至少在官方的Anno没有发布出来之前)。

这是Spring提供的BDS服务导出配置:
<!-- Bootstraps and exposes the BlazeDS MessageBroker -->  
<bean id="mySpringManagedMessageBroker"  
class="org.springframework.flex.messaging.MessageBrokerFactoryBean" />

<!-- Implementation of ProductService using Spring's SimpleJdbcTemplate -->  
<bean id="productService" class="flex.samples.product.JdbcProductService" >  
    <constructor-arg ref="dataSource"/>  
</bean>  
  
<!-- Expose the productService bean for BlazeDS remoting -->  
<bean id="product"  
    class="org.springframework.flex.messaging.remoting.FlexRemotingServiceExporter">  
    <property name="messageBroker" ref="mySpringManagedMessageBroker"/>  
    <property name="service" ref="productService"/>  
</bean> 

注册请求服务
<!-- Maps request paths at /messagebroker to the BlazeDS MessageBroker -->  
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
    <property name="mappings">  
        <value>  
            /messagebroker/*=mySpringManagedMessageBroker   
        </value>  
    </property>  
</bean>  
  
<!-- Dispatches requests mapped to a MessageBroker -->  
<bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter"/>

服务导出的重点就在FlexRemotingServiceExporter,更具它的原理,重构一个ProxyExporter
public class FlexRemotingServiceProxyExporter implements InitializingBean, ResourceLoaderAware {
	private static final Logger logger = Logger.getLogger(FlexRemotingServiceProxyExporter.class);

	/**
	 * Flex 消息发布器
	 */
	private MessageBroker messageBroker;

	/**
	 * BdsService集合
	 */
	private Map<String, Object> bdsServiceMap = new HashMap<String, Object>();

	private List<RemotingDestination> rdList = new ArrayList<RemotingDestination>();

	public void setMessageBroker(MessageBroker messageBroker) {
		this.messageBroker = messageBroker;
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(messageBroker, "The 'messageBroker' property is required.");

		RemotingService remotingService = (RemotingService) messageBroker
				.getServiceByType(RemotingService.class.getName());
		Assert.notNull(remotingService, "Could not find a proper RemotingService in the Flex MessageBroker.");

		for (String serviceId : bdsServiceMap.keySet()) {
			RemotingDestination destination = (RemotingDestination) remotingService
					.createDestination(serviceId);
			rdList.add(destination);

			FlexFactory factory = new FlexFactoryInstance(bdsServiceMap.get(serviceId));

			destination.setFactory(factory);
			destination.start();

			Assert.isInstanceOf(JavaAdapter.class, destination.getAdapter(),
					"Spring beans exported as a RemotingDestination require a JavaAdapter.");
		}
	}

	public void setResourceLoader(ResourceLoader resourceLoader) {
		if (resourceLoader instanceof XmlWebApplicationContext) {
			XmlWebApplicationContext context = (XmlWebApplicationContext) resourceLoader;
			String[] names = context.getParent().getBeanDefinitionNames();

			for (String name : names) {

				Object bean = context.getParent().getBean(name);
				BdsService bdsService = bean.getClass().getAnnotation(BdsService.class);
				if (bdsService == null) {
					continue;
				}

				bdsServiceMap.put(bdsService.serviceName(), bean);

				logger.info("Export bdsService: " + bdsService.serviceName());
			}
		}
	}

}


FlexFactoryInstance:
public class FlexFactoryInstance implements FlexFactory {
	private Object bean;

	public FlexFactoryInstance(Object bean) {
		this.bean = bean;
	}

	public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
		return new ServiceFactoryInstance(this, id, properties);
	}

	public Object lookup(FactoryInstance instanceInfo) {
		throw new UnsupportedOperationException("FlexFactory.lookup");
	}

	public void initialize(String id, ConfigMap configMap) {
		// No-op			
	}

	public final class ServiceFactoryInstance extends FactoryInstance {
		public ServiceFactoryInstance(FlexFactory factory, String id, ConfigMap properties) {
			super(factory, id, properties);
		}

		@Override
		public Object lookup() {
			return bean;
		}
	}
}


BdsService
@Target(TYPE)
@Retention(RUNTIME)
public @interface BdsService {
	/**
	 * 服务名称
	 * @return
	 */
	String serviceName() default "";
}

大功告成,下面是使用SAMPLE
创建bdsServiceContext.xml,在WEB.XML中定义(注意,该xml只能在WEB.XML中引入,如果跟其他的webController配置放置在一起,在SPRING加载时,因为有限读取Adapter的原则,其他的controller配置——包括Hessian的服务导出都会失效):

web.xml
<servlet>
		<servlet-name>flexDispatcherServlert</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:nbf/config/spring/flexContext.xml
			</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>flexDispatcherServlert</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>

bdsServiceContext.xml
<!--去掉之前的FlexRemotingServiceExporter,替换成下面的Exporter-->
<!--FlexRemotingServiceProxyExporter会自动扫描标注了BdsService的BEAN,并将其注册为BDS服务-->
<bean id="flexServiceExporter"
		class="steel.web.flex.FlexRemotingServiceProxyExporter">
		<property name="messageBroker"
			ref="mySpringManagedMessageBroker" />
	</bean>


(附件中提供Eclipse/Flex项目源代码)
分享到:
评论
2 楼 找不到服务器 2009-03-11  
能不能剖析一下原理???
1 楼 roger_588 2009-03-10  
好!目前正在研究这个东西。

相关推荐

    jakarta.annotation-api-1.3.5-API文档-中文版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    jakarta.annotation-api-1.3.5-API文档-中英对照版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    javax.annotation-api-1.3.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...

    geronimo-annotation_1.0_spec-1.1.1-API文档-中文版.zip

    赠送jar包:geronimo-annotation_1.0_spec-1.1.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,

    javax.annotation-api-1.3.2-API文档-中英对照版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 包含翻译后的API文档:javax.annotation-api-...

    javax.annotation-api-1.3.2 jar包.rar

    在Java编程语言中,...通过深入理解和正确使用`javax.annotation-api-1.3.2.jar` 中提供的注解,开发者可以编写出更加简洁、可维护且易于扩展的代码,特别是在企业级Java应用开发中,注解的运用是不可或缺的一部分。

    javax.annotation-api-1.2-API文档-中英对照版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    javax.annotation-api-1.3.2

    JDK9及以上版本没有javax.annotation-api-***.jar包 ,无法使用注解:@Resource JDK新特性,高版本JDK没有自带的javax(java扩展包)了。或者是使用的JDK不完整。 下载javax.annotation.jar包,导入到lib文件夹下,...

    point-cloud-annotation-tool

    github链接链接:...在windows平台编译完成后的exe和dll文件,可以直接打开使用,并对原项目进行了优化,增加了列表和标注文件的显示功能,标注起来更快,效率更高。

    mybatis-plus-annotation-3.2.0-API文档-中英对照版.zip

    赠送jar包:mybatis-plus-annotation-3.2.0.jar; 赠送原API文档:mybatis-plus-annotation-3.2.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.2.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    point-cloud-annotation-tool win10解压直接运行

    通过了解和熟练使用"point-cloud-annotation-tool",你将能够有效地对点云数据进行标注,为后续的分析和建模提供高质量的数据基础。随着点云技术的发展,掌握这类工具的使用将对你的IT事业大有裨益。

    Other annotation tool related information - eng

    Other annotation tool related information - eng

    mybatis-plus-annotation-3.5.1-API文档-中文版.zip

    赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    javax.annotation-api-1.2.jar

    javax.annotation-api-1.2.jar包,注解,声明,@Resource是做bean的注入使用。

    Edge annotation tool usability testing - ENG.docx

    Edge annotation tool usability testing - ENG.docx

    mybatis-plus-annotation-3.1.0-API文档-中文版.zip

    赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    mybatis-plus-annotation-3.5.1-API文档-中英对照版.zip

    赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    mybatis-plus-annotation-3.1.0-API文档-中英对照版.zip

    赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    javax.annotation-api-1.3.jar

    @Resource注解所在的jar包 与@Autowired不同的是此注解是ByName进行依赖注入的

Global site tag (gtag.js) - Google Analytics