Jython MBean

Carlos Quiroz

Revision History
Revision $Revision: 1.1 $$Date: 2002/02/24 11:02:40 $


The JythonRunner MBean can be used to execute jython scripts. The main objective is to use them for management tasks which are easiliy handled as scripts and that could be modified and deployed at runtime. The MBean's class is openjmx.tools.jython.JythonRunner.

You can use it in two ways:

  • By calling the runScript method

  • By listening notifications, for instance from a Timer or Monitor

The usage of the MBean requires that you have jython installed. You can get a copy at http://www.jython.org. Jython is distributed (as version 2.1) on a class file which is the installer and once execute will install the jython distribution, e.g. at /usr/local/jython-2.1 or C:\Java\jython-2.1. Jython can be run in two different ways depending on whether you want to use the jython standard library.

If you want to use the standard librarie, add the jython.jar as in the installation dir to the classpath. e.g.

java -cp /usr/local/jython-2.1/jython.jar:openjmx.jar:openjmx-tools.jar

In this way jython will be able to find the install dir and load the libraries from there. You can get the same effect by passing a python.path variable as:

java -Dpython.path=/usr/local/jython-2.1 -cp jython.jar:openjmx.jar:openjmx-tools.jar

If you don't need the standard libraries (and that can be the case for many JMX tasks, it is enough to have the jython.jar in your system classpath


The MBean has a few parameters which can be used to configure the MBean. This includes the ObservedObjectName and Notification Name used to configure the MBean to listen for notifications. The Script and ScriptFile parameters point to the content of the script. For simple scripts you could just use the Script parameter. For morke comple scripts you are better of pointing to a file. The ScriptFile is actually a URL and therefore can point to http locations

Built-in functions

To ease the script constructions some variables, class and functions are readily available to your script. The most important one is server which point to the current MBean server. The server variable IS a MBeanServer object and all methods are valid. You can write scripts like:

# write all the "test:name=test" mbean operations
[print operation for operation in server.getMBeanInfo(ObjectName("test:name=test")).operations]
Aditionally all the javax.management.* class are imported making objects like ObjectName or Attribute already available.

There are also some utility functions and classes:

  • proxy The proxy class wraps a objectname making it easier to access (It is equivalent to the StandardMBeanProxy class). You can directly retrieve and modify attributes and invoke mathods. For instance:

    # Assume the MBean with ObjectName Test:name=jython has name and time attributes and a method start()
    o = ObjectName("Test:name=jython")
    p = proxy(o)
    # you can access functions directly
    print p.name
    p.time = 12343
    # also works with functions

  • mbeans(query) Returns a list of the mbeans in the server which are in the query. The query is of the form "*:*". It can be omited and the function will return all the mbeans

  • instances(classname, query) Returns a list of the mbeans in the server which are instances of classname. The server accepts also an optional query method.