Monday, March 7, 2011

Use WLST to Delete All JMS Messages From a Destination

This blog was originally posted on when I worked for Oracle.

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:


D:\Oracle\fmw11gr1ps3\user_projects\domains\hotspot_domain>java weblogic.WLST


Initializing WebLogic Scripting Tool (WLST) ...


Welcome to WebLogic Server Administration Scripting Shell


Type help() for help on available commands


wls:/offline> connect('weblogic','welcome1','t3://localhost:7001')

Connecting to t3://localhost:7001 with userid weblogic ...

Successfully connected to Admin Server 'AdminServer' that belongs to domain 'hotspot_domain'.


Warning: An insecure protocol was used to connect to the

server. To ensure on-the-wire security, the SSL port or

Admin port should be used instead.


wls:/hotspot_domain/serverConfig> serverRuntime()

Location changed to serverRuntime tree. This is a read-only tree with ServerRuntimeMBean as the root.

For more help, use help(serverRuntime)


wls:/hotspot_domain/serverRuntime> cd('JMSRuntime/AdminServer.jms/JMSServers/JMSServer-0/Destinations/SystemModule-0!Queue-0')

wls:/hotspot_domain/serverRuntime/JMSRuntime/AdminServer.jms/JMSServers/JMSServer-0/Destinations/SystemModule-0!Queue-0> ls()

dr--   DurableSubscribers


-r--   BytesCurrentCount                            0

-r--   BytesHighCount                               174620

-r--   BytesPendingCount                            0

-r--   BytesReceivedCount                           253548

-r--   BytesThresholdTime                           0

-r--   ConsumersCurrentCount                        0

-r--   ConsumersHighCount                           0

-r--   ConsumersTotalCount                          0

-r--   ConsumptionPaused                            false

-r--   ConsumptionPausedState                       Consumption-Enabled

-r--   DestinationInfo                    ,items=((itemName=ApplicationName,,(itemName=ModuleName,,(itemName


ule-0!Queue-0, Queue=true, SerializedDestination=rO0ABXNyACN3ZWJsb2dpYy5qbXMuY29tbW9uLkRlc3RpbmF0aW9uSW1wbFSmyJ1qZfv8DAAAeHB3kLZBABZTeXN0ZW1Nb2R1bGUtMCFRdWV1ZS0wAAtKTVNTZXJ2ZXItMAAOU3lzdGVtTW9kdWxlLTABAANBbGwCAlb6IS6T5qL/AAAACgEAC0FkbWluU2VydmVyAC2EGgJW+iEuk+ai/wAAAAsBAAtBZG1pblNlcnZlcgAthBoAAQAQX1dMU19BZG1pblNlcnZlcng=, ServerName=JMSServer-0, Topic=false, VersionNumber=1})

-r--   DestinationType                              Queue

-r--   DurableSubscribers                           null

-r--   InsertionPaused                              false

-r--   InsertionPausedState                         Insertion-Enabled

-r--   MessagesCurrentCount                         0

-r--   MessagesDeletedCurrentCount                  3

-r--   MessagesHighCount                            2

-r--   MessagesMovedCurrentCount                    0

-r--   MessagesPendingCount                         0

-r--   MessagesReceivedCount                        3

-r--   MessagesThresholdTime                        0

-r--   Name                                         SystemModule-0!Queue-0

-r--   Paused                                       false

-r--   ProductionPaused                             false

-r--   ProductionPausedState                        Production-Enabled

-r--   State                                        advertised_in_cluster_jndi

-r--   Type                                         JMSDestinationRuntime


-r-x   closeCursor                                  Void : String(cursorHandle)

-r-x   deleteMessages                               Integer : String(selector)

-r-x   getCursorEndPosition                         Long : String(cursorHandle)

-r-x   getCursorSize                                Long : String(cursorHandle)

-r-x   getCursorStartPosition                       Long : String(cursorHandle)

-r-x   getItems                           [] : String(cursorHandle),Long(start),Integer(count)

-r-x   getMessage                          : String(cursorHandle),Long(messageHandle)

-r-x   getMessage                          : String(cursorHandle),String(messageID)

-r-x   getMessage                          : String(messageID)

-r-x   getMessages                                  String : String(selector),Integer(timeout)

-r-x   getMessages                                  String : String(selector),Integer(timeout),Integer(state)

-r-x   getNext                            [] : String(cursorHandle),Integer(count)

-r-x   getPrevious                        [] : String(cursorHandle),Integer(count)

-r-x   importMessages                               Void :[],Boolean(replaceOnly)

-r-x   moveMessages                                 Integer : String(java.lang.String),,Integer(java.lang.Integer)

-r-x   moveMessages                                 Integer : String(selector),

-r-x   pause                                        Void :

-r-x   pauseConsumption                             Void :

-r-x   pauseInsertion                               Void :

-r-x   pauseProduction                              Void :

-r-x   preDeregister                                Void :

-r-x   resume                                       Void :

-r-x   resumeConsumption                            Void :

-r-x   resumeInsertion                              Void :

-r-x   resumeProduction                             Void :

-r-x   sort                                         Long : String(cursorHandle),Long(start),String[](fields),Boolean[](ascending)


wls:/hotspot_domain/serverRuntime/JMSRuntime/AdminServer.jms/JMSServers/JMSServer-0/Destinations/SystemModule-0!Queue-0> cmo.deleteMessages('')


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.


SSL Cert News said...

Hello James!

Your Blog has informative source. Thanks for sharing it with US.

- WildCard SSL

Ivan said...

Hi. I have a problem with deleting jms messages, because in my WLS 10.3.3 I don't see your same WLS Console:

From WLST I have this configuration:
but with "ls()" command I don't see any JMSServer

Could you tell me why?

Blogger1306 said...

Hi James, I have the same problem as Ivan.

I cannot use the script you have suggested
wls:/hotspot_domain/serverRuntime/JMSRuntime/AdminServer.jms/JMSServers/JMSServer-0/Destinations/SystemModule-0!Queue-0> cmo.deleteMessages('')

because /AdminServer.jms/JMSServers does not show the JMS Server for which I intend to delete the messages. I have also tried the following but it did not work.

wls:/SOADomain1/serverConfig/JMSSystemResources/myJMSServer/JMSResource/myJMSServer/Queues/myQueue> cmo.deleteMessages('')
Traceback (innermost last):
File "", line 1, in ?
AttributeError: deleteMessages

Can you please advise?

Blogger1306 said...

I found an answer to my question. I should have used the server's url while connecting instead of using that of Admin Server
e.g. connect('weblogic','welcome','t3://lcoalhost:8001')

Shitanshu Jain said...

Please check which server you are trying to connect. In my case, my JMS server was on soa_server, but in this example we are connecting to AdminServer

Unknown said...

Do you know how is it possible to delete JMS message in WLS 12c ?

koe61831 said...

I urgently needed to find some help in writing essays, due to the large number of essays that we were given at home, I could not cope, although I tried, so I needed a great assistant, and this site was nearby, thanks for which I got an A