Hi All,

Jay SenSharma

Jay SenSharma

Here is another sample of WebLogic Server JMX which allows us to Forcibly shuting down the Running WebLogic Server.

Many times there is a requirement in production or Development environments to shutdown all the Servers present inside a Domain. So manually doing the same task is very trouble some. Here is a simple Java Management extension script which can be used to ShutDown all the Servers present inside a Domain.  Just you need to run the “setWLSEnv.cmd” before compiling and Running the following code to shutdown all the servers present in Domain.

NOTE: in this JMX we have written code to just shut down the AdminServer you can remove the if{} block from  from Line No -60 the following JMX code to shutdown all the Servers rather than  shutting down Only the Admin Server.

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 AllServerShutDown
   {
	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
        {
		System.out.println("allServersShutDown in initConnection");
		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();
		System.out.println("=============SERVER NAMES=================");
		for (int i=0;i < serverRT.length;i++)
		{
			String name=(String)connection.getAttribute(serverRT[i],"Name");
			if(!name.equals("AdminServer"))
			{
				System.out.println("Server Name : " + name +" shutting down");
				Object obj=connection.invoke(serverRT[i],"forceShutdown", null, null);
				System.out.println("Server Name : " + name +" down now.");
			}

		}
  	}

	public static void main(String[] args) throws Exception
	{
		System.out.println("Shutting Down The Servers....");
		String hostname=args[0];
		String portString=args[1];
		String username=args[2];
		String password=args[3];

		System.out.println("Hostname : " + hostname);
		System.out.println("PortString : " + portString);
		System.out.println("Username : " + username);
		System.out.println("Password : " + password);

		AllServerShutDown allServersShutDown=new AllServerShutDown();
		initConnection(hostname, portString, username, password);
		allServersShutDown.printServerNames();
		connector.close();

	}
 }

Regards

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.