3
votes

I'm trying to get list of all messages I have in activeMQ queue using java and delete one of the messages based on their ID. My code looks like the following:

Connection connection = connectionFactory.createConnection("username","password");
            connection.start();
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            Destination topicDestination = session.createQueue(queue_name);
            QueueBrowser browser = session.createBrowser((Queue) topicDestination);
            Enumeration<?> messages = browser.getEnumeration();
            int count=0;
            while( messages.hasMoreElements()){
                count++;
                TextMessage messageInTheQueue = (TextMessage)messages.nextElement();
                System.out.println("Message "+count+" in the queue:" );
                System.out.println(messageInTheQueue.getJMSMessageID());
                System.out.println(messageInTheQueue.getText());
                System.out.println("===============================================");
                System.out.println(" ");

when I run it I get the following output:

Message 1 in the queue:
ID:message1-server-42764-1483561148119-0:0:1:1:1
Today is warm
===============================================

Message 2 in the queue:
ID:message1-server-42764-1483561148119-0:0:1:1:2
Today is dry
===============================================

I use the ID I get like for example the second ID message1-server-42764-1483561148119-0:0:1:1:2 to consume or delete the message like the following:

Connection connection = connectionFactory.createConnection("username","password");
            Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
            Destination topicDestination = session.createQueue(queue_name);
            MessageConsumer consumer = session.createConsumer(topicDestination, "JMSMessageID="+message_id); 
            connection.start(); 
            consumer.receive();
            consumer.close(); 
            session.commit(); 
            session.close(); 
            connection.stop();

but I keep getting jms exception:

 javax.jms.InvalidSelectorException: JMSMessageID=message1-server-42764-1483561148119-0:0:1:1:2
        at org.apache.activemq.selector.SelectorParser.parse(SelectorParser.java:47)
        at org.apache.activemq.ActiveMQMessageConsumer.<init>(ActiveMQMessageConsumer.java:186)
        at org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:840)
        at activeMQ.DeleteSingleMessage.run(DeleteSingleMessage.java:30)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.activemq.selector.TokenMgrError: Lexical error at line 1, column 51.  Encountered: ":" (58), after : ""
        at org.apache.activemq.selector.SelectorParserTokenManager.getNextToken(SelectorParserTokenManager.java:1057)
        at org.apache.activemq.selector.SelectorParser.jj_scan_token(SelectorParser.java:1133)
        at org.apache.activemq.selector.SelectorParser.jj_3R_18(SelectorParser.java:849)
        at org.apache.activemq.selector.SelectorParser.jj_3R_11(SelectorParser.java:857)
        at org.apache.activemq.selector.SelectorParser.jj_3R_9(SelectorParser.java:883)
        at org.apache.activemq.selector.SelectorParser.jj_3_5(SelectorParser.java:916)
        at org.apache.activemq.selector.SelectorParser.jj_2_5(SelectorParser.java:563)
        at org.apache.activemq.selector.SelectorParser.addExpression(SelectorParser.java:323)
        at org.apache.activemq.selector.SelectorParser.comparisonExpression(SelectorParser.java:172)
        at org.apache.activemq.selector.SelectorParser.equalityExpression(SelectorParser.java:132)
        at org.apache.activemq.selector.SelectorParser.andExpression(SelectorParser.java:96)
        at org.apache.activemq.selector.SelectorParser.orExpression(SelectorParser.java:75)
        at org.apache.activemq.selector.SelectorParser.JmsSelector(SelectorParser.java:67)
        at org.apache.activemq.selector.SelectorParser.parse(SelectorParser.java:44)
        ... 4 more

I tried following this post but I'm not sure what I'm missing?

1
I'm not entirely sure but I think you'll have to set Message ID equal to Correlation ID or something similar to achieve this. I recall facing similar issue when I was working with IBM Websphere. But as I said, I'm not entirely sure about it.user2004685
thanks for your comment! I tried getting the correlation ID and it returned nulltkyass
Does the exception give you any further detail about what is going wrong?DaveH
@DaveH please see my updated question with detailed exception message. I also found this post which I'm not sure if its related or nottkyass
Try wrapping the message selector in single quotes. JMSMessageID='"+message_id+"'");DaveH

1 Answers

3
votes
 javax.jms.InvalidSelectorException: JMSMessageID=message1-server-42764-1483561148119-0:0:1:1:2

you forgot ID: of JMSMessageID

add it and wrap selector value in single quotes

  String message_id = "'ID:message1-server-42764-1483561148119-0:0:1:1:2'";
  MessageConsumer consumer = session.createConsumer(topicDestination, "JMSMessageID=" + message_id);