I got a question today about whether WebLogic Server has any tools to delete all messages from a JMS Queue. It just so happens that the WLS Console has this capability already. It’s available on the screen after the “Show Messages” button is clicked on a destination’s Monitoring tab as seen in the screen shot below.
The console is great for something ad-hoc, but what if I want to automate this? Well it just so happens that the console is just a weblogic application layered on top of the JMX Management interface. If you look at the MBean Reference, you’ll find a JMSDestinationRuntimeMBean that includes the operation deleteMessages that takes a JMS Message Selector as an argument. If you pass an empty string, that is essentially a wild card that matches all messages.
Coding a stand-alone JMX client for this is kind of lame, so let’s do something more suitable to scripting. In addition to the console, WebLogic Scripting Tool (WLST) based on Jython is another way to browse and invoke MBeans, so an equivalent interactive shell session to delete messages from a destination would looks like this:
where the domain name is “hotspot_domain”, the JMS Server name is “JMSServer-0”, the Queue name is “Queue-0” and the System Module is named “SystemModule-0”. To invoke the operation, I use the “cmo” object, which is the “Current Management Object” that represents the currently navigated to MBean. The 2 indicates that two messages were deleted.
Combining this WLST code with a recent post by my colleague Steve that shows you how to use an encrypted file to store the authentication credentials, you could easily turn this into a secure automated script. If you need help with that step, a long while back I blogged about some WLST basics. Happy scripting.