Managing Oracle Weblogic with WLST - Running WLST in Embedded mode

 

Running WLST in Embedded mode

In embedded mode, you instantiate the WLST interpreter in your Java code and use it to run WLST commands and scripts. All WLST commands and variables that you use in interactive and script mode can be also run in embedded mode.

The following example shows how to connect WLST to a running server, create two servers, assign them to a newly created cluster and exit.

import java.util.*;

import weblogic.management.scripting.utils.WLSTInterpreter;

import org.python.util.InteractiveInterpreter;

 

public class EmbeddedWLST

{

  static InteractiveInterpreter interpreter = null;

  EmbeddedWLST() {

    interpreter = new WLSTInterpreter();

  }

 

private static void connect() {

    StringBuffer buffer = new StringBuffer();

    buffer.append("connect('weblogic','welcome1')");

    interpreter.exec(buffer.toString());

  }

 

private static void createServers() {

    StringBuffer buf = new StringBuffer();

    buf.append(startTransaction());

    buf.append("man1=create('msEmbedded1','Server')\n");

    buf.append("man2=create('msEmbedded2','Server')\n");

    buf.append("clus=create('clusterEmbedded','Cluster')\n");

    buf.append("man1.setListenPort(8001)\n");

    buf.append("man2.setListenPort(9001)\n");

    buf.append("man1.setCluster(clus)\n");

    buf.append("man2.setCluster(clus)\n");

    buf.append(endTransaction());

    buf.append("print 'Script ran successfully ...' \n");

    interpreter.exec(buf.toString());

  }

private static String startTransaction() {

    StringBuffer buf = new StringBuffer();

    buf.append("edit()\n");

    buf.append("startEdit()\n");

    return buf.toString();

  }

private static String endTransaction() {

    StringBuffer buf = new StringBuffer();

    buf.append("save()\n");

    buf.append("activate(block='true')\n");

    return buf.toString();

  }

public static void main(String[] args) {

    new EmbeddedWLST();

    connect();

    createServers();

  }

}

As you can see, the embedded mode consists in passing WLST commands as String to the weblogic.management.scripting.utils.WLSTInterpreter class. This kind of mode can be useful for developers that want to spice their application with some administration tasks, although it is discouraged for system administrators as it will be harder to maintain.

 

Francesco Google+