ActiveMQ消息过滤
前言
ActiveMQ提供了一种机制,使用它,消息服务可根据消息选择器中的标准来执行消息过滤。生产者可在消息中放入应用程序特有的属性,而消费者可使用基于这些属性的选择标准来表明对消息是否感兴趣。这就简化了客户端的工作,并避免了向不需要这些消息的消费者传送消息的开销。然而,它也使得处理选择标准的消息服务增加了一些额外开销。
消息选择器是用于MessageConsumer的过滤器,可以用来过滤传入消息的属性和消息头部分(但不过滤消息体),并确定是否将实际消费该消息。消息选择器是一些字符串,它们基于某种语法,而这种语法是SQL-92的子集。可以将消息选择器作为MessageConsumer 创建的一部分。
实现对MapMessage和TextMessage两种消息的过滤条件的设置和消费
Producer
在消息的属性中设置过滤条件
package com.tgb.activemqFilter; import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Producer {
// 单例模式
// 1、连接工厂
private ConnectionFactory connectionFactory;
// 2、连接对象
private Connection connection;
// 3、Session对象
private Session session;
// 4、生产者
private MessageProducer messageProducer;
private Destination destination; public Producer() {
try {
this.connectionFactory = new ActiveMQConnectionFactory("admin",
"admin", "tcp://127.0.0.1:61616");
this.connection = connectionFactory.createConnection();
this.connection.start();
// 设置自动签收模式
this.session = this.connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
this.destination = this.session.createQueue("first");
this.messageProducer = this.session.createProducer(null);
} catch (JMSException e) {
throw new RuntimeException(e);
} } public Session getSession() {
return this.session;
} public void send1(/* String QueueName, Message message */) {
try { Destination destination = this.session.createQueue("first");
MapMessage msg1 = this.session.createMapMessage();
msg1.setString("name", "张三");
msg1.setInt("age", );
// 设置用于消息过滤器的条件
msg1.setStringProperty("name", "张三");
msg1.setIntProperty("age", );
msg1.setStringProperty("color", "bule"); MapMessage msg2 = this.session.createMapMessage();
msg2.setString("name", "李四");
msg2.setInt("age", );
// 设置用于消息过滤器的条件
msg2.setStringProperty("name", "李四");
msg2.setIntProperty("age", );
msg2.setStringProperty("color", "white"); MapMessage msg3 = this.session.createMapMessage();
msg3.setString("name", "赵六");
msg3.setInt("age", );
// 设置用于消息过滤器的条件
msg3.setStringProperty("name", "赵六");
msg3.setIntProperty("age", );
msg3.setStringProperty("color", "black");
// 发送消息
this.messageProducer.send(destination, msg1,
DeliveryMode.NON_PERSISTENT, , * * );
this.messageProducer.send(destination, msg2,
DeliveryMode.NON_PERSISTENT, , * * );
this.messageProducer.send(destination, msg3,
DeliveryMode.NON_PERSISTENT, , * * );
} catch (JMSException e) {
throw new RuntimeException(e);
}
} public void send2() {
try {
Destination destination = this.session.createQueue("first");
TextMessage message = this.session.createTextMessage("我是一个字符串");
message.setIntProperty("age", );
// 发送消息
this.messageProducer.send(destination, message,
DeliveryMode.NON_PERSISTENT, , * * );
} catch (JMSException e) {
throw new RuntimeException(e);
} } public static void main(String[] args) {
Producer producer = new Producer();
producer.send1();
// producer.send2(); }
}
Conmuser
消费消息时,直接在session创建MessageConsumer时,将过滤条件作为参数传入(过滤条件的写法和SQL的写法是很像的)
package com.tgb.activemqFilter; import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory; public class Conmuser {
// 单例模式
// 1、连接工厂
private ConnectionFactory connectionFactory;
// 2、连接对象
private Connection connection;
// 3、Session对象
private Session session;
// 4、生产者
private MessageConsumer messageConsumer;
// 5、目的地址
private Destination destination;
// 消息选择器
public final String SELECTOR_1 = "age > 25";
public final String SELECTOR_2 = " age > 20 and color='black'"; public Conmuser() {
try {
this.connectionFactory = new ActiveMQConnectionFactory("admin",
"admin", "tcp://127.0.0.1:61616");
this.connection = connectionFactory.createConnection();
this.connection.start();
// 设置自动签收模式
this.session = this.connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
this.destination = this.session.createQueue("first");
// 在构造消费者的时候,指定了 消息选择器
// 有选择性的消费消息
this.messageConsumer = this.session.createConsumer(destination,
SELECTOR_1);
} catch (JMSException e) {
throw new RuntimeException(e);
}
} public Session getSession() {
return this.session;
} // 用于监听消息队列的消息
class MyLister implements MessageListener { @Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage ret = (TextMessage) message;
System.out.println("results;" + ret.getText());
}
if (message instanceof MapMessage) {
MapMessage ret = (MapMessage) message;
System.out.println(ret.toString());
System.out.println(ret.getString("name"));
System.out.println(ret.getInt("age"));
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
} } // 用于异步监听消息
public void receiver() {
try {
this.messageConsumer.setMessageListener(new MyLister());
} catch (JMSException e) {
throw new RuntimeException(e);
}
} public static void main(String[] args) {
Conmuser conmuser = new Conmuser();
conmuser.receiver(); }
}
测试

Messages Enqueued: 张三 20 | 李四 25 | 赵六 30
消息过滤条件:age>25
ActiveMQ消息过滤的更多相关文章
- JMS学习四(ActiveMQ消息过滤)
一.消息的选择器 不管是在消息发送端设置消息过期时间还是在接收端设置等待时间,都是对不满足的消息有过滤的作用,那消息选择器就是为过滤消息而生的下面来看看消息选择器: ActiveMQ提供了一种机制,使 ...
- 消息中间件-activemq消息机制和持久化介绍(三)
前面一节简单学习了activemq的使用,我们知道activemq的使用方式非常简单有如下几个步骤: 创建连接工厂 创建连接 创建会话 创建目的地 创建生产者或消费者 生产或消费消息 关闭生产或消费者 ...
- ActiveMQ消息选择器Selector
一.前言 消息发送到Broker,消费者通过Destination可以订阅消费某个特定的通道内的消息.一些特殊情况下,需要消费者对消息过滤下再进行消费,也就是筛选出某些特定消息.ActiveMQ提供了 ...
- C#实现ActiveMQ消息队列
本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...
- 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列
基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...
- Activemq消息类型
Activemq消息类型JMS规范中的消息类型包括TextMessage.MapMessage.ObjectMessage.BytesMessage.和StreamMessage等五种.ActiveM ...
- ActiveMQ消息的可靠性机制(转)
文章转自:http://www.linuxidc.com/Linux/2013-02/79664.htm 1.JMS消息确认机制 JMS消息只有在被确认之后,才认为已经被成功地消费了.消息的成功消费通 ...
- activemq消息队列的使用及应用docker部署常见问题及注意事项
activemq消息队列的使用及应用docker部署常见问题及注意事项 docker用https://hub.docker.com/r/rmohr/activemq/配置在/data/docker/a ...
- JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明
1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...
随机推荐
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- sqli-labs(5)
双查询注入 0x01爱之初了解 在第一次接触到双查询注入时 肯定会有很多问题 在这里我们先了解一下什么叫做 双查询注入 他的语法结构 以及为什么这样构造 答:在此之前,我们理解一下子查询,查询的关键字 ...
- 「CQOI 2014」危桥
题目链接 戳我 \(Solution\) 首先往返\(n\)次等价于走\(2n\)次. 将 \(a_n*2,b_n*2\); 那么我们直接按原图构图,然后: \((S,a_1,a_n),(S,b_1, ...
- dubbo源码阅读笔记-如何引用远程服务,变成invoker
1 消费端如何通过注册中心获取远程服务的invoker? RegistryDirectory.subscribe从注册中心中获取provider的url,通过DubboProtocol的refer方法 ...
- 第三周课程总结&实验报告
课程总结 在这周对Java进行了更深层次的学习,Java的学习也变得越来越困难而有趣,加入了一些新的构造新的方法,还学习了一些简化代码的方式. 面向对象的基本概念 对于面向对象的程序设计有三个主要特征 ...
- 如何使用EF?
方法1: 新建好项目之后 → 右击类库 → 新建项 → ADO.NET实体数据模型(在Visual C#项中) → 从数据库生成 → 选择你要映射的数据库的数据源(将 『是,在连接字符串中包含敏感数据 ...
- centos6.4编译gcc6.4
#!/bin/bash dir=$(pwd) echo $dir cd $dir #wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz .tar ...
- windows+VS+Openjdk12 搭建debug环境
openjdk12 下载地址: http://jdk.java.net/archive/ https://download.java.net/java/GA/jdk12/33/GPL/openjdk- ...
- Navicat1_介绍
https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&shareId=400000000398 ...
- 关于Layui的表格中分页处理
table.render({ elem: '#test' ,height:'full-125' ,url:'data.php' ,cellMinWidth: 80 //全局定义常规单元格的最小宽度,l ...