Solved

Access Application MBean via Jython?

Posted on 2010-11-23
27
1,155 Views
Last Modified: 2013-12-10

Is it possible to access an application MBeans directly with a jython wsadmin script?

We have an application that has an MBean intended to refresh a small data cache. The original intent was to write a servlet /jsp that could be accessed to refresh the cache, but because we are in a clustered environment, we never know which appServer will be hit by the jsp, and can't specifically direct the request to one clone or the other, since we're using a load balanced address to get to the app.

I was "thinking"  if we could access the mbean directly through wsadmin and direct our wsadmin script to hit the mbean on each appServer in the cluster, that might solve our issue.

Even if it's not the best way to address this particular issue, now I'm just curious if this can be done, and if so, I'd love to see an example of how.
0
Comment
Question by:Bbouch
  • 15
  • 12
27 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Yes.

The challenge will be to determine how to find the MBean string name.

This is most commonly done by using the AdminControl.queryNames() method.
For example, to look at the first 50 characters of each of the active server MBeans, one could use something like this:

wsadmin>for bean in AdminControl.queryNames( 'type=Server,*' ).splitlines() :
wsadmin>  print bean[ :50 ]
wsadmin>
WebSphere:name=dmgr,process=dmgr,platform=proxy,no
WebSphere:name=nodeagent,process=nodeagent,platfor
WebSphere:name=server1,process=server1,platform=pr

Open in new window

0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 500 total points
Comment Utility
If you are going to be writing, or using Jython wsadmin scripts, you should seriously consider taking a look at: "WebSphere Application Server Administration Using Jython" from IBM Press books: http://www.ibmpressbooks.com/bookstore/product.asp?isbn=0137009526
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Thanks HonorGod. I actually have the book, I just didn't find information about this in there.   Feel free to direct me to the right place.

I've poked around a little bit with the syntax you gave, but I'm still not able to figure out what I am doing exactly.

SO, let me be a bit more specific.  From our developers, we have the following application installed:
PromotionsWSEAR.ear

According to the developer there an mbean is registered on server startup as "LLBCacheUtil:name=Promotions_v2_KeyValueCache"


I think the issue is I'm not sure what "type" to use when using AdminControl.queryNames.  I tried Application but that seemed to pull only the installed applications (not the Mbeans registered from within the application).

Is there a list of available "types" somewhere that I could reference for this kind of thing?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
> I actually have the book ...

Great!  I hope that you find it helpful, and useful.

Well, the list of Types known to WebSphere can be obtained using AdminConfig.types() method (page 152 in the book).

If I want to locate the types that contain a specific substring, I use the findTypes() routine, below

An example use of which is:

wsadmin>findTypes( 'MBean' )
['ExtensionMBean', 'ExtensionMBeanProvider']


def findTypes( text ) :
  'findTypes( text ) - Returns the list of configuration types that containing the specified text.'
  result = [];
  for Type in AdminConfig.types().splitlines() :
    if Type.find( text ) > -1 :
      result.append( Type );
  return result;

Open in new window

0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 500 total points
Comment Utility
So, the question is, how do we locate a specific MBean that exists only within your application?

Well, the "help" for the AdminControl.queryNames() method isn't too helpful:

wsadmin>print AdminControl.help( 'queryNames' )
WASX7036I: Method: queryNames

        Arguments: object name

        Description: Returns a String containing the ObjectNames that match
        the input object name, which may be a wild card.
wsadmin>

What we need to do is to figure out how to identify the MBeans associated with an application.
Once we do this, we will have a much better chance of trying to figure out which is the one that we actually want.

First, we can get the list of active MBeans using something like:

beans = AdminControl.queryNames( '*' ).splitlines()

The number that exist depend upon a number of factors:
- Are you using a single, stand-alone AppServer, or a Network Deployment environment?
- How many resources are defined, and active?
- How many applications are defined, and active?

In my case the number of beans happened to be a little over 300

wsadmin>len( beans )
306

So, how can we find the MBeans associated with our application?
Let's see if the application name is part of the MBean:

wsadmin>for app in AdminApp.list().splitlines() :
wsadmin>  count = 0
wsadmin>  for b in beans :
wsadmin>    if b.find( app ) > -1 :
wsadmin>      count += 1
wsadmin>  print '%3d %s' % ( count, app )
wsadmin>
  5 DefaultApplication
  7 PlantsByWebSphere
  6 SamplesGallery
  5 ivtApp
  3 query


That looks pretty neat.  Let's see which MBeans exist for the Application named "query":

wsadmin>for b in beans :
wsadmin>  if b.find( 'query' ) > -1 :
wsadmin>    print b
wsadmin>

In each MBean, we find a value like:

,mbeanIdentifier=cells/CELLNAME/applications/query.ear/deployments/query/deployment.xml#ApplicationDeployment_1183122157718

and, more importantly, a value of:

name=query

So, we can quickly, and easily list the MBeans associated with the query Application using a call like:

print AdminControl.queryNames( 'name=query,*' )

Once you have this list, you have a much better time figuring out which MBean is the one you want.

Does this help?
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
I think that helped me find the one I need...   but I'm still not sure how to "use" it once I have found it...
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Ah, given an MBean identifer, we can "ask it" what methods exist.

If you execute:

print Help.help()

Notice the first few methods that exist, and the required parameter...
attributes              given an MBean, returns help for attributes
operations              given an MBean, returns help for operations
constructors            given an MBean, returns help for constructors
description             given an MBean, returns help for description
notifications           given an MBean, returns help for notifications
classname               given an MBean, returns help for classname
all                     given an MBean, returns help for all the above

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
So,  to find out what "attributes" an MBean has, all you need do (given the MBean name) is something like:


# Get the MBean names into two different variables

wsadmin>b1, b2 = AdminControl.queryNames( 'name=query,*' ).splitlines()

# Example use of Help.operations() to determine which methods exist, and their expected parameter lists, and any return value:

wsadmin>print Help.operations( b1 )
Operation
[Ljava.lang.String; getModules()
java.lang.String getImplementationVersion()
java.lang.String getEdition()
[Ljava.lang.String; getEventTypes()
java.lang.String getServer()
java.lang.String getDeploymentDescriptor()
java.lang.String getName()
java.lang.String getJavaVersion()
java.lang.String getJavaVendor()
java.lang.String getObjectNameStr()
boolean isStateManageable()
boolean isStatisticsProvider()
boolean isEventProvider()

# Example invocation of a method returning a boolean value:

wsadmin>print AdminControl.invoke( b1, 'isEventProvider' )
true

Open in new window

0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Ok, I'm closer, but I'm still not 100% following ...

In my case I have the following MBean I think is the right one...

['WebSphere:name=PromotionsManager,process=MarketingW_TEST_c1,Application=MarketingW_TESTPromotionsWSEAR,platform=dynamicproxy,J2EEApplication=MarketingW_TESTPromotionsWSEAR,node=mw-gentest-001,J2EEName=MarketingW_TESTPromotionsWSEAR#PromotionsEJB.jar#PromotionsManager,j2eeType=StatelessSessionBean,J2EEServer=MarketingW_TEST_c1,Server=MarketingW_TEST_c1,version=7.0.0.9,type=StatelessSessionBean,mbeanIdentifier=MarketingW_TESTPromotionsWSEAR#PromotionsEJB.jar#PromotionsManager,EJBModule=PromotionsEJB.jar,cell=WAS7TEST,spec=1.0']


So, I had this:
b1 = AdminControl.queryNames( 'name=PromotionsManager,Server=MarketingW_TEST_c1,*').splitlines()
wsadmin>print b1
['WebSphere:name=PromotionsManager,process=MarketingW_TEST_c1,Application=MarketingW_TESTPromotionsWSEAR,platform=dynamicproxy,J2EEApplication=MarketingW_TESTPromotionsWSEAR,node=mw-gentest-001,J2EEName=MarketingW_TESTPromotionsWSEAR#PromotionsEJB.jar#PromotionsManager,j2eeType=StatelessSessionBean,J2EEServer=MarketingW_TEST_c1,Server=MarketingW_TEST_c1,version=7.0.0.9,type=StatelessSessionBean,mbeanIdentifier=MarketingW_TESTPromotionsWSEAR#PromotionsEJB.jar#PromotionsManager,EJBModule=PromotionsEJB.jar,cell=WAS7TEST,spec=1.0']

Open in new window

Then, I tried the following, with no luck:
wsadmin>print Help.operations( b1 )
WASX7015E: Exception running command: "Help.operations( b1 )"; exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
TypeError: operations(): 1st arg can't be coerced to String

Open in new window


If I try assigning 2 variables to the original AdminControl.queryNames call It looks like:
wsadmin>b1, b2 = AdminControl.queryNames( 'name=PromotionsManager,Server=MarketingW_TEST_c1,*').splitlines()
WASX7015E: Exception running command: "b1, b2 = AdminControl.queryNames( 'name=PromotionsManager,Server=MarketingW_TEST_c1,*').splitlines()"; exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
ValueError: unpack sequence too short

Open in new window




Clearly I am still missing something :(  Thoughts?
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 500 total points
Comment Utility
You are really close!

When you use the string splitlines() method, it returns a list.  That's why you see the MBean name surrounded by single quotes (meaning it's a string), and enclosed in square brackets (meaning it's a list).

After you get the list of MBeans:

beans = AdminControl.queryNames( 'name=PromotionsManager,Server=MarketingW_TEST_c1,*').splitlines()

You can (and should) verify that you have one, and only one!

if len( beans ) != 1 :
  print 'We have a problem:  %d beans matched our query' % len( beans )
  # ... if you're in a routine/function, use return, otherwise use sys.exit() ...
else :
  bean = beans[ 0 ];
  print Help.operations( bean );


0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Sory HonorGod, I know I'm making you work for these points, and if I could give you 10,000 I would :(

It's starting to look like I am NOT finding the Mbean I am looking for with this method.

I was speaking with one of the developers of the app, and they asked me to clarify, when I say MBean I'm actually referring to a JMX MBean, if that makes a difference...  Is there a special type that I should be looking at for these types of MBeans, and/or is what I am trying to accomplish still possible...?


Specifically, if it helps any, i got the following information from our developer:

the mbean is registered on server startup as "OURCacheUtil:name=Promotions_v2_KeyValueCache"



For what it's worth I tried:
wsadmin>beans = AdminControl.queryNames( '*' ).splitlines()
wsadmin>for b in beans:
wsadmin>   if b.find('KeyValueCache') > -1 :
wsadmin>      print b
wsadmin>

Open in new window


but came back with no hits...  

I also tried b.find('CacheUtil') and didn't get anything.


0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Thanks for the compliments.  ;-)

If the call to find() always comes back with -1, then the specified (case sensitive) text does not exist.
You are doing the right kind of test.

Remember, if the Application isn't started, then no MBeans will exist.

Has the Application been started?

If you are not certain about the exact string (i.e., which characters may, or may not be uppercase), you can change this test:

if b.find( 'KeyValueCache' ) > -1 :

to something like this:

if b.lower().find( 'keyvaluecache' ) > -1 :

Note how the MBean name is converted to lowercase before the comparison / test is made, so all of the characters in the literal string need to be lowercase.

Does this make sense?
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Ok...

The app is started.

I tried if b.lower().find( 'keyvaluecache' ) > -1 :
and that still didn't find what I was looking for.


Digging a little deeper, I found the developer can access this MBean through jconsole and it appears "out of the WebSphere realm" when you see it in the jconsole gui.  i.e. there is a WebSPhere folder that has a bunch of the stuff I am (loosely) familiar with...  and a separate folder at the same level that is labeled LLBCacheUtil.  Not sure if that is the root of my issues or not, but I will attach a screenshot  of the jconsole on a local developer machine to show what I mean...

 local jconsole screenshot
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:Bbouch
Comment Utility
ack.  uploaded wrong screenshot...  the one here is the one I meant to upload.. developer jconsole
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 500 total points
Comment Utility
Ah... "out of the websphere realm"... that makes it very difficult (if not impossible) for wsadmin scripts to access... :-(

You might be able to access via some of the scripting object jmx related methods, but I'm not 100% certain about this (having never done this before).
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Ok. that DOES make sense. Any idea how the MBEan being registered as part of app startup would get "outside" WebSphere?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
No, that's not something that I have seen before.

You probably have to ask your developers what they are doing, and how this is happening.
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
Will do.  I'll post a comment with whatever we find. Thanks again for all your help!  Some of this is definitely starting to sink in. Coming from a perl background, getting up to speed on this stuff is turning out to be a  much longer process than I had hoped for.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Learning is a journey, not a destination.  ;-)
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
After reviewing some more of the research, it looks like the way the MBean is registered is what was causing the issue.  The developers are going to look at a different method of registering the MBean which is supposed to make it accessible via wsadmin.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Wow, I would like to see what you learn.  Please let me know!

Thanks, and Happy New Year.
0
 
LVL 1

Author Comment

by:Bbouch
Comment Utility
We have a PMR open with IBM now. It appears on some application servers the MBEan is working as expected and on others (even within the same cluster) it is not.  PMR is to help understand why. I will update this thread when I find out more.  Thanks for your patience.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Thanks for sharing what you learn!
0
 
LVL 1

Accepted Solution

by:
Bbouch earned 0 total points
Comment Utility
Well, I still don't have a good answer to my original question, but I now understand why there was different behavior in our different environments.

In QA I had not enambled the following JVM argument:

-Dcom.sun.management.jmxremote


Once i added this, I was able to run our refresh cache mbean through jconsole, but i still have not been able to figure out how to script it in jython.

Given we probably won't dig any deeper into this issue at this point, i'm going to close the question at this point and call it good. Thanks for all your help on this HonorGod. I really do appreciate it.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
I'm sorry that we weren't able to more completely answer your question, but I appreciate your help, and assistance, and providing us with detail that I hope will be able to assist others.

Good luck & have a great day.
0
 
LVL 1

Author Closing Comment

by:Bbouch
Comment Utility
HonorGod provided a lot of useful information in regards to accessing Application MBeans through Jython, but ultimately we weren't able to come to a final resolution to my overall issue which is accessing a specific MBean. I did find one issue with my configuration, which was included in my last comment, should anyone else be trying to figure out how to do something similar.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
Thanks for the assist, the points, and the interesting question.

Good luck & have a great day.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresClump  challenge 31 107
for loop with Set 4 45
pairs challenge 5 44
Which is the latest version of Apache Tomcatl 3 35
This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
This article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now