在activemq中,消费者可以有选择的接收消息。
注意,setString的属性不能被过滤,需要用setStringProperty等setXxxProperty才可以做正常过滤。
生产者:
package com.sniper.jms.selector;
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;
/**
* 发布者
* @author audaque
*
*/
public class Sender {
public static void main(String[] args) {
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session = null;
try {
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory("sniper", "sniper", "tcp://sniper0:61616");
connection = connectionFactory.createConnection(); // 通过连接工厂获取连接
connection.start(); // 启动连接
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session,没有事务
// 消息的目的地
Destination destination = session.createQueue("FirstQueue1"); // 创建消息队列
MapMessage msg1 = session.createMapMessage();
msg1.setString("name", "admin1");
msg1.setString("passwd", "123456");
msg1.setStringProperty("sex", "m");
msg1.setIntProperty("age", 25);
MapMessage msg2 = session.createMapMessage();
msg2.setString("name", "admin2");
msg2.setString("passwd", "123456");
msg2.setStringProperty("sex", "f");
msg2.setIntProperty("age", 30);
// 创建消息生产者
MessageProducer messageProducer = session.createProducer(null);
messageProducer.send(destination, msg1, DeliveryMode.NON_PERSISTENT, 2, 1000 * 10);
messageProducer.send(destination, msg2, DeliveryMode.NON_PERSISTENT, 2, 1000 * 10);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
消费者:
package com.sniper.jms.selector;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 订阅者1
* @author audaque
*
*/
public class Receiver {
public static void main(String[] args) {
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session = null;
try {
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory("sniper", "sniper", "tcp://sniper0:61616");
connection = connectionFactory.createConnection(); // 通过连接工厂获取连接
connection.start(); // 启动连接
//自动签收,就是客户端接收到消息之后,会自动给服务端发送消息表示消息已经签收
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session
Destination destination = session.createQueue("FirstQueue1"); // 创建消息队列
MessageConsumer messageConsumer = session.createConsumer(destination, "sex = 'm'"); // 创建消息消费者
messageConsumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
System.err.println("收到的消息:" + message);
}
});
try {
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (JMSException e) {
e.printStackTrace();
} finally {
if(connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}