Saturday, June 25, 2011

WebLogic Server JMS WLST Script – Who is Connected To My Server

This blog was originally posted on http://blogs.oracle.com/jamesbayer when I worked for Oracle.

Update 6/27/2011: Tom Barnes indicated a clarification, check out the comments.
Ever want to know who was connected to your WebLogic Server instance for troubleshooting?  JT_Switchboard_770x540An email exchange about this topic and JMS came up this week, and I’ve heard it come up once or twice before too.  Sometimes it’s interesting or helpful to know the list of JMS clients (IP Addresses, JMS Destinations, message counts) that are connected to a particular JMS server.  This can be helpful for troubleshooting.  Tom Barnes from the WebLogic Server JMS team provided some helpful advice:
The JMS connection runtime mbean has “getHostAddress”, which returns the host address of the connecting client JVM as a string. 
A connection runtime can contain session runtimes, which in turn can contain consumer runtimes.  The consumer runtime, in turn has a “getDestinationName” and “getMemberDestinationName”.  I think that this means you could write a WLST script, for example, to dump all consumers, their destinations, plus their parent session’s parent connection’s host addresses.   
Note that the client runtime mbeans (connection, session, and consumer) won’t necessarily be hosted on the same JVM as a destination that’s in the same cluster (client messages route from their connection host to their ultimate destination in the same cluster).

Writing the Script

So armed with this information, I decided to take the challenge and see if I could write a WLST script to do this.  It’s always helpful to have the WebLogic Server MBean Reference handy for activities like this.  This one is focused on JMS Consumers and I only took a subset of the information available, but it could be modified easily to do Producers.  I haven’t tried this on a more complex environment, but it works in my simple sandbox case, so it should give you the general idea.
# Better to use Secure Config File approach for login as shown here http://buttso.blogspot.com/2011/02/using-secure-config-files-with-weblogic.html
connect('weblogic','welcome1','t3://localhost:7001')
 
# Navigate to the Server Runtime and get the Server Name
serverRuntime()
serverName = cmo.getName()
 
# Multiple JMS Servers could be hosted by a single WLS server
cd('JMSRuntime/' + serverName + '.jms' )
jmsServers=cmo.getJMSServers()
 
# Find the list of all JMSServers for this server
namesOfJMSServers = ''
for jmsServer in jmsServers:
 namesOfJMSServers = jmsServer.getName() + ' '
 
# Count the number of connections
jmsConnections=cmo.getConnections()
print str(len(jmsConnections)) + ' JMS Connections found for ' + serverName + ' with JMSServers ' + namesOfJMSServers
 
# Recurse the MBean tree for each connection and pull out some information about consumers
for jmsConnection in jmsConnections:
 try:
  print 'JMS Connection:'
  print '  Host Address = ' + jmsConnection.getHostAddress()
  print '  ClientID = ' + str( jmsConnection.getClientID() )
  print '  Sessions Current = ' + str( jmsConnection.getSessionsCurrentCount() )
  jmsSessions = jmsConnection.getSessions()
  for jmsSession in jmsSessions:
   jmsConsumers = jmsSession.getConsumers()
   for jmsConsumer in jmsConsumers:
    print '   Consumer:'
    print '     Name = ' + jmsConsumer.getName()
    print '     Messages Received = ' + str(jmsConsumer.getMessagesReceivedCount())
    print '     Member Destination Name = ' + jmsConsumer.getMemberDestinationName()
 except:
  print 'Error retrieving JMS Consumer Information'
  dumpStack()
 
# Cleanup
disconnect()
exit()

Example Output

I expect the output to look something like this and loop through all the connections, this is just the first one:
1 JMS Connections found for AdminServer with JMSServers myJMSServer
JMS Connection:
  Host Address = 127.0.0.1
  ClientID = None
  Sessions Current = 16
   Consumer:
     Name = consumer40
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue

Notice that it has the IP Address of the client.  There are 16 Sessions open because I’m using an MDB, which defaults to 16 connections, so this matches what I expect.  Let’s see what the full output actually looks like:
D:\Oracle\fmw11gr1ps3\user_projects\domains\offline_domain>java weblogic.WLST d:\temp\jms.py
 
Initializing WebLogic Scripting Tool (WLST) ...
 
Welcome to WebLogic Server Administration Scripting Shell
 
Type help() for help on available commands
 
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'offline_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.
 
Location changed to serverRuntime tree. This is a read-only tree with ServerRuntimeMBean as the root.
For more help, use help(serverRuntime)
 
1 JMS Connections found for AdminServer with JMSServers myJMSServer
JMS Connection:
  Host Address = 127.0.0.1
  ClientID = None
  Sessions Current = 16
   Consumer:
     Name = consumer40
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer34
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer37
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer16
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer46
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer49
     Messages Received = 2
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer43
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer55
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer25
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer22
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer19
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer52
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer31
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer58
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer28
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
   Consumer:
     Name = consumer61
     Messages Received = 1
     Member Destination Name = myJMSModule!myQueue
Disconnected from weblogic server: AdminServer
 
 
Exiting WebLogic Scripting Tool.
Thanks to Tom Barnes for the hints and the inspiration to write this up.
Image of telephone switchboard courtesy of http://www.JoeTourist.net/ JoeTourist InfoSystems