Solx
Solx

Reputation: 5121

How can I delete Messages from a JMS Queue?

I have several jobs that each have multiple messages queued. The messages for each job are randomly interleaved. If a user decides to cancel a job I want to remove all the messages that are part of that job from the queue. I have been able to use browse()to find all the messages to remove but haven't been able to figure out how to remove them. I tried getting rid of them by using receiveSelected() but it just hangs. (I am using JmsTemplate)

Upvotes: 3

Views: 8885

Answers (3)

Petter Nordlander
Petter Nordlander

Reputation: 22279

You are on the right track. Consuming those messages using a selector is the way to go - such as with JmsTemplate receiveSelected.

If it "hangs", it likely means you have no matching messages on the queue. Can you identify your messages on some Property, such as JMSType or other StringProperty? Make sure you can and supply a JMS Selector.

I.e. if your jobs are initiated by user X, then set some property such as "initiatingUser" to "x". Then to consume all messages, use the selector initiatingUser='X'.

Upvotes: 0

Varsha
Varsha

Reputation: 1198

There is no any JMS API to remove message. However seems you can invoke purge removeMessage or other operation as per your requirement on MBean org.apache.activemq:type=Broker,brokerName=amq,destinationType=Queue,destinationName=testQ to delete messages.

Upvotes: 0

Matt Pavlovich
Matt Pavlovich

Reputation: 4316

JMS does not define administration type functions, such as deleting a message from the queue.

The programmatic way is to consume the message. Alternatively, there are messaging management tools that allow you to do this without programming.

Upvotes: 0

Related Questions