activemq消息的过滤

1,消息的过滤
对消息消费者处理的消息数据进行过滤。这种处理可以明确消费者的角色,细分消费者 的功能。 设置过滤: Session.createConsumer(Destinationdestination,StringmessageSelector); 过滤信息为字符串,语法类似 SQL92 中的 where 子句条件信息。可以使用诸如 AND、 OR、IN、NOTIN 等关键字。

注意:消息的生产者在发送消息的的时候,必须设置可过滤的属性信息,所有的属性信 息设置方法格式为:setXxxxProperty(String name,Tvalue)。 其中方法名中的 Xxxx 是类型, 如 setObjectProperty/setStringProperty 等。

 

2,直接上代码:

       生产者:

public class Producer {

	private ConnectionFactory connectionFactory;

	private Connection connection;

	public Producer() throws Exception {
		init();
	}
	private void init() throws Exception {
		this.connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
		this.connection = this.connectionFactory.createConnection("username", "password");
	}
	/**
	 * close Connection
	 * 
	 * @param connection
	 * @throws JMSException
	 */
	private void close(Connection connection) throws JMSException {
		if (null != connection) {
			connection.close();
		}
	}
	public void send() throws JMSException {
		Connection connection = this.connection;
		Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue("first");
		MapMessage cMap1 = session.createMapMessage();
		cMap1.setString("name", "zhang1");
		cMap1.setString("sal", "3100");
		cMap1.setStringProperty("color", "blue");
		cMap1.setIntProperty("age", 21);
		
		MapMessage cMap2 = session.createMapMessage();
		cMap2.setString("name", "zhang2");
		cMap2.setString("sal", "3200");
		cMap2.setStringProperty("color", "red");
		cMap2.setIntProperty("age", 22);
		
		MapMessage cMap3= session.createMapMessage();
		cMap3.setString("name", "zhang3");
		cMap3.setString("sal", "3300");
		cMap3.setStringProperty("color", "green");
		cMap3.setIntProperty("age", 23);
		
		MapMessage cMap4= session.createMapMessage();
		cMap4.setString("name", "zhang4");
		cMap4.setString("sal", "3400");
		cMap4.setStringProperty("color", "blue");
		cMap4.setIntProperty("age", 24);
		
		MessageProducer producer = session.createProducer(destination);
		
		producer.send(destination, cMap1);
		producer.send(destination, cMap2);
		producer.send(destination, cMap3);
		producer.send(destination, cMap4);
		close(connection);
	}
	public static void main(String[] args) throws Exception {
		Producer p = new Producer();
		p.send();
	}
}

       消费者:

          

/**
 * 消费者  
 * @author reyco
 *
 */
public class Consumer {
	private final String SELECT_1 = "age>21";
	
	private final String SELECT_2 = "color = 'blue'";
	
	private final String SELECT_3 = "age>21 AND color = 'blue'";
	
	private ConnectionFactory connectionFactory;

	private Connection connection;

	private Session session;

	private MessageConsumer Consumer;
	
	private Destination destination;
	
	public Consumer() throws Exception {
		init();
	}
	private void init() throws Exception {
		this.connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
		this.connection = this.connectionFactory.createConnection("username", "password");
		this.connection.start();
		this.session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		this.destination = this.session.createQueue("first");
		//  过滤条件
		this.Consumer = this.session.createConsumer(this.destination,SELECT_3);
	}
	public void receiver() {
		try {
			this.Consumer.setMessageListener(new Listener());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		Consumer c = new Consumer();
		c.receiver();
	}
	
	class Listener implements MessageListener{

		@Override
		public void onMessage(Message message) {
			try {
				if(message instanceof TextMessage) {
					System.out.println("message="+message.toString());
				}
				if(message instanceof MapMessage) {
					Message ms =(Message)message;
					System.out.println("ms="+ms.toString());
					System.out.println("age="+ms.getIntProperty("age"));
					System.out.println("color="+ms.getStringProperty("color"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java的艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值