Secondly, two options need to be set on each server in the domain:
- You have to enable tunneling. You do this by ticking “Enable Tunneling” under Domain > Environment > Servers > “Server” > Protocols > General
- You have to ignore http sessions during shutdown, otherwise the server keeps on waiting for ever until no more http sessions are active. Enable the option “Ignore Sessions During Shutdown” under Domain > Environment > Servers > “Server” > Control > Start/Stop
Code
package be.i8c.systemtools.weblogic;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.runtime.ServerStates;
public class ServerStopper
{
public static void stop()
{
Properties prop = System.getProperties();
try
{
MBeanHome home = null;
//url of the Admin server
String url = “”;
if (prop.getProperty(“weblogic.management.server”) != null)
{
// this property will be set automatically on a managed server to indicate the location of the admin server
url = prop.getProperty(“weblogic.management.server”);
}
else
{
// the admin.url is a property to indicate the location of the admin server on the admin server. It refers to itself, but is necessary to perform a proper shutdown. You can add it manually as a –D property when you install the server as a windows service. The name of the property can be any random name.
url = prop.getProperty(“admin.url”);
}
// username and password to perform the shutdown operation
username = “username”;
password = “password”;
ServerRuntimeMBean serverRuntime = null;
Set mbeanSet = null;
Iterator mbeanIterator = null;
// Set ContextClassloader to prevent assertions
URL[] urls = { new File(“/”).toURL() };
Thread.currentThread().setContextClassLoader(new URLClassLoader(urls));
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
home = (MBeanHome)ctx.lookup(“weblogic.management.adminhome”);
mbeanSet = home.getMBeansByType(“ServerRuntime”);
mbeanIterator = mbeanSet.iterator();
while(mbeanIterator.hasNext())
{
serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
try
{
// we have to add this check, since the iterator will list all the servers, both admin and managed. If we don’t add this check a shutdown of all the servers will be performed in case we only stop one server
if (serverRuntime.getName().equalsIgnoreCase(prop.getProperty(“weblogic.Name”)))
{
if(serverRuntime.getState().equals(ServerStates.RUNNING))
{
// we perform a normal shut down in case the server is in the RUNNING state
serverRuntime.shutdown();
}
else
{
//This is the same as a forced shutdown, but not the same as killing the JVM. We don’t do a normal shutdown in case the server is not in the RUNNING state.
System.exit(10);
}
}
}
catch (Exception e)
{
// You can do nothing over here. In case the shutdown call will fail, windows will wait for the service to end, but that won’t happen. It will kill the service anyway after a while.
}
}
}
catch (Exception e)
{
// We get here in case the admin server couldn’t be reached
System.exit(10);
}
}
}
Author: Dimitri Van Kerckhoven