Force Shutingdown WLS Using JMX

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import weblogic.management.runtime.*;
import javax.naming.Context;
import java.lang.*;

public class ShutdownServerUsingJMX
{
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
static
{
try {
service=new ObjectName(“com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean”);
}
catch (MalformedObjectNameException e)
{
throw new AssertionError(e.getMessage());
}
}
public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException
{

String protocol=”t3″;
Integer portInteger=Integer.valueOf(portString);
int port=portInteger.intValue();
String jndiroot=”/jndi/”;
String mserver=”weblogic.management.mbeanservers.domainruntime”;
JMXServiceURL serviceURL=new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);
Hashtable h=new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,”weblogic.management.remote”);
connector=JMXConnectorFactory.connect(serviceURL, h);
connection=connector.getMBeanServerConnection();
}

public static ObjectName[] getServerRuntimes() throws Exception
{
return (ObjectName[]) connection.getAttribute(service,”ServerRuntimes”);
}

public void printServerNames() throws Exception
{
ObjectName[] serverRT=getServerRuntimes();

for (int i=0;i < serverRT.length;i++)
{
String name=(String)connection.getAttribute(serverRT[i],”Name”);

System.out.println(“Server Name : ” + name +” SHUTTING DOWN”);
Object obj=connection.invoke(serverRT[i],”forceShutdown”, null, null);
System.out.println(“Server Name : ” + name +” SHUT DOWN”);

}
}

public static void main(String[] args) throws Exception
{

ShutdownServerUsingJMX shutDown=new ShutdownServerUsingJMX();
initConnection(“localhost”, “7001”, “weblogic”, “weblogic”);
shutDown.printServerNames();
connector.close();

}
}