aspose file tools*
The moose likes BEA/Weblogic and the fly likes weblogic server state using embedded form of WLST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "weblogic server state using embedded form of WLST" Watch "weblogic server state using embedded form of WLST" New topic
Author

weblogic server state using embedded form of WLST

Pavan Maram
Greenhorn

Joined: May 28, 2009
Posts: 2
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

Joined: Oct 06, 2006
Posts: 33
hi
Give full coding of your program


SCJP 1.5 95% /SCWCD 1.5 93% /RHCE EL4 100% /WEBLOGIC 9.2 SYSTEM ADMINISTRATION 100% / Oracle Weblogic 10g Server Certified System Export 88%
Pavan Maram
Greenhorn

Joined: May 28, 2009
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: weblogic server state using embedded form of WLST