Wednesday, May 23, 2012

Weblogic 10.3.5: WLST in Embedded Mode

Weblogic 10.3.5: 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 run in embedded mode.

This example illustrates how to instantiate the WLST interpreter and use it to connect to a running server, create two servers, and assign them to clusters.


package wlst;
import java.util.*;
import weblogic.management.scripting.utils.WLSTInterpreter;
import org.python.util.InteractiveInterpreter;

/**
 * Simple embedded WLST example that will connect WLST to a running server,
 * create two servers, and assign them to a newly created cluster and exit.
 * <p>Title: EmbeddedWLST.java</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: BEA Systems</p>
 */

public class EmbeddedWLST 
{
  static InteractiveInterpreter interpreter = null;

  EmbeddedWLST() {
    interpreter = new WLSTInterpreter();
  }

private static void connect() {
    
  String user="user1";
  String pass="pw12ab";
  String url ="t3://localhost:7001";
  Environment env = new Environment();
  env.setProviderUrl(url);
  env.setSecurityPrincipal(user);
  env.setSecurityCredentials(pass);
  Context ctx = env.getInitialContext();
  interpreter.exec
   ("connect('"+user+"','"+pass+"','"+url+"')");
  }

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();
  }
}

Reference: http://docs.oracle.com/cd/E13222_01/wls/docs90/config_scripting/using_WLST.html 

OCI Knowledge Series: OCI Infrastructure components

  Oracle Cloud Infrastructure (OCI) provides a comprehensive set of infrastructure services that enable you to build and run a wide range of...