问题
I have a list of messages in activemq queue. each message has a custom header property with value. How I should be able to access only those messages whose custom header property value = 123.?
I am using something like below to pick a message from queue. How to pick all messages which or a single message which has customHeaderProperty =123.?
ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
Exchange ex = consumerTemplate.receive("activemq:queueName",10000);
String data = ex.getIn().getBody(String.class);
String number = ex.getIn().getHeader("customProperty", String.class);
回答1:
Use message selectors on the consumer. A selector is a SQL like query. So you could write something like myCustomHeader = 123
. Here is a pretty good cheat sheet.
Since you tagged the question with apache-camel, I guess you are working with a Camel setup. In that case, you need to supply the selector to Camel. Something like from("activemq:queue:myqueue?selector=myCustomHeader%3D123").
.
来源:https://stackoverflow.com/questions/31549200/how-to-read-only-specific-queue-messages-based-on-message-header-property