Spring与ActiveMQ(JMS)的整合说明

本文详细介绍如何将ActiveMQ消息中间件与Spring框架进行集成,包括配置ActiveMQ Broker、设置连接工厂、定义队列及消息模板等步骤,并通过示例展示了如何创建消息生产者和消费者。

ActiveMQ是完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,也是Apache Geronimo默认的JMS provider。

  ActiveMQ的网站:http://www.activemq.org

  使用ActiveMQ后,可以在普通Web服务器使用JMS功能,不依赖于特定的应用服务器。

1. ActiveMQ 4.1-incubator-SNAPSHOT 与Spring 2.0 集成

ActiveMQ4.1 响应Spring 2.0号召,支持了引入XML Schema namespace的简单配置语法,简化了配置的语句。

1.1 引入ActiveMQ的XSD

在ApplicationContext.xml(Spring的配置文件)中引入ActiveMQ的XML Scheam 配置文件),如下:
(说明:由于ActiveMQ的那个XSD有部分错误,因此使用的是自行修改过的XSD,见”配置ClassPath中的scheam”小节)


  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.   xmlns:amq="http://activemq.org/config/1.0"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.   http://www.springframework.org/schema/beans/spring-beans.xsd   
  6.   http://activemq.org/config/1.0   
  7.   http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd">  
  8. </beans>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.org/config/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd"> </beans>

1.2 配置ClassPath中的schema

在ClassPath 下面建立META-INF\spring.schemas 内容如下


  1. http\://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd=/activemq-core-4.1-incubator-SNAPSHOT.xsd   
  2. 这个spring.schemas是spring自定义scheam的配置文件   
  3. 请注意"http:\://"部分写法  
http\://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd=/activemq-core-4.1-incubator-SNAPSHOT.xsd 这个spring.schemas是spring自定义scheam的配置文件 请注意"http:\://"部分写法

1.3 配置ActiveMQ embedded ActiveMQ Broker


  1. <!-- lets create an embedded ActiveMQ Broker -->  
  2. <amq :broker useJmx="false" persistent="false"></amq>  
  3. <amq :transportConnectors>  
  4.   <amq :transportConnector uri="tcp://localhost:0"/>  
  5. </amq>  
<!-- lets create an embedded ActiveMQ Broker --> <amq :broker useJmx="false" persistent="false"></amq> <amq :transportConnectors> <amq :transportConnector uri="tcp://localhost:0"/> </amq>

当spring初始化时候,ActiveMQ embedded Broker 就会启动了

1.4 配置BookStore MDP

配置ConnectionFactory,由于是embedded 所以URL为:vm://localhost


  1. <!--  ActiveMQ connectionFactory to use  -->  
  2. <amq :connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost"/>  
<!-- ActiveMQ connectionFactory to use --> <amq :connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost"/>

配置Queue名字


  1. <!-- ActiveMQ destinations to use -->  
  2. <amq :queue name="destination" physicalName="org.apache.activemq.spring.Test.spring.embedded"/>  
<!-- ActiveMQ destinations to use --> <amq :queue name="destination" physicalName="org.apache.activemq.spring.Test.spring.embedded"/>

配置JmsTemplate


  1. <!--  Spring JmsTemplate config -->  
  2. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  3. <property name="connectionFactory">  
  4.     <!-- lets wrap in a pool to avoid creating a connection per send -->  
  5.     <bean class="org.springframework.jms.connection.SingleConnectionFactory">  
  6. <property name="targetConnectionFactory">  
  7.         <ref local="jmsConnectionFactory"/>  
  8.       </property>  
  9.     </bean>  
  10.   </property>  
  11.   <!-- custom MessageConverter define -->  
  12. <property name="messageConverter" ref="orderMessageConverter"/>  
  13. </bean>  
<!-- Spring JmsTemplate config --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <!-- lets wrap in a pool to avoid creating a connection per send --> <bean class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory"> <ref local="jmsConnectionFactory"/> </property> </bean> </property> <!-- custom MessageConverter define --> <property name="messageConverter" ref="orderMessageConverter"/> </bean>

配置MDP POJO


  1. <!-- a sample POJO which uses a Spring JmsTemplate -->  
  2. <bean id="orderMessageProducer" class="org.springside.bookstore.components.activemq.OrderMessageProducer">  
  3. <property name="template">  
  4.     <ref bean="jmsTemplate"/>  
  5.   </property>  
  6. <property name="destination">  
  7.     <ref bean="destination"/>  
  8.   </property>  
  9. </bean>  
  10. <!-- this is the Message Driven POJO (MDP) -->  
  11. <!-- define MessageListenerAdapter -->  
  12. <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">  
  13.   <constructor -arg>  
  14.     <bean class="org.springside.bookstore.components.activemq.OrderMessageConsumer">  
  15. <property name="mailService" ref="mailService"></property>  
  16.     </bean>  
  17.   </constructor>  
  18.   <!-- mey be other method -->  
  19. <property name="defaultListenerMethod" value="sendEmail"/>  
  20.   <!-- custom MessageConverter define -->  
  21. <property name="messageConverter" ref="orderMessageConverter"/>  
  22. </bean>  
  23. <!-- and this is the attendant message listener container -->  
  24. <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
  25. <property name="connectionFactory" ref="jmsConnectionFactory"/>  
  26. <property name="destination" ref="destination"/>  
  27. <property name="messageListener" ref="messageListener"/>  
  28. </bean>  
<!-- a sample POJO which uses a Spring JmsTemplate --> <bean id="orderMessageProducer" class="org.springside.bookstore.components.activemq.OrderMessageProducer"> <property name="template"> <ref bean="jmsTemplate"/> </property> <property name="destination"> <ref bean="destination"/> </property> </bean> <!-- this is the Message Driven POJO (MDP) --> <!-- define MessageListenerAdapter --> <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <constructor -arg> <bean class="org.springside.bookstore.components.activemq.OrderMessageConsumer"> <property name="mailService" ref="mailService"></property> </bean> </constructor> <!-- mey be other method --> <property name="defaultListenerMethod" value="sendEmail"/> <!-- custom MessageConverter define --> <property name="messageConverter" ref="orderMessageConverter"/> </bean> <!-- and this is the attendant message listener container --> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory"/> <property name="destination" ref="destination"/> <property name="messageListener" ref="messageListener"/> </bean>

补充说明

采用了自定义的MessageConverter,Producer能够直接发送POJO。 使用了Spring的DefaultMessageListenerContainer,MessageListenerAdapter, Consumer 不用实现MessageListener 接口。
【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理编程实现方法,重点聚焦于直流最优潮流模型的构建求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现学习。此外,文档还列举了大量电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模求解的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值