JMS Message Selector does not work

1.9k views Asked by At

I have a requirement to filter message based on the Message selector but it seems the consumer with messageSelector is not filtering any message.

I have written below code

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");
    }
}

When I tried to use Message message = consumerWithSelector.receive(100); It does not consumes any message and prints the below output

Produced Message with msgID 25000
Message consumed null

However with Message message = consumer.receive(100); properly consumes the message and the we get below output.

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}

Please let me know whether there is some issue in messageSelector

1

There are 1 answers

2
Shirish Bari On BEST ANSWER

I have found out the issue.

Here I have created two consumers as consumer = session.createConsumer(destination); and consumerWithSelector = session.createConsumer(destination,"msgID > 900");

Consumer consumer is capable of selecting all types of messages as internally it calls method with null selector as session.createConsumer(destination,(String)null);

Hence that message was available to to first consumer and my consumerWithSelector was not able to see this message.

To Solve this

  • I have commented consumer = session.createConsumer(destination);

Now I am able to see the the filtered message.