Managing JBoss and WildFly with Groovy shell

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

The JBoss/WildFly CLI is quite powerful however that is not the only option available when we are managing the application server with shells. In this tutorial we will learn how to use the Groovy Shell to manage the application server.

Why should we replace the beautiful CLI ? Although powerful some limitations with the CLI exists, for example:

  • It's not possible to define variables in the CLI, although you can embed System Properties in your Scripts (See Using properties in CLI scripts)
  • It's not possible to define functions like in a programming language
  • It's not easy to manipulate complex logic with the logical operators available

Some years ago an useful post appeared on Jboss.org https://developer.jboss.org/wiki/AdvancedCLIScriptingWithGroovyRhinoJythonEtc which showed how you can execute CLI scripts which are coded in another scripting language. Today I wanted to check if these features are still available in the most recent version of the application server (WildFly 10.X) and if it's possible to execute CLI commands directly from the Groovy native shell.

Installing groovy

First al all, check that you have an available JDK on your system. The next step will be installing groovy. On a RHEL/Fedora box you would execute

$ yum install groovy

On an Ubuntu Debian you would install it as follows:

sudo apt-get install groovy

Done with the installation, let's execute the groovysh:

$ groovysh -cp /home/jboss/wildfly-10.1.0.Final/bin/client/jboss-cli-client.jar -Djava.security.manager -Djava.security.policy=security.policy

A couple of things to notice:

  • We are passing in Grovvy's classpath the jboss-cli-client.jar libraries which are needed to execute CLI commands from Groovy
  • As the jboss-cli JAR Libraries aren't signed, we will pass a permissive Security Manager to the shell, otherwise it will fail when executing methods from the JAR file.
grant {
	    permission java.security.AllPermission "", "";
};

Jboss cli functions groovy rhino

Great! now let's try to kick something. At first we will import the required script libraries:

groovy:000>  import org.jboss.as.cli.scriptsupport.*

Now let's connect to the CLI

groovy:001> cli = CLI.newInstance()
groovy:002> cli.connect()

Great we are in! let's execute a minimal command from the shell:

groovy:003> cli.cmd("connection-info")

As you can see, the shell correctly replies with the connection information:

Dec 07, 2016 5:14:08 PM org.jboss.as.cli.impl.CommandContextImpl printLine
INFO: Username               $local, granted role ["SuperUser"] 
Logged since           Wed Dec 07 16:17:37 CET 2016       
Not an SSL connection.                                    
Username               $local, granted role ["SuperUser"] 
Logged since           Wed Dec 07 16:17:37 CET 2016       
Not an SSL connection.  

Building a function

We wanted to build function, right? so we are in the right place ! We will create a simple function called db which collects statistics from a Datasource. The Datasource name is passed as parameter to the function. Cool isn't it ?

With groovy is pretty simple:

def db(name) {


cli = CLI.newInstance()  
cli.connect()  

cli.cmd("cd /subsystem=datasources/data-source=$name/statistics=pool")
result = cli.cmd(":read-resource(include-runtime=true")
response = result.getResponse()
println(response)
}

Now you can execute the function as follows:

groovy:000> db("ExampleDS")

As a result the Pool statistics from the ExampleDS Datasource will be displayed:

Dec 07, 2016 5:21:29 PM org.jboss.as.cli.impl.CommandContextImpl printLine
INFO: Warning! The CLI is running in a non-modular environment and cannot load commands from management extensions.
Warning! The CLI is running in a non-modular environment and cannot load commands from management extensions.
{
    "outcome" => "success",
    "result" => {
        "ActiveCount" => 0,
        "AvailableCount" => 0,
     . . . . . . . . . . . . .
     }
}

Building Classes with Groovy CLI

More powerful than functions we have classes. Classes are first citizens in Groovy and can hold functions and constructors like a standard Java Class. Let's see how we can turn our function in a Groovy class:

 class WFCLI {

  def cli = CLI.newInstance() 

  WFCLI() {   
    
   cli.connect()  
  }

  def db(name) {
 
 
   cli.cmd("cd /subsystem=datasources/data-source=$name/statistics=pool")
   def result = cli.cmd(":read-resource(include-runtime=true")
   def response = result.getResponse()
   println(response)
  }
  
 }

You can past the above class directly in the shell, it will be stored in the interpeter.

Notice we have to slightly change the style of our code, as when programming in Groovy classes we needed to define ("def operator") variables which are used in methods.

Now from the shell, instantiate a copy of your WFCLI class as follows:

groovy:000> c = new WFCLI()

And execute the method db:

groovy:000> c.db("ExampleDS")

You should see the same result as when the db function was executed.

That was pretty a quick dive into what you can do using Groovy as CLI interpeter. Personally I'd find terribly useful storing a set of pre-defined Groovy classes for performing repetitive administrative classes, such as creating Datasources or hardening the Security and so on. Have fun with Groovy and WildFly!

Related articles available on mastertheboss.com

How do I get the list of MBeans with twiddle?

  JBoss provides a simple command line tool that allows for inte

How to count active session with twiddle ?

  There are some well known alternatives to count the number of

How do I run garbage collector from shell?

  You can suggest JBoss to run the garbage collector in differen

How do I check my application status from shell?

  This is a two process step: at first you need to retrieve the

How do you shut down multiple JBoss instances?

  Supposing you have configured 2 instances of JBoss, one on por

twiddle reference guide

Please note: twiddle is not available any more as default Command

Follow us on Twitter