Thursday, July 1, 2010

Using WLST Script To List Messages From A JMS Queue

WebLogic Server added support for Runtime Message Management for Destination hosted on WebLogic JMS Servers in WLS 9.0. It will an exciting feature if you are currently in WLS 8.1 or new to WLS. Now you can administratively view and browse all messages, and manipulate most messages in a running JMS server, by using the Administration Console or WLS public runtime APIs. Once I start getting excited about this feature to customer the very next logical question from administrators are can I script this? The answer is obviously, Yes. Java or WLST is your answer. If you are a developer and there is a requirement to provide a Java/JEE application for your administrators or business users to view or browse the messages then Java will be your option. For administrators who are non-Java, there are MBeans like JMSDestinationRuntimeMBean and JMSDurableSubscriberRuntimeMBean that you can use from WLST to script any of the message managment features. Needless to say if there are existing Java implementation for message management then you can simply import and use them within WLST as well. But WLST scripts are natural choice for administrators.

So I started out writing a simple WLST script that can list all the messages from a given JMS Queue. The script is written to list all the headers and properties along with the body. I have made enough comments in the script to explain the different steps. But the high-level steps are:
  1. Connect to the server
  2. Get to the JMSDestinationRuntimeMBean
  3. Get the cursor to find the number of messages
  4. Get the messages without body to find the cursor and the Message ID
  5. Get the message with body using the Message ID
  6. Print the Headers, Properties and Body (or) Pay Load of the Message

A key information to note here is if you have any Object Messages in the Queue then those classes should be added to the Java Classpath before you execute this WLST Script. I haven't implemented any exception handling logic in my script whereas handling exceptions are best practices and you might want to refer to my other entries regarding that information.

#Import necessary classes/interfaces
from weblogic.jms.extensions import JMSMessageInfo
from javax.jms import TextMessage
from javax.jms import ObjectMessage

#Define constants


#Switch to the server runtime tree

#Navigate to the JMS Destination Runtime MBean
cd('JMSRuntime/' + serverName + '.jms/JMSServers/' + jmsservername)
cd('Destinations/' + jmsmodulename + '!' + jmsdestname)

#Get the cursor (JMSMessageCursorRuntimeMBean) to browse the messages - No selector & No time out
cursor = cmo.getMessages('',0)

#Determine the number of messages in the destination
cursorsize = cmo.getCursorSize(cursor)
print '------------------------------------------'
print 'Total Number of Messages -> ', cursorsize
print '------------------------------------------'

#Get all the messages as an array of
messages = cmo.getNext(cursor, cursorsize)

#Loop through the array of messages to print
for message in messages:

#Create WebLogic JMSMessageInfo to get Message ID
jmsmsginfo = JMSMessageInfo(message)
wlmsg = jmsmsginfo.getMessage()
wlmsgid = wlmsg.getJMSMessageID()

#Get Message with body
fullcursormsg = cmo.getMessage(cursor,wlmsgid)
fulljmsmsginfo = JMSMessageInfo(fullcursormsg)
handle = fulljmsmsginfo.getHandle()
compdata = cmo.getMessage(cursor, handle)
msgwithbody = JMSMessageInfo(compdata)

#Print Key Message Headers
print 'Message ID - ' + msgwithbody.getMessage().getJMSMessageID()
print 'Message Priority -' , msgwithbody.getMessage().getJMSPriority()
if msgwithbody.getMessage().getJMSRedelivered() == 0:
redeliv = 'false'
redeliv = 'true'
print 'Message Redelivered - ' + redeliv
print 'Message TimeStamp -' , msgwithbody.getMessage().getJMSTimestamp()
print 'Message DeliveryMode -' , msgwithbody.getMessage().getJMSDeliveryMode()

#Print Message Properties
prop_enum = msgwithbody.getMessage().getPropertyNames()
print ' '
print 'Message Properties :'
print ' '
for prop in prop_enum:
print prop + ' - > ' + msgwithbody.getMessage().getStringProperty(prop)

#Print Message Body
fullwlmsg = fulljmsmsginfo.getMessage()
print ' '
print 'Message Body :'
print ' '
if isinstance(fullwlmsg, TextMessage):
print fullwlmsg.getText()
if isinstance(fullwlmsg, ObjectMessage):
print fullwlmsg.getObject()
print '***Not a Text or Object Message***'
print fullwlmsg.toString()
print ' '
print '--------------------------------------------------------------'
print ' '

#Close cursor as No Time Out specified - Best practice

#Disconnect & Exit


  1. I'm still a novice with WLST, but where is serverName declared? Also what would happen if you have multiple managed servers connected to the same adminserver?

  2. serverName is a built-in WLST variable which will hold the name of the server you are connected to. In this sample script I assumed that you already have a JMS Server, Queue deployed on the server that you are connecting to. If you have JMS deployed to the managed servers then you should change the URL.

  3. Well, that's my main problem. I was trying to connect to our admin server and then switch via cd to each managed server (and then to each JMS queue). But how do I determine which managed servers are connected to it? I try to avoid connecting to each managed server but try to connect to admin or node manager instead.

  4. Once you are connected to the administration server you have to switch to "domainRuntime" tree and look for ServerRuntime MBeans. The entries (MBeans) get listed under the ServerRuntimes can be used to identify the servers that are running in the domain and are connected to the administration server.


    The output of the above ls() will show the list of the running servers in this domain. If you further cd('xyz') in the servers you will find a boolean attribute called "AdminServer" which will signify whether that server is an administration server or not.

  5. Hi Bala,
    Is there any way to read weblogic server's console message using WLST?


  6. Im gettin this error while executing the script.
    Problem invoking WLST - Traceback (innermost last):
    (no code object) at line 0
    File "/tmp/", line 39
    jmsmsginfo = JMSMessageInfo(message)
    SyntaxError: invalid syntax
    Do i have to import or define anyother class files apart from the ones mentioned in the script.
    from weblogic.jms.extensions import JMSMessageInfo
    from javax.jms import TextMessage
    from javax.jms import ObjectMessage

  7. Problem invoking WLST - Traceback (innermost last):
    (no code object) at line 0
    File "/tmp/Vasanth/", line 39
    jmsmsginfo = JMSMessageInfo(message)
    SyntaxError: invalid syntax
    Im getting the above error while running the script.Do i have to import/define any other class files apart from the ones mentioned in your script

  8. Hi Kothandaraman,
    very useful post.
    But when i try to run it weblogic.jms.extensions import JMSMessageInfo is creating exception saying class not found.when i have checked in the
    wljmsclient.jar, i could not find the JMSMessageInfo class.I am using the weblogic latest version (WLS_PRODUCT_VERSION= assist.

  9. If someone is facing to the problem with

    jmsmsginfo = JMSMessageInfo(message)
    SyntaxError: invalid syntax

    then try to format the Python code correctly, e.g. the code inside for loop needs to be indentedl.