spring +ActiveMQ 实战 topic selecter指定接收

queue:点对点模式,一个消息只能由一个消费者接受

topic:一对多,发布/订阅模式,需要消费者都在线(可能会导致信息的丢失)

看了网上很多的文件,但大都是不完整的,或不是自己想要的特异性接受功能,特意研究了一下,总结总结

一,下载并安装ActiveMQ

首先我们到apache官网上下载activeMQ(http://activemq.apache.org/download.html),进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。

二,新建一个maven 项目并导入相关jar包

三,在maven中引入:

  1. <!-- activemq -->
  2. <dependency>
  3. <groupId>org.apache.activemq</groupId>
  4. <artifactId>activemq-core</artifactId>
  5. <version>5.7.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.activemq</groupId>
  9. <artifactId>activemq-pool</artifactId>
  10. <version>5.12.1</version>
  11. </dependency>
  12. </dependencies>

四,spring-active.xml配置

  1. <?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:amq="http://activemq.apache.org/schema/core"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://activemq.apache.org/schema/core
    http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">
  2.  
  3. <context:component-scan base-package="com.demo.test1.activemq" />
    <mvc:annotation-driven />
  4.  
  5. <amq:connectionFactory id="amqConnectionFactory"
    brokerURL="tcp://127.0.0.1:61616"
    userName="admin"
    password="admin" />
  6.  
  7. <!-- 配置JMS连接工厂 -->
    <bean id="connectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="sessionCacheSize" value="100" />
    </bean>
  8.  
  9. <!-- 定义消息队列(Queue) -->
    <bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQTopic">
    <!-- 设置消息队列的名字 -->
    <constructor-arg>
    <value>first-queue</value>
    </constructor-arg>
    </bean>
  10.  
  11. <jms:listener-container destination-type="topic"
    container-type="default" connection-factory="connectionFactory"
    acknowledge="auto">
    <jms:listener destination="first-queue" selector="con=14" ref="queueMessageListener" />
    <jms:listener destination="first-queue" selector="con=15" ref="queueMessageListener2" />
    </jms:listener-container>
  12.  
  13. <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="defaultDestination" ref="demoQueueDestination" />
    <property name="receiveTimeout" value="10000" />
    <!-- true是topic,false是queue,默认是false,此处显示写出false -->
    <property name="pubSubDomain" value="true" />
    <property name="deliveryMode" value="2"/>
    </bean>
  14.  
  15. <bean id="queueMessageListener" class="com.demo.test1.activemq.filter.QueueMessageListener" />
    <bean id="queueMessageListener2" class="com.demo.test1.activemq.filter.QueueMessageListener2" />
  16.  
  17. </beans>

五,springmvc.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  15.  
  16. <!-- 指定Sping组件扫描的基本包路径 -->
  17. <context:component-scan base-package="com.demo.test1" >
  18. <!-- 这里只扫描Controller,不可重复加载Service -->
  19. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  20. </context:component-scan>
  21. <!-- 启用MVC注解 -->
  22. <mvc:annotation-driven />
  23.  
  24. <!-- JSP视图解析器-->
  25. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  26. <property name="prefix" value="/" />
  27. <property name="suffix" value=".jsp" />
  28. <!-- 定义其解析视图的order顺序为1 -->
  29. <property name="order" value="1" />
  30. </bean>
  31. </beans>

六, web.xml配置

七,消息发送接口代码

(1)消息发送接口

  1. import javax.jms.Destination;
  2.  
  3. public interface ProducerService {
  4.  
  5. void sendMessage(Destination destination, final String msg,final int i);
  6.  
  7. }

(2)消息发送接口实现类

  1. import com.demo.test1.activemq.service.ProducerService;
  2. import org.springframework.jms.core.JmsTemplate;
  3. import org.springframework.jms.core.MessageCreator;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import javax.annotation.Resource;
  7. import javax.jms.*;
  8.  
  9. @Service
  10. public class ProducerServiceImpl implements ProducerService {
  11.  
  12. @Resource(name="jmsTemplate")
  13. private JmsTemplate jmsTemplate;
  14.  
  15. @Override
  16. public void sendMessage(Destination destination, final String msg, final int i) {
  17. System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息--------->"+msg);
  18.  
  19. jmsTemplate.send(destination, new MessageCreator() {
  20. public Message createMessage(Session session) throws JMSException {
  21. TextMessage textMessage = session.createTextMessage(msg);
  22. textMessage.setIntProperty("con",i);
  23. return textMessage;
  24. }
  25. });
  26. }
  27.  
  28. }

八,消息监听代码

(1)queueMessageListener

  1. import javax.jms.*;
  2.  
  3. public class QueueMessageListener implements MessageListener {
  4.  
  5. public void onMessage(Message message) {
  6. TextMessage tm = (TextMessage) message;
  7. try {
  8. System.out.println("MyListenner 1 监听到了文本消息:\t"
  9. + tm.getText());
  10. //do something ...
  11. } catch (JMSException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15.  
  16. }

(2)queueMessageListener2

  1. import javax.jms.*;
  2.  
  3. public class QueueMessageListener2 implements MessageListener {
  4. @Override
  5. public void onMessage(Message message) {
  6. TextMessage tm = (TextMessage) message;
  7. try {
  8. System.out.println("MyListenner 2 监听到了文本消息:\t"
  9. + tm.getText());
  10. //do something ...
  11. } catch (JMSException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }

九,控制层

  1. import com.demo.test1.activemq.service.ConsumerService;
  2. import com.demo.test1.activemq.service.ProducerService;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9.  
  10. import javax.annotation.Resource;
  11. import javax.jms.Destination;
  12. import javax.jms.TextMessage;
  13.  
  14. /**
  15. * Created by Administrator on 2017/5/3.
  16. */
  17. @Controller
  18. public class MessageController {
  19. private Logger logger = LoggerFactory.getLogger(MessageController.class);
  20. @Resource(name = "demoQueueDestination")
  21. private Destination destination;
  22.  
  23. //队列消息生产者
  24. @Resource
  25. private ProducerService producer;
  26.  
  27. @RequestMapping(value = "/SendMessage", method = RequestMethod.GET)
  28. @ResponseBody
  29. public void send(String msg,int i) {
  30. logger.info(Thread.currentThread().getName()+"------------开始发送消息");
  31. producer.sendMessage(destination,"消息序号:"+msg,i);
  32. logger.info(Thread.currentThread().getName()+"------------发送完毕");
  33. }
  34. }

十,启动active和tomcat

启动结果:

总结:

由于我们在监听器的配置中配置了selecter属性,因此MyListenner1,只接受con=14的消息,MyListenner2只接受con=15的消息。

    从中可以看到监听的消息队列名称是fist-queue,con值为14的消息,因此值不为14的消息则进入不了该监听消息中。

spring +ActiveMQ 实战 topic selecter指定接收的更多相关文章

  1. spring+activemq实战之配置监听多队列实现不同队列消息消费

    摘选:https://my.oschina.net/u/3613230/blog/1457227 摘要: 最近在项目开发中,需要用到activemq,用的时候,发现在同一个项目中point-to-po ...

  2. ActiveMQ5.0实战三:使用Spring发送,消费topic和queue消息

    实战一 , 实战二 介绍了ActiveMQ的基本概念和配置方式. 本篇将通过一个实例介绍使用spring发送,消费topic, queue类型消息的方法. 不懂topic和queue的google 之 ...

  3. ActiveMQ的作用总结(应用场景及优势)以及springboot+activeMq 实战

      业务场景说明: 消息队列在大型电子商务类网站,如京东.淘宝.去哪儿等网站有着深入的应用, 队列的主要作用是消除高并发访问高峰,加快网站的响应速度. 在不使用消息队列的情况下,用户的请求数据直接写入 ...

  4. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  5. Apache ActiveMQ实战(1)-基本安装配置与消息类型

    ActiveMQ简介 ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用Apache ...

  6. Centos7环境下消息队列之ActiveMQ实战

    Activemq介绍 对于消息的传递有两种类型: 一种是点对点的,即一个生产者和一个消费者一一对应: 另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收. JMS定义了 ...

  7. 使用spring + ActiveMQ 总结

    使用spring + ActiveMQ 总结   摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接 ...

  8. RabbitMQ与Spring的框架整合之Spring Boot实战

    1.RabbitMQ与Spring的框架整合之Spring Boot实战. 首先创建maven项目的RabbitMQ的消息生产者rabbitmq-springboot-provider项目,配置pom ...

  9. Spring Security 实战干货:使用 JWT 认证访问接口

    (转载)原文链接:https://my.oschina.net/10000000000/blog/3127268 1. 前言 欢迎阅读Spring Security 实战干货系列.之前我讲解了如何编写 ...

随机推荐

  1. 基于flink和drools的实时日志处理

    1.背景 日志系统接入的日志种类多.格式复杂多样,主流的有以下几种日志: filebeat采集到的文本日志,格式多样 winbeat采集到的操作系统日志 设备上报到logstash的syslog日志 ...

  2. Spring IoC 循环依赖的处理

    前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 是 ...

  3. Java工具类——日期相关的类

    前言 在日常的开发工作当中,我们经常需要用到日期相关的类(包括日期类已经处理日期的类),所以,我就专门整理了一篇关于日期相关的类,希望可以帮助到大家. 正文 一.日期类介绍 在 Java 里面,操作日 ...

  4. P2220 [HAOI2012]容易题【快速幂】

    题目描述 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下: 有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪些值,我们定 ...

  5. 缺少对公共可见类型或成员的XML注释

    最近突然心血来潮,想清理下代码,结果看到了一堆这样的警告——缺少对公共可见类型或成员“XXX”的 XML 注释: 其实要想取消上面的警告,仅仅需要在项目属性里找到生成页签里的”错误和警告“项,在禁止显 ...

  6. 第二部分用户交互程序开发,通过paramiko记录ssh会话记录

    需求及任务:实现一个给用户登录的界面(通过ssh登到堡垒机上,然后给它展现一个命令行的页面,然后他选择登哪台机器,一选择就连上去且把日志也记录下来). 先在admin创建几条组数据并与用户关联如下图: ...

  7. 【python爬虫实战】使用Selenium webdriver采集山东招考数据

    目录 1.目标 2.Selenium webdriver说明 2.1 为什么使用webdriver 2.2 webdriver支持浏览器 2.3 配置与使用说明 3.采集 3.1 分析网站 3.2 遍 ...

  8. ExecutorsService 中的 submit和 execute的区别

    闲来无事,写点代码练练手.于是就看了下ExecutorService常用的提交任务的方法: <T> Future<T> submit(Callable<T> tas ...

  9. 03-springboot整合elasticsearch-源码初识

        前面两个小节已经知道了spring boot怎么整合es,以及es的简单使用,但是springboot中是怎么和es服务器交互的.我们可以简单了解一下.要看一下源码 在看源码的同时,先要对sp ...

  10. rsync+inotify 备份

    一,服务端安装(备份服务器): #安装rsync cd /usr/local/src/ wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.ta ...