ActiveMQ的消息过滤器(六)
来源:互联网 发布:用php写99乘法表 编辑:程序博客网 时间:2024/06/05 10:03
原文 http://blog.csdn.net/zbw18297786698/article/details/52999451
代码 http://download.csdn.net/detail/u014401141/9746321
1、消息过滤器的简介
消息选择器的用法
MessageConsumer是一个Session创建的对象,用来从Destination接收消息
关于消息选择器
MessageConsumer createConsumer( Destination destination, String messageSelector )
MessageConsumer createConsumer( Destination destination, String messageSelector, boolean noLocal )
其中,messageSelector为消息选择器;
noLocal标志默认为false,当设置为true时,限制消费者只能接收和自己相同的连接(Connection)所发布的消息,此标志只适用于主题,不适用于队列。
public final String SELECTOR="JMS_TYPE='MY_TAG1'" ;
选择器检查传入消息的JMS_TYPE的属性,并确定这个属性的值是否等于MY_TAG1;
如果相等,消息报消费;如果不相等,那么消息就会被忽略;
2、Producer.Java的代码
- 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 {
-
-
-
-
- private ConnectionFactory connectionFactory;
-
- private Connection connection;
-
- private Session session;
-
- private MessageProducer messageProducer;
-
- public Producer() {
-
- try {
- this.connectionFactory = new ActiveMQConnectionFactory("zhangsan",
- "123", "tcp://localhost:61616");
- this.connection = connectionFactory.createConnection();
- this.connection.start();
-
- this.session = this.connection.createSession(false,
- Session.AUTO_ACKNOWLEDGE);
- this.messageProducer = this.session.createProducer(null);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
-
- }
-
- public Session getSession() {
- return this.session;
- }
-
- public void send1() {
- try {
-
- Destination destination = this.session.createQueue("first");
- MapMessage msg1 = this.session.createMapMessage();
- msg1.setString("name", "张三");
- msg1.setInt("age", 20);
-
- msg1.setStringProperty("name", "张三");
- msg1.setIntProperty("age", 20);
- msg1.setStringProperty("color", "bule");
-
- MapMessage msg2 = this.session.createMapMessage();
- msg2.setString("name", "李四");
- msg2.setInt("age", 25);
-
- msg2.setStringProperty("name", "李四");
- msg2.setIntProperty("age", 25);
- msg2.setStringProperty("color", "white");
-
- MapMessage msg3 = this.session.createMapMessage();
- msg3.setString("name", "张三");
- msg3.setInt("age", 30);
-
- msg3.setStringProperty("name", "赵六");
- msg3.setIntProperty("age", 30);
- msg3.setStringProperty("color", "black");
-
-
- this.messageProducer.send(destination, msg1,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- this.messageProducer.send(destination, msg2,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- this.messageProducer.send(destination, msg3,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
-
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
-
- public void send2() {
- try {
- Destination destination = this.session.createQueue("first");
- TextMessage message = this.session.createTextMessage("我是一个字符串");
-
- this.messageProducer.send(destination, message,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
-
- }
-
- public static void main(String[] args) {
- Producer producer = new Producer();
- producer.send1();
-
- }
-
- }
3、Conmuser.java的代码
- 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 {
-
-
-
-
- private ConnectionFactory connectionFactory;
-
- private Connection connection;
-
- private Session session;
-
- private MessageConsumer messageConsumer;
-
- private Destination destination;
-
-
- public final String SELECTOR_1 = "age > 20";
-
- public Conmuser() {
-
- try {
- this.connectionFactory = new ActiveMQConnectionFactory("zhangsan",
- "123", "tcp://localhost: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) {
-
- }
- 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();
-
- }
-
- }
4、需要注意的地方
4.1 注意消息过滤器的过滤条件的设置
-
- msg3.setStringProperty("name", "赵六");
- msg3.setIntProperty("age", 30);
- msg3.setStringProperty("color", "black");
4.2 消息过滤器的写法(类似于SQL语句的写法)
-
- public final String SELECTOR_1 = "age > 20";
- public final String SELECTOR_2 = " age > 20 and color='bule'";
0 0