`

Blazeds+JMS(ActiveMQ)+Spring实现消息

    博客分类:
  • JMS
阅读更多

转载:http://yunzhongxia.iteye.com/blog/565249

 

 

  1. 为什么要消息?

工行直连要用到深证通数据交换平台(MDEP),MDEP的请求是不同步的。托管行每天会推过来一些头寸报表信息,这些信息要及时的反应给业务人员。另外,文件扫描和预警都要及时的通知给业务人员。因此,平台利用Blazeds的消息。值得说明的一点是:BlazeDS 是一个基于服务器的 Java 远程控制 (remoting) 和 Web 消息传递 (messaging) 技术,以LGPL(Lesser GNU Public License)公共许可证书发布。它能够使得后端的 Java 应用程序和运行在浏览器上的 Adobe Flex 应用程序相互通信。在Java应用服务器上,它以servlet的形式存在, 因此可以在任何标准Java网络应用中运用它。Blazeds的消息机制是采用不断的轮询来实现的,因此性能不怎么好,并发数最大为100,LCDS的消息机制是采用java的NIO,最高并发数是1000,Blazeds和LCDS相比性能还是比较差的,不过***系统的用户数比较少的情况下,Blazeds还是可以的,毕竟,客户都是比较吝啬的,有免费的肯定不想掏钱买昂贵的LCDS。

 

    2. 消息适配器该用哪一个?

 

Java代码 复制代码
  1. <adapters>   
  2.         <adapter-definition id="actionscript"  
  3.             class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"  
  4.             default="true" />   
  5.         <adapter-definition id="jms"  
  6.             class="flex.messaging.services.messaging.adapters.JMSAdapter" />   
  7.     </adapters>  
<adapters>
		<adapter-definition id="actionscript"
			class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
			default="true" />
		<adapter-definition id="jms"
			class="flex.messaging.services.messaging.adapters.JMSAdapter" />
	</adapters>

 

Blazeds提供了两种消息适配器,actionscript和jms.项目一期用的是actionscript适配器,但是有的时候发现文件扫描的消息不能发送到客户端,一直也没有找到原因,关键是Log里面找不到原因。二期应该会有大量的消息要推送到前端,因此打算用jms消息适配器。消息先发送到消息服务器上,flex端订阅消息,如果服务器上有消息在客户端就显示出来。消息服务器很多都是收费的,开源的有ActiveMQ、OPENJMS等。

 

   3.消息服务器该用哪个?

    

 ActiveMQ 是Apache的产品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。在网上查阅了一下关于ActiveMQ 的文档也比较多,而且是开放源代码的,blazeds的官方文档上的例子就是用的ActiveMQ作为消息服务器,因此消息服务器就选它了。

 

   4. 如何搭建环境?

 

      首先要在J2EE服务器(tomcat6)中提供消息服务(JNDI)。在WebRoot/WETA-INFw文件夹下新建一个context.xml.文件的内容如下:

 

Xml代码 复制代码
  1. <Context privileged="true" antiResourceLocking="false"  
  2.     antiJARLocking="false" reloadable="false">  
  3.     <!-- Resourced needed for JMS -->  
  4.     <Resource name="jms/flex/TopicConnectionFactory"  
  5.         type="org.apache.activemq.ActiveMQConnectionFactory"  
  6.         description="JMS Connection Factory"  
  7.         factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  8.         brokerURL="tcp://localhost:61616"  
  9.         brokerName="LocalActiveMQBroker" />  
  10.     <Resource name="jms/topic/flex/simpletopic"  
  11.         type="org.apache.activemq.command.ActiveMQTopic"  
  12.         description="my Topic"  
  13.         factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  14.         physicalName="FlexTopic" />  
  15.     <Resource name="jms/flex/QueueConnectionFactory"  
  16.         type="org.apache.activemq.ActiveMQConnectionFactory"  
  17.         description="JMS Connection Factory"  
  18.         factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  19.         brokerURL="tcp://localhost:61616"  
  20.         brokerName="LocalActiveMQBroker" />  
  21.     <Resource name="jms/queue/flex/simplequeue"  
  22.         type="org.apache.activemq.command.ActiveMQQueue"  
  23.         description="my Queue"  
  24.         factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  25.         physicalName="FlexQueue" />  
  26.     <!--   
  27.     <Valve className="flex.messaging.security.TomcatValve" />  
  28.      -->  
  29. </Context>  
<Context privileged="true" antiResourceLocking="false"
	antiJARLocking="false" reloadable="false">
	<!-- Resourced needed for JMS -->
	<Resource name="jms/flex/TopicConnectionFactory"
		type="org.apache.activemq.ActiveMQConnectionFactory"
		description="JMS Connection Factory"
		factory="org.apache.activemq.jndi.JNDIReferenceFactory"
		brokerURL="tcp://localhost:61616"
		brokerName="LocalActiveMQBroker" />
	<Resource name="jms/topic/flex/simpletopic"
		type="org.apache.activemq.command.ActiveMQTopic"
		description="my Topic"
		factory="org.apache.activemq.jndi.JNDIReferenceFactory"
		physicalName="FlexTopic" />
	<Resource name="jms/flex/QueueConnectionFactory"
		type="org.apache.activemq.ActiveMQConnectionFactory"
		description="JMS Connection Factory"
		factory="org.apache.activemq.jndi.JNDIReferenceFactory"
		brokerURL="tcp://localhost:61616"
		brokerName="LocalActiveMQBroker" />
	<Resource name="jms/queue/flex/simplequeue"
		type="org.apache.activemq.command.ActiveMQQueue"
		description="my Queue"
		factory="org.apache.activemq.jndi.JNDIReferenceFactory"
		physicalName="FlexQueue" />
	<!-- 
	<Valve className="flex.messaging.security.TomcatValve" />
	 -->
</Context>

 

      备注:其实通过上面的配置后,在tomcat6\conf\Catalina\localhost文件下生成了一个utmost.xml文件。关于上面的配置,请继续关注我的博客,我会在以后的写博客的。

 

     修改WEB-INF/flex/messaging-config.xml文件。

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <service id="message-service" class="flex.messaging.services.MessageService">   
  3.     <adapters>   
  4.         <adapter-definition id="actionscript"  
  5.             class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"  
  6.             default="false" />   
  7.         <adapter-definition id="jms"  
  8.             class="flex.messaging.services.messaging.adapters.JMSAdapter"  
  9.             default="true" />   
  10.     </adapters>   
  11.   
  12.     <default-channels>   
  13.         <channel ref="my-streaming-amf" />   
  14.         <channel ref="my-polling-amf" />   
  15.     </default-channels>   
  16.   
  17.     <destination id="scanfile" />   
  18.     <!-- active MQ stock feed -->   
  19.     <destination id="jmsamc"">   
  20.         <adapter ref="jms" />   
  21.         <properties>   
  22.             <!--这里的配置是最关键的,只有durable属性设计为true才能实现持久化订阅-->   
  23.             <server>   
  24.                 <durable>true</durable>   
  25.             </server>   
  26.             <jms>   
  27.                 <connection-factory>   
  28.                     java:comp/env/jms/flex/TopicConnectionFactory   
  29.                 </connection-factory>   
  30.                 <destination-type>Topic</destination-type>   
  31.                 <destination-jndi-name>   
  32.                     java:comp/env/jms/topic/flex/simpletopic   
  33.                 </destination-jndi-name>   
  34.                 <message-type>javax.jms.TextMessage</message-type>   
  35.                 <!-- 持久性 -->   
  36.                 <delivery-mode>PERSISTENT</delivery-mode>   
  37.                 <!-- 优先级  -->   
  38.                 <message-priority>DEFAULT_PRIORITY</message-priority>   
  39.                 <!--应答模式   -->   
  40.                 <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>   
  41.                 <initial-context-environment>   
  42.                     <property>   
  43.                         <name>Context.SECURITY_PRINCIPAL</name>   
  44.                         <value>anonymous</value>   
  45.                     </property>   
  46.                     <property>   
  47.                         <name>Context.SECURITY_CREDENTIALS</name>   
  48.                         <value>anonymous</value>   
  49.                     </property>   
  50.                     <property>   
  51.                         <name>Context.INITIAL_CONTEXT_FACTORY</name>   
  52.                         <value>   
  53.                             org.apache.activemq.jndi.ActiveMQInitialContextFactory   
  54.                         </value>   
  55.                     </property>   
  56.                     <property>   
  57.                         <name>Context.PROVIDER_URL</name>   
  58.                         <value>tcp://192.168.124.114:61616</value>   
  59.                     </property>   
  60.                 </initial-context-environment>   
  61.             </jms>   
  62.         </properties>   
  63.         <channels>   
  64.             <channel ref="my-polling-amf" />   
  65.             <channel ref="my-streaming-amf" />   
  66.         </channels>   
  67.     </destination>   
  68. </service>  
<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service" class="flex.messaging.services.MessageService">
	<adapters>
		<adapter-definition id="actionscript"
			class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
			default="false" />
		<adapter-definition id="jms"
			class="flex.messaging.services.messaging.adapters.JMSAdapter"
			default="true" />
	</adapters>

	<default-channels>
		<channel ref="my-streaming-amf" />
		<channel ref="my-polling-amf" />
	</default-channels>

	<destination id="scanfile" />
	<!-- active MQ stock feed -->
	<destination id="jmsamc"">
		<adapter ref="jms" />
		<properties>
			<!--这里的配置是最关键的,只有durable属性设计为true才能实现持久化订阅-->
			<server>
				<durable>true</durable>
			</server>
			<jms>
				<connection-factory>
					java:comp/env/jms/flex/TopicConnectionFactory
				</connection-factory>
				<destination-type>Topic</destination-type>
				<destination-jndi-name>
					java:comp/env/jms/topic/flex/simpletopic
				</destination-jndi-name>
				<message-type>javax.jms.TextMessage</message-type>
				<!-- 持久性 -->
				<delivery-mode>PERSISTENT</delivery-mode>
				<!-- 优先级  -->
				<message-priority>DEFAULT_PRIORITY</message-priority>
				<!--应答模式   -->
				<acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
				<initial-context-environment>
					<property>
						<name>Context.SECURITY_PRINCIPAL</name>
						<value>anonymous</value>
					</property>
					<property>
						<name>Context.SECURITY_CREDENTIALS</name>
						<value>anonymous</value>
					</property>
					<property>
						<name>Context.INITIAL_CONTEXT_FACTORY</name>
						<value>
							org.apache.activemq.jndi.ActiveMQInitialContextFactory
						</value>
					</property>
					<property>
						<name>Context.PROVIDER_URL</name>
						<value>tcp://192.168.124.114:61616</value>
					</property>
				</initial-context-environment>
			</jms>
		</properties>
		<channels>
			<channel ref="my-polling-amf" />
			<channel ref="my-streaming-amf" />
		</channels>
	</destination>
</service>

 

felx前端订阅消息

Java代码 复制代码
  1. <mx:ChannelSet id="cs">    
  2.         <mx:AMFChannel url="http://192.168.124.114:8099/utmost/messagebroker/amfpolling"/>    
  3.         <mx:AMFChannel url="http://192.168.124.114:8099/utmost/messagebroker/streamingamf"/>    
  4.     </mx:ChannelSet>    
  5.     
  6.     <mx:Consumer id="consumer" destination="jmsamc" channelSet="{cs}"    
  7.          message="messageHandler(event.message)" selector=""/>    
  8.         
<mx:ChannelSet id="cs"> 
        <mx:AMFChannel url="http://192.168.124.114:8099/utmost/messagebroker/amfpolling"/> 
        <mx:AMFChannel url="http://192.168.124.114:8099/utmost/messagebroker/streamingamf"/> 
    </mx:ChannelSet> 
 
    <mx:Consumer id="consumer" destination="jmsamc" channelSet="{cs}" 
         message="messageHandler(event.message)" selector=""/> 
      

 

 

通过以上的配置,基本上实现了Blazeds和JMS的结合。但是还没有解决二期项目的问题,服务器一启动,文件扫描就应该开始;预警是达到一定时间要发送消息给客户端的;ActiveMQ怎么交给Spring管理。下面几篇文章,将会带你慢慢解决这些问题。
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics