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:
- Connect to the server
- Get to the JMSDestinationRuntimeMBean
- Get the cursor to find the number of messages
- Get the messages without body to find the cursor and the Message ID
- Get the message with body using the Message ID
- 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
url='t3://localhost:7001'
username='weblogic'
password='weblogic1'
jmsservername='test-jms-server'
jmsmodulename='test-jms-module'
jmsdestname='test-queue'
#Connect
connect(username,password,url)
#Switch to the server runtime tree
serverRuntime()
#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 javax.management.openmbean.CompositeData
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'
else:
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()
else:
if isinstance(fullwlmsg, ObjectMessage):
print fullwlmsg.getObject()
else:
print '***Not a Text or Object Message***'
print fullwlmsg.toString()
print ' '
print '--------------------------------------------------------------'
print ' '
#Close cursor as No Time Out specified - Best practice
cmo.closeCursor(cursor)
#Disconnect & Exit
disconnect()
exit()
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?
ReplyDeleteserverName 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.
ReplyDeleteWell, 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.
ReplyDeleteOnce 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.
ReplyDeleteconnect()
domainRuntime()
cd('ServerRuntimes')
ls()
...
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.
Hi Bala,
ReplyDeleteIs there any way to read weblogic server's console message using WLST?
Thanks,
-Selva
Im gettin this error while executing the script.
ReplyDelete****************
Problem invoking WLST - Traceback (innermost last):
(no code object) at line 0
File "/tmp/JMSMessage.py", 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
Problem invoking WLST - Traceback (innermost last):
ReplyDelete(no code object) at line 0
File "/tmp/Vasanth/JMSMessage.py", 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
Hi Kothandaraman,
ReplyDeletevery 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=10.3.4.0).Please assist.
If someone is facing to the problem with
ReplyDeletejmsmsginfo = JMSMessageInfo(message)
^
SyntaxError: invalid syntax
then try to format the Python code correctly, e.g. the code inside for loop needs to be indentedl.