我需要根据Message选择器过滤消息,但似乎带有messageSelector的消费者没有过滤任何消息。
我写了以下代码
ActiveMQUtil
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class ActiveMQUtil {
private ConnectionFactory connectionFactory = null;
private Connection connection =null;
private Session session =null;
private MessageProducer producer = null;
private MessageConsumer consumer = null;
private Queue destination = null;
private MessageConsumer consumerWithSelector;
public static void main(String []arg) throws JMSException {
ActiveMQUtil activeMQUtil = new ActiveMQUtil();
activeMQUtil.initialize();
activeMQUtil.produceMessage(25000);
activeMQUtil.consumeMessage();
}
private void consumeMessage() throws JMSException {
Message message = consumerWithSelector.receive(100); // this does not select any message
//Message message = consumer.receive(100); // This properly received the message and I can see the message
if(null!=message && message.propertyExists("msgID")){
System.out.println("Consumed message with msgID = " + message.getIntProperty("msgID"));
}
System.out.println("Message consumed "+message);
}
private void produceMessage(int messageID) throws JMSException {
Message message = session.createTextMessage();
message.setIntProperty("msgID",messageID);
producer.send(message);
System.out.println("Produced Message with msgID " + message.getIntProperty("msgID"));
}
private void initialize() throws JMSException {
connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination= session.createQueue("TEST.FOO3");
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
consumer = session.createConsumer(destination);
consumerWithSelector = session.createConsumer(destination,"msgID > 900");
}
}
当我尝试使用Message message = consumerWithSelector.receive(100);
时,它不会消耗任何消息并打印以下输出
Produced Message with msgID 25000
Message consumed null
然而,Message message = consumer.receive(100);
正确使用消息,我们得到低于输出。
Produced Message with msgID 25000
Consumed message with msgID = 25000
Message consumed ActiveMQTextMessage {commandId = 7, responseRequired = true, messageId = ID:didnsriina6-54782-1433918830039-3:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:didnsriina6-54782-1433918830039-3:1:1:1, destination = queue://TEST.FOO3, transactionId = null, expiration = 0, timestamp = 1433918830394, arrival = 0, brokerInTime = 1433918830394, brokerOutTime = 1433918844118, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = org.apache.activemq.util.ByteSequence@2330633a, dataStructure = null, redeliveryCounter = 0, size = 1040, properties = {msgID=25000}, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = null}
请告诉我messageSelector
答案 0 :(得分:1)
我发现了这个问题。
在这里,我创建了两个消费者consumer = session.createConsumer(destination);
和consumerWithSelector = session.createConsumer(destination,"msgID > 900");
消费者consumer
能够选择所有类型的消息,因为它在内部调用带有 null 选择器的方法为session.createConsumer(destination,(String)null);
因此,第一个consumer
可以使用该邮件,我的consumerWithSelector
无法看到此消息。
解决这个问题
consumer = session.createConsumer(destination);
现在我能够看到已过滤的消息。