Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

weblogic server state using embedded form of WLST

 
Pavan Maram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to WLST. I want to get a weblogic server state using wlstInterpreter in java code.

I am creating a new instance of weblogic.management.scripting.utils.WLSTInterpreter and using it to print state of a given server. I am using the following code to print server state in java file
interpreter.exec("connect(" + "'" + username + "'" + "," + "'" + password + "'" + "," + "'" + ip + "'" + ")");
interpreter.exec("state(" + "'" + serverName + "'" + ", 'Server') \n");

The state is getting printed in output console but I want the state to be stored in java string variable so that I can use the state in java code.

Please help me how to get the server state and store it in a java string variable using wlst embedded mode.
 
kamal kannan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Give full coding of your program
 
Pavan Maram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The issue that I posted got solved. For getting server state, I am setting the interpreter out to ByteArrayOutputStream and redaing from that to get state of server and then resetting back the interpreter outstream to system.out This helped me to get and use the server state for restarting the server.
I am using the following code to restart the weblogic server.

import java.io.ByteArrayOutputStream;

import org.python.util.InteractiveInterpreter;

import weblogic.management.runtime.ServerStates;
import weblogic.management.scripting.utils.WLSTInterpreter;

/**
* This class is wlst interpreter helper for restarting server
*/
public class WlServerController
{
//max time to wait for the server to shutdown
public static final int MAX_SHUTDOWN_TIME = 360000;

static InteractiveInterpreter interpreter = null;

static
{
interpreter = new WLSTInterpreter();
}

private static void connect(String username, String password, String ip)
{
StringBuffer buffer = new StringBuffer();
buffer.append("connect(" + "'" + username + "'" + "," + "'" + password
+ "'" + "," + "'" + ip + "'" + ")");
interpreter.exec(buffer.toString());
}

public static void restartServer(String args[]) throws Throwable
{
if (args != null && args.length == 4) {
String ip = args[0];
String username = args[1];
String password = args[2];
String serverName = args[3];


System.out.println("***serverName::::" + serverName);
//connect to the Admin Server
connect(username, password, ip);

//shutdown the required server.
StringBuffer stopCommand = new StringBuffer();
stopCommand.append("shutdown(" + "'" + serverName + "'"
+ ", 'Server', force='true') \n");
interpreter.exec(stopCommand.toString());

long startTime = System.currentTimeMillis();
while(true) {
//if the server is shutdown or max time exceeded, break
if (isServerShutDown(serverName)
|| System.currentTimeMillis() - startTime > MAX_SHUTDOWN_TIME) {
break;
}
Thread.currentThread().sleep(5000);
}

//restart the required server
final StringBuffer startCommand = new StringBuffer();
startCommand.append("start(" + "'" + serverName + "'"
+ ", 'Server') \n");
interpreter.exec(startCommand.toString());

} else {
System.out.println("Usage : java WlServerController ip username password serverName");
}

}

public static boolean isServerShutDown(String serverName) {
try {
// get the state of the server and store it in outputstream.
ByteArrayOutputStream out = new ByteArrayOutputStream();
interpreter.setOut(out);
interpreter.exec("state(" + "'" + serverName + "'"
+ ", 'Server') \n");
System.out.println("****" + out.toString());
String output = out.toString();
String serverState = null;
if (output != null && output.contains(":")
&& output.length() > output.indexOf(":") + 1) {
serverState = output.substring(output.indexOf(":") + 1);
}
if (serverState != null
&& ServerStates.SHUTDOWN.equals(serverState.trim())) {
return true;
}

return false;
} catch (Exception e) {
return false;
} finally {
interpreter.setOut(System.out);
}
}

public static void main(String args[]) {
try {
restartServer(args);
System.exit(0);
} catch(Throwable t) {
System.out.println("***Unable to restart the server");
t.printStackTrace();
}
}


}

Please let me know if there is any other better alternative to restart the weblogic server using java code.
Thanks
Pavan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic