`
myrev
  • 浏览: 163223 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

Spring 2.5 + ActiveMQ 5.2.0 Embedded Broker实现

阅读更多
    要的代码实现在上一篇文章,而本篇文章重点是说明 ActiveMQ 5.2.0 Embedded Broker实现。
     另外,打开 activemq-all-5.2.0.jar 修改 spring.schemas文件,在文件最后添加
\://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd = activemq.xsd
这样应用系统启动时就不用到官方网站读 activemq-core-5.2.0.xsd 文件了,省了好多时间~

首先新建 applicationContext-jms.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd"
default-lazy-init="true">
<description>Spring JMS 配置文件</description>

</beans>



然后再加入Embedded Broker
<!-- lets create an embedded ActiveMQ Broker -->
<amq:broker useJmx="false" persistent="false">
  <amq:persistenceAdapter> 
  <amq:amqPersistenceAdapter directory="I:/amq"/> //加入这句,就算服务器关闭,信息也不会丢失。
  </amq:persistenceAdapter> 
  <amq:transportConnectors> 
   <amq:transportConnector uri="vm://localhost:0" /> 
  </amq:transportConnectors> 
</amq:broker>


加入连接
<!-- ActiveMQ connectionFactory to use -->
<amq:connectionFactory id="connectionFactory" brokerURL="vm://localhost"/>


队列
<!-- ActiveMQ destinations to use -->
<amq:queue name="destination" physicalName="myqueue"/>


JmsTemplate用来发送接收,还有转换器InnerMessageConverter,转换对象
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory"></property>
  <property name="defaultDestination" ref="destination"></property>
  <property name="messageConverter" ref="innerMessageConverter"> </property>
</bean>

<bean id="innerMessageConverter"   class="jmu.xmpg.service.jms.InnerMessageConverter"></bean>



MDB和监听容器
    <bean id="messageListener" class="jmu.xmpg.service.jms.MessageMDB"></bean>

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" lazy-init="false">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="destination"></property>
<property name="messageListener" ref="messageListener"></property>
<property name="concurrentConsumers" value="5"></property>
<!-- 0:CACHE_NONE,1:CACHE_CONNECTION,2:CACHE_SESSION,3:CACHE_CONSUMER,4:CACHE_AUTO -->
<property name="cacheLevel" value="0"/>
</bean>


总结:重点在于头部引入的amq空间,另外就是要加入activemq-all-5.2.0.jar 、spring-jms.jar 和 xbean-spring-3.4.jar,这些包在 apache-activemq-5.2.0\lib\optional 下有。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics