JMS 中createConsumer方法中的参数Selector

  • Post author:
  • Post category:其他


参考:http://blog.csdn.net/iamfafa/article/details/6265290


在JMS中默认创建一个消息消费者的method:


sesssion.createConsumer(destination),该消费者将接收

destination中的所有消息。

如果想创建一个只关注特定消息的消费者可以使用method:sesssion.createConsumer(destination, selector)

参数:selector是字符串,格式为“key=’value’”,key和value可以用户自己定义。该字段用于匹配消息头中的属性。

通过selector可以让特定消费者consumer只接收包含该属性的消息。

例子:

注意:该例子在MyEclipse2015+JDK1.7+ActiveMQ5.11.1中验证通过。

package example;

import javax.jms.Connection;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.MessageListener;

import javax.jms.MessageProducer;

import javax.jms.Queue;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl;

import org.apache.qpid.amqp_1_0.jms.impl.QueueImpl;

import org.apache.qpid.amqp_1_0.jms.impl.TopicImpl;

public class JMSSelectorTest {



public static void main(String[] args) throws Exception {




String user = env(“ACTIVEMQ_USER”, “admin”);

String password = env(“ACTIVEMQ_PASSWORD”, “password”);

String host = env(“ACTIVEMQ_HOST”, ”

localhost

“);

int port = Integer.parseInt(env(“ACTIVEMQ_PORT”, “5672”));

String destination = arg(args, 0, “queue://JMSSelectorTestQueue”);

ConnectionFactoryImpl factory = new ConnectionFactoryImpl(host, port, user, password);

Destination dest = null;

if( destination.startsWith(“topic://”) ) {


dest = new TopicImpl(destination);

} else {


dest = new QueueImpl(destination);

}

Connection connection = factory.createConnection(user, password);

connection.start();






Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);



MessageConsumer comsumerA = session.createConsumer(dest, “receiver = ‘A'”);



comsumerA.setMessageListener(new MessageListener() {




public void onMessage(Message m) {




try {




System.out.println(“ConsumerA get ” + ((TextMessage) m).getText());



} catch (JMSException e1) {




}



}



});



MessageConsumer comsumerB = session.createConsumer(dest, “receiver = ‘B'”);



comsumerB.setMessageListener(new MessageListener() {




public void onMessage(Message m) {




try {




System.out.println(“ConsumerB get ” + ((TextMessage) m).getText());



} catch (JMSException e) {




}



}



});



MessageProducer producer = session.createProducer(dest);



for (int i = 0; i < 10; i++) {




String receiver = (i % 3 == 0 ? “A” : “B”);



TextMessage message = session.createTextMessage(“Message” + i + “, receiver:” + receiver);



message.setStringProperty(“receiver”, receiver);



producer.send(message);



}



}



private static String arg(String[] args, int i, String string) {




// TODO Auto-generated method stub



return string;



}



private static String env(String string, String string2) {




// TODO Auto-generated method stub



return string2;



}

}

运行程序,console中打印信息:

ConsumerA get Message0, receiver:A

ConsumerB get Message1, receiver:B

ConsumerB get Message2, receiver:B

ConsumerA get Message3, receiver:A

ConsumerB get Message4, receiver:B

ConsumerB get Message5, receiver:B

ConsumerA get Message6, receiver:A

ConsumerB get Message7, receiver:B

ConsumerB get Message8, receiver:B

ConsumerA get Message9, receiver:A

ConsumerA get Message0, receiver:A

ConsumerB get Message1, receiver:B

ConsumerB get Message4, receiver:B

ConsumerA get Message6, receiver:A

ConsumerB get Message7, receiver:B