【 JMS Selectors 】

JMS Selectors用于在订阅中,基于消息属性对消息进行过滤。

以下是个Selectors的例子:Java代码

consumer = session.createConsumer(destination, "JMSType = 'car' AND weight > 2500");

在JMS Selectors表达式中,可以使用IN、NOT IN、LIKE等,

例如: LIKE '12%3' ('123' true,'12993' true,'1234' false) LIKE 'l_se' ('lose' true,'loose' false) LIKE '\_%' ESCAPE '\' ('_foo' true,'foo' false)

需要注意的是,JMS Selectors表达式中的日期和时间需要使用标准的long型毫秒值。

另外表达式中的属性不会自动进行类型转换,例如:Java代码myMessage.setStringProperty("NumberOfOrders", "2");"NumberOfOrders > 1" 求值结果是false。

关于JMS Selectors的详细文档请参考javax.jms.Message的javadoc。 上一小节介绍的Message Groups虽然可以保证具有相同message group的消息被唯一的consumer顺序处理,但是却不能确定被哪个consumer处理。在某些情况下,Message Groups可以和JMS Selector一起工作,例如: 设想有三个consumers分别是A、B和C。你可以在producer中为消息设置三个message groups分别是"A"、"B"和"C"。然后令consumer A使用"JMXGroupID = 'A'"作为selector。B和C也同理。这样就可以保证message group A的消息只被consumer A处理。需要注意的是,这种做法有以下缺点:• producer必须知道当前正在运行的consumers,也就是说producer和consumer被耦合到一起。• 如果某个consumer失效,那么应该被这个consumer消费的消息将会一直被积压在broker上。

【消息过滤实例】

【生产者】

package test2.activemq.demo;

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; public Producer(){
try{
this.connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://localhost:61616");
this.connection= this.connectionFactory.createConnection();
this.connection.start();
this.session = this.connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
this.messageProducer = this.session.createProducer(null);
}catch(JMSException e){
e.printStackTrace();
}
} public Session getSession(){
return this.session;
} public void sendMapMessage(){
try{
Destination destination = this.session.createQueue("bestQueue");
MapMessage msg1 = this.session.createMapMessage();
msg1.setString("name", "Higgin");
msg1.setString("age", ""); //这个格式不属于过滤的格式
msg1.setIntProperty("money", ); //这个格式次才是能过滤的格式
msg1.setStringProperty("color", "blue"); MapMessage msg2 = this.session.createMapMessage();
msg2.setString("name", "Higgin2");
msg2.setString("age", "");
msg2.setIntProperty("money", );
msg2.setStringProperty("color", "yellow"); MapMessage msg3 = this.session.createMapMessage();
msg3.setString("name", "Zhansan");
msg3.setString("age", "");
msg3.setIntProperty("money", );
msg3.setStringProperty("color", "red"); MapMessage msg4 = this.session.createMapMessage();
msg4.setString("name", "Lisi");
msg4.setString("age", "");
msg4.setIntProperty("money", );
msg4.setStringProperty("color", "blue"); MapMessage msg5 = this.session.createMapMessage();
msg5.setString("name", "Wangwu");
msg5.setString("age", "");
msg5.setIntProperty("money", );
msg5.setStringProperty("color", "blue"); this.messageProducer.send(destination,msg1,DeliveryMode.NON_PERSISTENT,,**10L); //消息有效时间10分钟
this.messageProducer.send(destination,msg2,DeliveryMode.NON_PERSISTENT,,**10L);
this.messageProducer.send(destination,msg3,DeliveryMode.NON_PERSISTENT,,**10L);
this.messageProducer.send(destination,msg4,DeliveryMode.NON_PERSISTENT,,**10L);
this.messageProducer.send(destination,msg5,DeliveryMode.NON_PERSISTENT,,**10L); }catch(JMSException e){
e.printStackTrace();
}
} public void sendTextMessage(){
try{
Destination destination = this.session.createQueue("bestQueue");
TextMessage textMessage = this.session.createTextMessage("text Message Ha Ha Ha");
this.messageProducer.send(destination,textMessage,DeliveryMode.NON_PERSISTENT,,**10L);
}catch(JMSException e){
e.printStackTrace();
}
} public static void main(String[] args) {
Producer p = new Producer();
p.sendMapMessage();
} }

【消费者】

package test2.activemq.demo;

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.MessageProducer;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class Consumer {
//错误的条件,因为producer是使用setString()方法添加的属性,只有setIntProperty或setStringProperty这种格式的才能满足过滤条件
public final String SELECTOR_0 = "age = 25"; public final String SELECTOR_1 = "color = 'blue'"; public final String SELECTOR_2 = "color = 'blue' AND money > 100 "; public final String SELECTOR_3 = "receiver = 'A'"; //1.连接工厂
private ConnectionFactory connectionFactory;
//2.连接对象
private Connection connection;
//3.Session对象
private Session session;
//4.生产者
private MessageConsumer messageConsumer;
//5.队列
private Destination destination; public Consumer(){
try{
this.connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://localhost:61616");
this.connection= this.connectionFactory.createConnection();
this.connection.start();
this.session = this.connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
this.destination = this.session.createQueue("bestQueue");
//创建消费者的时候,除了增加队列信息,还可以添加一个过滤器
this.messageConsumer = this.session.createConsumer(this.destination,this.SELECTOR_1);
}catch(JMSException e){
e.printStackTrace();
}
} public void receiver(){
try{
this.messageConsumer.setMessageListener(new Listener());
}catch(JMSException e){
e.printStackTrace();
}
} class Listener implements MessageListener{ @Override
public void onMessage(Message msg) {
try{
if(msg instanceof MapMessage){
MapMessage result =(MapMessage)msg;
System.out.println(result.toString());
System.out.println("被消费的消息:"+result.getString("name")+"--"+result.getString("age")+"--"+result.getStringProperty("color"));
}
}catch(Exception e){
e.printStackTrace();
}
}
} public static void main(String[] args) {
Consumer c = new Consumer();
c.receiver();
}
}

【消费者SELECT_1的运行结果】

05_ActiveMQ的selectors的更多相关文章

  1. DOM扩展-Selectors API(选择符 API)、元素遍历

    DOM扩展 对DOM的两个主要扩展是SelectorsAPI(选择符API)和HTML5 SelectorsAPI(选择符API)是由W3C发起制定的一个标准,致力于浏览器原生支持CSS查询,Sele ...

  2. BeautifulSoup高级应用 之 CSS selectors /CSS 选择器

    BeautifulSoup支持最常用的CSS selectors,这是将字符串转化为Tag对象或者BeautifulSoup自身的.select()方法. 本篇所使用的html为: html_doc ...

  3. CSS 笔记六(Image/Attribute Selectors)

    Image Opacity / Transparency The CSS opacity property is a part of the CSS3 recommendation. Example ...

  4. CSS 笔记一(Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width)

    Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width CSS Introduction: CSS stands for ...

  5. DOM扩展之Selectors API

    jQuery的核心就是通过CSS选择符查询DOM文档取得元素的引用,从而抛开了getElementById()和getElementsByTagName(). Selectors API致力于让浏览器 ...

  6. 转:SELENIUM TIPS: CSS SELECTORS

    This page will show you some CSS rules and pseudo-classes that will help you move your XPATH locator ...

  7. selectors实现高并发

    1. 下面的例子,客户端给服务端发送消息,服务端把消息返回 server #!/usr/bin/env python import selectors import socket import tim ...

  8. 【CSS3】Advanced3:Universal, Child, and Adjacent Selectors

    1.Universal selectors eg:#target*{ } 2.Child selectors < something immediately nested within some ...

  9. 【CSS】Intermediate1:Class and ID Selectors

    1.html tag = css selector 2.Define your own selectors in the form of class and ID selectors 3. .clas ...

随机推荐

  1. Drupal Coder 模块远程命令执行分析(SA-CONTRIB-2016-039)

    转载请注明文章出处:http://www.cnblogs.com/magic-zero/p/5787181.html 起初看到这个漏洞的时候是在exploit-db上边.地址在这里:https://w ...

  2. yield return 的使用方法

    以下代码,返回List,list内容为大于60的项 public Form1() { InitializeComponent(); } private void Form1_Load(object s ...

  3. Locust 类的使用

     HttpLocust类 可定义多个HttpLocust类,即多个用户可执行不同的任务或者相同的任务,但是执行频率不一样,用weight进行约定. # coding:utf-8 from locust ...

  4. DIY FRDM-KL25Z开发环境 -- 基于GNU工具链

    IDE大行其道的今天,一键make极大的便利了开发的同时,也每每让各种半路出家的猿们遇到工具链的问题感到束手无策(不就是说自己嘛?^_^!!!).也玩过不少板子了,始终没去深究工具链方面的问题,对于嵌 ...

  5. element ui 表格提交时获取所有选中的checkbox的数据

    <el-table ref="multipleTable" :data="appList" @selection-change="changeF ...

  6. Zookeeper选举算法原理

    Zookeeper选举算法原理 Leader选举 Leader选举是保证分布式数据一致性的关键所在.当Zookeeper集群中的一台服务器出现以下两种情况之一时,需要进入Leader选举. (1) 服 ...

  7. 汉诺塔问题java实现

    问题描述 三个柱子,起初有若干个按大小关系顺序安放的盘子,需要全部移动到另外一个柱子上.移动规则:在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘. 解题思路 使用递归算法进行处理,实在理不 ...

  8. linux下对应mysql数据库的常用操作

    ssh管理工具连接mysql数据库. 一.连接mysql数据库: 通过shh管理工具,登录linux的用户名,密码,进入ssh的命令行界面后,执行如下命令: mysql -u 数据库用户名 -p 然后 ...

  9. eclipse中修改tomcat的配置,解决全局性的get提交乱码问题

    在项目中如果页面提交方式为get的时候,中文会出现乱码. 为了解决乱码问题我们有两种办法. 第一种:在程序中加入get提交乱码的解决 String username = new String(user ...

  10. SQL Cookbook—查询、排序

    涉及到的问题1.在select语句中使用条件逻辑2.限制返回的行数3.从表中随机返回n条记录4.将空值转换为实际值5.对字母和数字混合的数据排序6.处理排序空值7.根据数据项的键排序–8.从一个表中查 ...