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----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...
随机推荐
- codevs2370 小机房的树 x
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号 ...
- 【UOJ #46】 【清华集训2014】玄学
题目描述 巨酱有 n 副耳机,他把它们摆成了一列,并且由 1 到n依次编号.每个耳机有一个玄学值,反映了各自的一些不可名状的独特性能.玄学值都是 0 到 m-1 间的整数.在外界的作用下(包括但不限于 ...
- sh_11_字典的其他操作
sh_11_字典的其他操作 xiaoming_dict = {"name": "小明", "age": 18} # 1. 统计键值对数量 p ...
- postfix -- 发件调试
按照教程(https://www.cnblogs.com/huandada/p/10554603.html)搭建好postfix之后,由于收件的邮件运营商的限制,部分邮件不能正常发送,需要更多其他配置 ...
- BootStrap之X-editable插件使用
项目背景 刚加入公司的新项目,主要在做开发工作.由于是新手,本周的工作是配合另外一个同事写前台页面.前台框架是Bootstrap,本文主要介绍一下项目需求的一个功能——表格行内编辑事件. 使用X-ed ...
- 【转载】使用 scikit-learn 进行特征选择
[转载]使用 scikit-learn 进行特征选择 Read more: http://bluewhale.cc/2016-11-25/use-scikit-learn-for-feature-se ...
- HTMLHint 配置文件
HTMLHint 工具可以对 HTML 代码做静态代码检查,从而保证 HTML 代码的规范和质量.HTMLHint 工具内置 23 条规则,建议在 .htmlhintrc 配置文件中将规则尽可能都打开 ...
- vue-cli构建一个工程
使用前,必须要先按照 node:安装node Vue CLI官方文档:https://cli.vuejs.org/zh/ 安装node地址:https://nodejs.org/zh-cn/downl ...
- Boost学习
使用boost function使用类成员函数作为回调函数 #include <iostream> #include <boost/function.hpp> struct M ...
- Python学习笔记:数据的处理
上次的学习中有个split函数,照着head first Python上敲一遍代码: >>> with open('james.txt') as jaf: data=jaf.read ...