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.