Wednesday, March 05, 2003

Weblogic: Starting ManagedServer Programmatically

Weblogic6.1 Node Manager do *not* attempt to restart a Managed Server when its goes down or provide a mechanism where user can use some compand line tool to start the managed server using Node Manager. The following is the example code to start a managed server using nodemanager programmatically. This code when used in combination with script that detects when a server goes down (Example: SNMP management tools which get a trap when managed server goes down), will automate server restart when a problem occurs.

Tested with weblogic6.1SP4.

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;


* This tools starts a managed server when node manager corresponding
* to that server is already running.
* Usage : StartManagedServer -adminurl <> -user <> -pass <> -domain <> -managedserver <>

public class StartManagedServer {

public static void main(String[] args) {

ServerMBean serverMBean = null;

String url = "t3://localhost:7001";
String username = "system";
String password = "password";
String domain = "yourdomain";
String managedServer = "Server1";

int i = 0;

try {

for (; i < args.length; i++) {

if (args[i].equals("-adminurl"))
url = args[++i];

else if (args[i].equals("-user"))
username = args[++i];

else if (args[i].equals("-pass"))
password = args[++i];

else if (args[i].equals("-domain"))
domain = args[++i];

else if (args[i].equals("-managedserver"))
managedServer = args[++i];
} catch (ArrayIndexOutOfBoundsException aio) {
log("Missing value for arguement '" + args[--i] + "'");
log("Usage : StartManagedServer -adminurl <> -user <> -pass <> -domain <> -managedserver <>");

try {

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL, url);
props.put(Context.SECURITY_PRINCIPAL, username);
props.put(Context.SECURITY_CREDENTIALS, password);
Context ctx = new InitialContext(props);

MBeanHome home = (MBeanHome) ctx.lookup("");
serverMBean = (ServerMBean) home.getMBean(managedServer, "Server", domain);

} catch (Exception e) {
log("Exception in getting Admin home & serverMBean: " + e);

try {
log("Sending a start command to the AdminServer ...");
} catch (weblogic.rmi.extensions.RemoteRuntimeException rre) {
//The returned which is not serializable :-)
log("Ignorable Exception occured ...");
} catch (Exception e) {
//InsufficientConfigurationException?? Couldn't find JavaDocs for exception
//mentioned in javadocs.:-)
log("Unexpected exception: " + e);

log("NodeManager initiated the server");

private static void log(String s) {

Starting weblogic7 Node Manager attempts to restart a Managed Server whose health state is "failed".

No comments: