我正在尝试使用java获取activeMQ队列中所有消息的列表,并根据其ID删除其中一条消息。我的代码如下所示:
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(" ");
当我运行它时,我得到以下输出:
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
===============================================
我使用我得到的ID,例如第二个ID message1-server-42764-1483561148119-0:0:1:1:2来使用或删除如下消息:
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();
但我一直得到jms例外:
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
我试过关注this帖子,但我不确定我错过了什么?
答案 0 :(得分:1)
javax.jms.InvalidSelectorException: JMSMessageID=message1-server-42764-1483561148119-0:0:1:1:2
您忘记了ID:
JMSMessageID
添加它并将选择器值换成单引号
String message_id = "'ID:message1-server-42764-1483561148119-0:0:1:1:2'";
MessageConsumer consumer = session.createConsumer(topicDestination, "JMSMessageID=" + message_id);