Skip to content

Instantly share code, notes, and snippets.

@shibli049
Last active July 25, 2018 11:42
Show Gist options
  • Save shibli049/db09ee516f818c6fbb036af6312c61d4 to your computer and use it in GitHub Desktop.
Save shibli049/db09ee516f818c6fbb036af6312c61d4 to your computer and use it in GitHub Desktop.
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 javax.naming.Context;
import java.io.IOException;
import java.net.MalformedURLException;
public class JMXClient {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static ObjectName service;
//Update correct port
private static String port = "7001";
//Update localhost if server is accessed remotely
private static String url = "t3://localhost:7001";
// Update hostname if server is accessed remotely
private static String hostname = "localhost";
private static String username = "weblogic";
private static String password = "weblogic";
// server name can differ from host name
private static String serverName = "Server-01";
/*
* Initialize connection to the Runtime MBean Server
*/
private static void init() throws IOException, MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(port);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.runtime";
//JMXServiceURL serviceURL = new JMXServiceURL(url);
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
Hashtable env = new Hashtable();
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, env);
connection = connector.getMBeanServerConnection();
service = new ObjectName("com.bea:Name="+serverName","+
"Type=ServerRuntime");
}
public static void getRuntimeInfo() {
System.out.println("Getting runtime info....");
try {
init();
String state = (String) connection.getAttribute(service, "State");
System.out.println("Server state: " + state);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void suspendServer() {
System.out.println("Suspending managed server....");
try {
init();
String state = (String) connection.getAttribute(service, "State");
System.out.println("Server state: " + state);
connection.invoke(service, "suspend", new Object[] {}, new String[] {});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void resumeServer() {
System.out.println("Resuming managed server....");
try {
init();
String state = (String) connection.getAttribute(service, "State");
System.out.println("Server state: " + state);
connection.invoke(service, "resume", new Object[] {}, new String[] {});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void stopServer() {
System.out.println("Shutting down managed server....");
try {
init();
String state = (String) connection.getAttribute(service, "State");
System.out.println("Server state: " + state);
connection.invoke(service, "forceShutdown", new Object[] {}, new String[] {});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void startServer() {
System.out.println("Starting managed server....");
try {
init();
String state = (String) connection.getAttribute(service, "State");
System.out.println("Server state: " + state);
connection.invoke(service, "start", new Object[] {}, new String[] {});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// Invoke required methods
JMXClient.getRuntimeInfo();
JMXClient.resumeServer();
JMXClient.getRuntimeInfo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment