Topic模式消息发送实例

1、pom引入

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.11</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.activemq</groupId>
  9. <artifactId>activemq-all</artifactId>
  10. <version>5.11.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-jms</artifactId>
  15. <version>4.1.4.RELEASE</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-test</artifactId>
  20. <version>4.1.4.RELEASE</version>
  21. </dependency>

2、生产者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  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.  
  7. <!-- 配置JMS连接工厂 -->
  8. <bean id="providerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  9. <property name="brokerURL" value="failover:(tcp://192.168.147.131:61616)" />
  10. <property name="useAsyncSend" value="true" />
  11. <property name="clientID" value="providerClienctConnect" />
  12. </bean>
  13.  
  14. <!-- 定义消息Destination -->
  15. <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  16. <constructor-arg value="testSpringTopic"/>
  17. </bean>
  18.  
  19. <!-- 消息发送者客户端 -->
  20. <bean id="providerJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  21. <property name="connectionFactory" ref="providerConnectionFactory" />
  22. <property name="defaultDestination" ref="topicDestination" />
  23. <!-- 开启订阅模式 -->
  24. <property name="pubSubDomain" value="true"/>
  25. <property name="receiveTimeout" value="10000" />
  26. <!-- deliveryMode, priority, timeToLive 的开关要生效,必须配置为true,默认false-->
  27. <property name="explicitQosEnabled" value="true"/>
  28. <!-- 发送模式
  29. DeliveryMode.NON_PERSISTENT=1:非持久 ;
  30. DeliveryMode.PERSISTENT=2:持久
  31. -->
  32. <property name="deliveryMode" value="1"/>
  33. </bean>
  34.  
  35. </beans>

生产者程序

  1. package com.mq.spring.topic;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.jms.core.JmsTemplate;
  6. import org.springframework.jms.core.MessageCreator;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import javax.annotation.Resource;
  10. import javax.jms.*;
  11.  
  12. /**
  13. * created on 2015/6/4
  14. * @author dennisit@163.com
  15. * @version 1.0
  16. */
  17. @RunWith(SpringJUnit4ClassRunner.class)
  18. @ContextConfiguration(locations={"classpath:spring-topic.xml"})
  19. public class TopicSender {
  20.  
  21. @Resource(name = "providerJmsTemplate")
  22. private JmsTemplate jmsTemplate;
  23.  
  24. @Test
  25. public void send(){
  26. sendMqMessage(null,"spring activemq topic type message[with listener] !");
  27. }
  28.  
  29. /**
  30. * 说明:发送的时候如果这里没有显示的指定destination.将用spring xml中配置的destination
  31. * @param destination
  32. * @param message
  33. */
  34. public void sendMqMessage(Destination destination, final String message){
  35. if(null == destination){
  36. destination = jmsTemplate.getDefaultDestination();
  37. }
  38. jmsTemplate.send(destination, new MessageCreator() {
  39. public Message createMessage(Session session) throws JMSException {
  40. return session.createTextMessage(message);
  41. }
  42. });
  43. System.out.println("spring send text message...");
  44. }
  45.  
  46. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  47. this.jmsTemplate = jmsTemplate;
  48. }
  49. }

3、消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  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.  
  7. <!-- 配置JMS连接工厂 -->
  8. <bean id="consumerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  9. <property name="brokerURL" value="failover:(tcp://192.168.147.131:61616)" />
  10. <property name="useAsyncSend" value="true" />
  11. <property name="clientID" value="consumerClienctConnect" />
  12. </bean>
  13.  
  14. <!-- 定义消息Destination -->
  15. <bean id="topic1Destination" class="org.apache.activemq.command.ActiveMQTopic">
  16. <constructor-arg value="testSpringTopic1"/>
  17. </bean>
  18.  
  19. <!-- 配置消息消费监听者 -->
  20. <bean id="consumerMessageListener" class="com.mq.spring.topic.ConsumerMessageListener" />
  21.  
  22. <!-- 消息订阅客户端1 -->
  23. <bean id="consumerListenerClient1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  24. <property name="connectionFactory" ref="consumerConnectionFactory" />
  25. <!-- 开启订阅模式 -->
  26. <property name="pubSubDomain" value="true"/>
  27. <property name="destination" ref="topic1Destination" />
  28. <property name="subscriptionDurable" value="true"/>
  29. <!---这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉-->
  30. <property name="clientId" value="consumerClient1"/>
  31. <property name="messageListener" ref="consumerMessageListener" />
  32. <!-- 消息应答方式
  33. Session.AUTO_ACKNOWLEDGE 消息自动签收
  34. Session.CLIENT_ACKNOWLEDGE 客户端调用acknowledge方法手动签收
  35. Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
  36. -->
  37. <property name="sessionAcknowledgeMode" value="1"/>
  38. </bean>
  39.  
  40. <!-- 消息订阅客户端2 -->
  41. <bean id="consumerListenerClient2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  42. <property name="connectionFactory" ref="consumerConnectionFactory" />
  43. <!-- 开启订阅模式 -->
  44. <property name="pubSubDomain" value="true"/>
  45. <property name="destination" ref="topicDestination" />
  46. <property name="subscriptionDurable" value="true"/>
  47. <!---这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉-->
  48. <property name="clientId" value="consumerClient2"/>
  49. <property name="messageListener" ref="consumerMessageListener" />
  50. <!-- 消息应答方式
  51. Session.AUTO_ACKNOWLEDGE 消息自动签收
  52. Session.CLIENT_ACKNOWLEDGE 客户端调用acknowledge方法手动签收
  53. Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
  54. -->
  55. <property name="sessionAcknowledgeMode" value="1"/>
  56. </bean>
  57.  
  58. </beans>

消费者监听代码

  1. package com.mq.spring.topic;
  2. import org.apache.commons.lang.builder.ToStringBuilder;
  3. import javax.jms.JMSException;
  4. import javax.jms.Message;
  5. import javax.jms.MessageListener;
  6. import javax.jms.TextMessage;
  7.  
  8. /**
  9. * created on 2015/6/4
  10. * @author dennisit@163.com
  11. * @version 1.0
  12. */
  13. public class ConsumerMessageListener implements MessageListener{
  14. @Override
  15. public void onMessage(Message message) {
  16. TextMessage tm = (TextMessage) message;
  17. try {
  18. System.out.println("---------消息消费---------");
  19. System.out.println("消息内容:\t" + tm.getText());
  20. System.out.println("消息ID:\t" + tm.getJMSMessageID());
  21. System.out.println("消息Destination:\t" + tm.getJMSDestination());
  22. System.out.println("---------更多信息---------");
  23. System.out.println(ToStringBuilder.reflectionToString(tm));
  24. System.out.println("-------------------------");
  25. } catch (JMSException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

运行结果:

说明:属于学习,网上资料结合个人理解,理解有误的地方,期待指导和建议,共同学习.

转载请注明出处:[http://www.cnblogs.com/dennisit/p/4552686.html]

spring整合activemq发送MQ消息[Topic模式]实例的更多相关文章

  1. spring整合activemq发送MQ消息[queue模式]实例

    queue类型消息 pom依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</ ...

  2. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  3. Spring整合ActiveMQ及多个Queue消息监听的配置

        消息队列(MQ)越来越火,在java开发的项目也属于比较常见的技术,MQ的相关使用也成java开发人员必备的技能.笔者公司采用的MQ是ActiveMQ,且消息都是用的点对点的模式.本文记录了实 ...

  4. Spring整合ActiveMQ实现消息延迟投递和定时投递

    linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...

  5. spring整合ActiveMq

    spring整合ActiveMq: 1:依赖的jar包: 2:spring-activemq.xml    的配置: 代码: <?xml version="1.0" enco ...

  6. 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

    spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...

  7. Java消息队列-Spring整合ActiveMq

    1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...

  8. SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)

    目录 一.前言 二.ActiveMq的下载和使用 三.依赖准备 四.yml文件配置 五.配置Bean 六.创建生产者(Queue+Topic) 七.创建消费者(Topic模式下) 八.测试结果(Top ...

  9. Spring整合ActiveMQ,实现队列主题消息生产消费

    1.引入依赖 pom.xml 1 <!-- activemq --> 2 <dependency> 3 <groupId>org.springframework&l ...

随机推荐

  1. js 格式化日期 ("/Date(1400046388387)/")

    var date = new Date(parseInt(str.replace(/\/Date\((-?\d+)\)\//, '$1'))); var d= date.getFullYear() + ...

  2. iOS报错笔记

    问题一: linker command failed with exit code 1 (use -vto see invocation) 原因:导入了.m的头文件,导致同时有两个一样的.m文件在编译 ...

  3. Java 第十章 类和对象

    类和对象 类与对象的关系是什么? 答 :类是具有相同属性和方法的一组对象的集合. 类是抽象的,对象是具体的:类是对象的模版,对象是类的实例. 定义一个类的语法是什么? public class 类名{ ...

  4. Jade之Case

    Case jade中的case类似js中的switch语句. 当前一个when中无语句的时候,将一直往下直至遇到一个有语句的when才跳出. jade: - var friends = 10 case ...

  5. 【动态规划】bzoj1669 [Usaco2006 Oct]Hungry Cows饥饿的奶牛

    #include<cstdio> #include<algorithm> using namespace std; int n,a[5001],b[5001],en; int ...

  6. 今天开始着手原来Office系统的重构

    原来系统架构Spring+Hibernate+Struts+springsecurity 拟改成 Spring+SpringMVC+MyBatis/JDBC+Shiro 同时优化前端的CSS和JQue ...

  7. ElasticSearch介绍 【未完成】

    ElasticSearch应用于搜索是一个不错的选择,虽有Lucene,但ELK的搜索方便. http://joelabrahamsson.com/elasticsearch-101/ 一.下载 ht ...

  8. Static Constructors

    A static constructor is used to initialize any static data, or to perform a particular action that n ...

  9. IIS Connection Timeout vs httpRuntime executionTimeout

    IIS Connection Timeout specifies how long, in seconds, should the code wait before timing out from t ...

  10. ceph placement group状态总结

    一.归置组状态 1. Creating 创建存储池时,它会创建指定数量的归置组.ceph 在创建一或多个归置组时会显示 creating;创建完后,在其归置组的 Acting Set 里的 OSD 将 ...