Spring与ActiveMQ整合

短命女 2022-09-25 14:19 200阅读 0赞

maven依赖:

  1. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-jms</artifactId>
  5. <version>4.3.2.RELEASE</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
  8. <dependency>
  9. <groupId>org.apache.xbean</groupId>
  10. <artifactId>xbean-spring</artifactId>
  11. <version>4.5</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
  14. <dependency>
  15. <groupId>org.apache.activemq</groupId>
  16. <artifactId>activemq-core</artifactId>
  17. <version>5.7.0</version>
  18. </dependency>

activemq.properties内容:

  1. brokerURL=tcp://127.0.0.1:61616
  2. activemq.userName=admin
  3. activemq.password=admin
  4. queue.address=queue
  5. topic.address=topic

spring与activemq整合的文件,由于此文件我是直接在spring与spring MVC整合的文件spring-mvc.xml中引入的,我已经在spring-mvc.xml中全局扫描含有注解的文件了,所以在这不需要扫描有注解的文件了:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
  5. xmlns:jms="http://www.springframework.org/schema/jms"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  10. http://www.springframework.org/schema/jms
  11. http://www.springframework.org/schema/jms/spring-jms-4.2.xsd
  12. http://activemq.apache.org/schema/core
  13. http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd">
  14. <!-- 引入配置文件 -->
  15. <bean id="activemqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  16. <property name="order" value="3" />
  17. <property name="ignoreUnresolvablePlaceholders" value="true" />
  18. <property name="locations">
  19. <list>
  20. <value>classpath:activemq.properties</value>
  21. </list>
  22. </property>
  23. </bean>
  24. <!-- ActiveMQ 连接工厂 -->
  25. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
  26. <!-- <amq:connectionFactory id="amqConnectionFactory"
  27. brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin" /> -->
  28. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  29. <property name="brokerURL" value="${brokerURL}"/>
  30. <property name="userName" value="${activemq.userName}"/>
  31. <property name="password" value="${activemq.password}"/>
  32. </bean>
  33. <!-- Spring 连接工厂 -->
  34. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  35. <!-- Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory。
  36. SingleConnectionFactory对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用。
  37. CachingConnectionFactory继承了SingleConnectionFactory,所以它拥有SingleConnectionFactory的所有功能,
  38. 同时它还新增了缓存功能,它可以缓存Session、MessageProducer和MessageConsumer。-->
  39. <bean id="jmsconnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
  40. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  41. <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
  42. <!-- Session缓存数量 -->
  43. <property name="sessionCacheSize" value="20" />
  44. </bean>
  45. <!-- 定义消息的目的地 -->
  46. <!--点对点消息队列目的地-->
  47. <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  48. <constructor-arg value="${queue.address}"/>
  49. </bean>
  50. <!--发布订阅主题目的地-->
  51. <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  52. <constructor-arg value="${topic.address}"/>
  53. </bean>
  54. <!-- 消息生产者 start -->
  55. <!-- 定义JmsTemplate的Queue类型 -->
  56. <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
  57. <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
  58. <property name="connectionFactory" ref="jmsconnectionFactory"/>
  59. <property name="defaultDestination" ref="queueDestination" />
  60. <!-- 如果不用defaultDestination而是用defaultDestinationName则不需要在前面配置目的地了 -->
  61. <!-- <property name="defaultDestinationName" value="${queue.address}"/> -->
  62. <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
  63. <property name="pubSubDomain" value="false" />
  64. </bean>
  65. <!-- 定义JmsTemplate的Topic类型 -->
  66. <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
  67. <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
  68. <property name="connectionFactory" ref="jmsconnectionFactory"/>
  69. <property name="defaultDestination" ref="topicDestination" />
  70. <!-- 如果不用defaultDestination而是用defaultDestinationName则不需要在前面配置目的地了 -->
  71. <!-- <property name="defaultDestinationName" value="${topic.address}"/> -->
  72. <!-- pub/sub模型(发布/订阅) -->
  73. <property name="pubSubDomain" value="true" />
  74. </bean>
  75. <!--消息生产者 end-->
  76. <!-- 消息消费者 start-->
  77. <!-- 定义Queue监听器 -->
  78. <jms:listener-container destination-type="queue" container-type="default" connection-factory="jmsconnectionFactory" acknowledge="auto">
  79. <!-- 在这里可以有很多的消息消费者,ref后是spring扫描文件时注入的bean -->
  80. <jms:listener destination="${queue.address}" ref="queueConsumer1"/>
  81. <jms:listener destination="${queue.address}" ref="queueConsumer2"/>
  82. </jms:listener-container>
  83. <!-- 定义Topic监听器 -->
  84. <jms:listener-container destination-type="topic" container-type="default" connection-factory="jmsconnectionFactory" acknowledge="auto">
  85. <!-- 在这里可以有很多的消息消费者,ref后是spring扫描文件时注入的bean -->
  86. <jms:listener destination="${topic.address}" ref="topicConsumer1"/>
  87. <jms:listener destination="${topic.address}" ref="topicConsumer2"/>
  88. </jms:listener-container>
  89. <!-- 消息消费者 end -->
  90. </beans>

消息生产者文件的内容:

QueueProducer.java文件:

  1. package com.cn.common.jmsProducer;
  2. import javax.jms.Destination;
  3. import javax.jms.JMSException;
  4. import javax.jms.Message;
  5. import javax.jms.Session;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.jms.core.JmsTemplate;
  9. import org.springframework.jms.core.MessageCreator;
  10. import org.springframework.stereotype.Component;
  11. @Component
  12. public class QueueProducer {
  13. @Autowired
  14. @Qualifier("jmsQueueTemplate")
  15. private JmsTemplate jmsTemplate; //注入配置文件中的bean
  16. @Autowired
  17. @Qualifier("queueDestination")
  18. private Destination destination;
  19. public void send(final String message){
  20. /*jmsTemplate.send(destination, new MessageCreator() {
  21. @Override
  22. public Message createMessage(Session paramSession) throws JMSException {
  23. return paramSession.createTextMessage(message);
  24. }
  25. });*/
  26. jmsTemplate.send(new MessageCreator() {
  27. @Override
  28. public Message createMessage(Session session) throws JMSException {
  29. return session.createTextMessage(message);
  30. }
  31. });;
  32. }
  33. }

TopicProducer.java文件:

  1. package com.cn.common.jmsProducer;
  2. import javax.jms.Destination;
  3. import javax.jms.JMSException;
  4. import javax.jms.Message;
  5. import javax.jms.Session;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.jms.core.JmsTemplate;
  9. import org.springframework.jms.core.MessageCreator;
  10. import org.springframework.stereotype.Component;
  11. @Component
  12. public class TopicProducer {
  13. @Autowired
  14. @Qualifier("jmsTopicTemplate")
  15. private JmsTemplate jmsTemplate; //注入配置文件中的bean
  16. @Autowired
  17. @Qualifier("topicDestination")
  18. private Destination destination;
  19. public void send(final String message){
  20. /*jmsTemplate.send(destination, new MessageCreator() {
  21. @Override
  22. public Message createMessage(Session paramSession) throws JMSException {
  23. return paramSession.createTextMessage(message);
  24. }
  25. });*/
  26. jmsTemplate.send(new MessageCreator() {
  27. @Override
  28. public Message createMessage(Session session) throws JMSException {
  29. return session.createTextMessage(message);
  30. }
  31. });;
  32. }
  33. }

消息消费者文件的内容:

QueueConsumer1.java文件:

  1. package com.cn.common.jmsConsumer;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. public class QueueConsumer1 implements MessageListener{
  9. @Override
  10. public void onMessage(Message message) {
  11. try {
  12. TextMessage text = null;
  13. if(message instanceof TextMessage){
  14. text = (TextMessage) message;
  15. System.out.println("QueueConsumer1接收到消息:"+text.getText());
  16. }
  17. } catch (JMSException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

QueueConsumer2.java文件:

  1. package com.cn.common.jmsConsumer;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. public class QueueConsumer2 implements MessageListener{
  9. @Override
  10. public void onMessage(Message message) {
  11. try {
  12. TextMessage text = null;
  13. if(message instanceof TextMessage){
  14. text = (TextMessage) message;
  15. System.out.println("QueueConsumer2接收到消息:"+text.getText());
  16. }
  17. } catch (JMSException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

TopicConsumer1.java文件:

  1. package com.cn.common.jmsConsumer;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. public class TopicConsumer1 implements MessageListener{
  9. @Override
  10. public void onMessage(Message message) {
  11. try {
  12. TextMessage text = null;
  13. if(message instanceof TextMessage){
  14. text = (TextMessage) message;
  15. System.out.println("TopicConsumer1接收到消息:"+text.getText());
  16. }
  17. } catch (JMSException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

TopicConsumer2.java文件:

  1. package com.cn.common.jmsConsumer;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. public class TopicConsumer2 implements MessageListener{
  9. @Override
  10. public void onMessage(Message message) {
  11. try {
  12. TextMessage text = null;
  13. if(message instanceof TextMessage){
  14. text = (TextMessage) message;
  15. System.out.println("TopicConsumer2接收到消息:"+text.getText());
  16. }
  17. } catch (JMSException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

下面就是在我们需要发送消息的java文件中依赖注入QueueProducer发送点对点消息或TopicProducer发送发布订阅主题消息了。

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring 整合 ActiveMQ

    Spring 是J2EE 最重要的框架,ActiveMQ 是Jms的框架,用于两个程序、系统中的异步通信,两者的用途都挺广泛。上一篇博文介绍的是发布-订阅形式,今次以点-点形式