The JBoss AS7 uses the CLI (Command Line Interface) as main management instrument. You can write complex management script using its own shell public API. However since the release 7.2 of JBoss AS you can also use many other languages to manage the application server. This tutorial shows how to use Jython to manage JBoss AS 7.
JBoss AS 7 introduces the scriptsupport.CLI class that acts as a facade for the CLI public API. Thanks to this API and the new CLI remote client jar (located in the JBOSS_HOME/bin/client folder) you can execute CLI commands using lots of different languages such as Jython, Groovy or Rhino. Let's see how to use Jython in this tutorial.
Jython is an implementation of Python for the JVM. Jython is extremely useful because it provides the productivity features of a mature scripting language while running on a JVM. Unlike a Python program, a Jython program can run in any environment that supports a JVM. Besides this, Jython is also the management choice for other main application servers (Oracle Weblogic, IBM Websphere) so it's really worth to learn at least some basics of it, and you will be able to manage all application servers.
Jython is invoked using the "jython" script, a short script that invokes your local JVM, sets the Java property install.path to an appropriate value, and then runs the Java classfile org.python.util.jython.
The first thing you have to do, in order to get started is downloading jython installer from http://www.jython.org/downloads.html
Run the installer with:
java -jar jython_installer-2.5.2.jar
Next, add JYTHON_HOME/bin folder (e.g. C:\jython2.5.3\bin) to the system path
Ok, now before starting, you need to add jboss-cli-client.jar to the system CLASSPATH. For example, on Windows:
Ok, now we will create our first script which basically will return the JNDI view of our application server. Create a file named for example script.py
from java.util import Date from org.jboss.as.cli.scriptsupport import CLI cli = CLI.newInstance() cli.connect() cli.cmd("cd /subsystem=naming") result = cli.cmd(":jndi-view") response = result.getResponse() print print 'JNDI VIEW======================= ' print response cli.disconnect()
now execute the script with:
As you can see, the code is much self-explanitory and they use the org.jboss.as.cli.scriptsupport.CLI class to send commands and read the response.
A more complex example can be found in the JBoss AS 7 wiki (https://community.jboss.org/wiki/AdvancedCLIScriptingWithGroovyRhinoJythonEtc) which will show some conditional execution, printing the server uptime:
from java.util import Date from org.jboss.as.cli.scriptsupport import CLI cli = CLI.newInstance() cli.connect() if cli.getCommandContext().isDomainMode(): cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime") else: cli.cmd("cd /core-service=platform-mbean/type=runtime") result = cli.cmd(":read-attribute(name=start-time)") response = result.getResponse() startTime = response.get("result").asLong() result = cli.cmd(":read-attribute(name=uptime)") response = result.getResponse() serveruptime = response.get("result").asString() print print 'The server was started on ' + Date(startTime).toString() print 'It has been running for ' + serveruptime + 'ms' cli.disconnect()
Enjoy using JBoss AS 7 and Python!