Wednesday, August 26, 2009

Faster WebSphere wsadmin Jython scripting with imports

When using wsadmin to run Jython scripts I'm seeing a 30 sec startup time, which is too long for my edit-run-test cycle. Here's a way of avoiding the startup delay.

This method uses a simple boot script to launch a read-execute-print-loop (REPL) which you use for your own work. wsadmin does not set up the modules it's imported properly. This you need to fix in your top-level script. Here's the invocation:
    ${path-to-wsadmin}/wsadmin.sh -lang jython -f ./repl.py
Here's repl.py
import sys
sys.modules['AdminConfig'] = AdminConfig

import code
repl = code.InteractiveConsole()
repl.interact("jython %s console" % (sys.version))

When you run this, you get a prompt:

jython 2.1 console
>>>
Your scripts need to import the relevant Admin module. e.g. 'foo.py' starts with

import AdminConfig
class Foo:
...
So when you the script with execfile, it picks up the wsadmin modules:

>>> execfile('foo.py')
or

>>> import foo
>>> reload foo
Now you can edit and run 'foo.py' without relaunching wsadmin.

2 comments:

Anonymous said...

Hi!

Nice post, seems to be very usefull.

Unfortunately, I couldn't exit that Jython prompt using quit, quit(), exit nor exit(). Please, how do you do this?

Thanks!

Bill Birch said...

import java.lang.Runtime

java.lang.Runtime.getRuntime().exit(0)