Tribal Knowledge Hub

Technical discoveries, solutions and musings from a long time IT professional.

Archive for May, 2013

Invoking WLST commands from Java

leave a comment

bea_logoI have been working with WebLogic Server since 1999 when it was still a BEA Systems product. Though professionally I  would characterize myself as an architect/developer, I have been working with WebLogic administrators for more than a decade. In fact, at BEA Systems I was the author of the original BEA WebLogic Administrator Certification exam. As a WebLogic consultant, the primary tools that I use with clients are the WebLogic Scripting Tool (WLST) and the WebLogic Diagnostic Framework (WLDF).

WLST is based on Jython, the all-Java version of Python. Since Jython runs in the JVM it is possible to invoke WLST commands directly form Java code. The possibilities are endless, you could use a Swing Gui front end and a WLST back end to provide automated management and monitoring for WebLogic.

Here is a simple example of WLST commands invoked from Java code. This code example uses a Runtime MBean to check how long the server has been running.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.IOException;
import java.io.OutputStream;

// from weblogic.jar
import org.python.core.PyLong;
import org.python.util.InteractiveInterpreter;
import weblogic.management.scripting.utils.WLSTInterpreter;

public class WSTLClient
{
  public static void main(String[] args)
  {
    InteractiveInterpreter interpreter = new WLSTInterpreter();    

    // WLST output to dev/null
    interpreter.setOut(new NullOutputStream());

    // Run commands no results
    interpreter.exec("connect('user','password','t3://host:port')");
    interpreter.exec("serverRuntime()");

    // Run command with result
    PyLong result = (PyLong)interpreter.eval("cmo.getActivationTime()");

    // calculate uptime
    long startTime = result.getValue().longValue();    
    long currentTime = System.currentTimeMillis();
    long uptimeMilli = currentTime - startTime;
    long uptimeMins = (uptimeMilli / 1000L) / 60L;

    System.out.println( "Server Uptime: " + uptimeMins + "min");
  }

  // custom class to iplement noop output stream
  public static class NullOutputStream extends OutputStream
  {
    @Override public void write(int b) throws IOException {}
    @Override public void write(byte[] b) throws IOException {}
    @Override public void write(byte[] b, int off, int len) throws IOException { }
  }
}

WLST documentation can be found here and here. Feel free to use this code as the basis for your own scripts.

Written by Dave

May 9th, 2013 at 10:56 pm

Posted in WebLogic Server