importjavax.management.remote.JMXConnector;importjavax.management.remote.JMXConnectorFactory;importjavax.management.remote.JMXServiceURL;classConnectToJMXServer{publicstaticvoidmain(String[]args)throwsException{ // Server address and portString host ="localhost";int port =9999; // JMX Service URLJMXServiceURL url =newJMXServiceURL("service:jmx:rmi:///jndi/rmi://"+ host +":"+ port +"/jmxrmi"); // Create a JMX ConnectorJMXConnector connector =JMXConnectorFactory.connect(url); // Get the MBean Server Connectionjavax.management.MBeanServerConnection mbsc =connector.getMBeanServerConnection(); // Close the connectorconnector.close();}}
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
class CreateJMXServer {
public static void main(String[] args) throws Exception {
// Create an MBean server
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
// Create a JMX Connector Server
JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
// Start the connector server
connectorServer.start();
// Stop the connector server when the JVM exits
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
connectorServer.stop();
}));
}
}
import javax.management.AttributeList;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
class InvokeOperation {
public static void main(String[] args) throws Exception {
// Server address and port
String host = "localhost";
int port = 9999;
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
// Create a JMX Connector
JMXConnector connector = JMXConnectorFactory.connect(url);
// Get the MBean Server Connection
javax.management.MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Create an ObjectName for the MBean
ObjectName mbeanName = new ObjectName("com.example:type=MyBean");
// Create an AttributeList for the operation parameters
AttributeList params = new AttributeList();
params.add(new Attribute("param1", "value1"));
// Invoke the operation
mbsc.invoke(mbeanName, "myOperation", params, null);
// Close the connector
connector.close();
}
}
import javax.management.Attribute;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
class GetAttributeValue {
public static void main(String[] args) throws Exception {
// Server address and port
String host = "localhost";
int port = 9999;
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
// Create a JMX Connector
JMXConnector connector = JMXConnectorFactory.connect(url);
// Get the MBean Server Connection
javax.management.MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Create an ObjectName for the MBean
ObjectName mbeanName = new ObjectName("com.example:type=MyBean");
// Get the attribute value
Attribute attribute = mbsc.getAttribute(mbeanName, "attributeName");
// Print the attribute value
System.out.println(attribute.getValue());
// Close the connector
connector.close();
}
}
import javax.management.Attribute;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
class SetAttributeValue {
public static void main(String[] args) throws Exception {
// Server address and port
String host = "localhost";
int port = 9999;
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
// Create a JMX Connector
JMXConnector connector = JMXConnectorFactory.connect(url);
// Get the MBean Server Connection
javax.management.MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Create an ObjectName for the MBean
ObjectName mbeanName = new ObjectName("com.example:type=MyBean");
// Create an Attribute for the new value
Attribute attribute = new Attribute("attributeName", "newValue");
// Set the attribute value
mbsc.setAttribute(mbeanName, attribute);
// Close the connector
connector.close();
}
}
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
class AddNotificationListener {
public static void main(String[] args) throws Exception {
// Server address and port
String host = "localhost";
int port = 9999;
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
// Create a JMX Connector
JMXConnector connector = JMXConnectorFactory.connect(url);
// Get the MBean Server Connection
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Create an ObjectName for the MBean
ObjectName mbeanName = new ObjectName("com.example:type=MyBean");
// Get the NotificationInfo for the MBean
MBeanNotificationInfo[] notificationsInfo = mbsc.getMBeanInfo(mbeanName).getNotifications();
// Create a NotificationListener
NotificationListener listener = new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
// Handle the notification here
}
};
// Add the listener to the MBean
mbsc.addNotificationListener(mbeanName, listener, null, null);
// Keep the connector open to receive notifications
// ...
// Remove the listener when done
mbsc.removeNotificationListener(mbeanName, listener);
// Close the connector
connector.close();
}
}
import javax.management.ListenerNotFoundException;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
class RemoveNotificationListener {
public static void main(String[] args) throws Exception {
// Server address and port
String host = "localhost";
int port = 9999;
// JMX Service URL
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
// Create a JMX Connector
JMXConnector connector = JMXConnectorFactory.connect(url);
// Get the MBean Server Connection
javax.management.MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Create an ObjectName for the MBean
ObjectName mbeanName = new ObjectName("com.example:type=MyBean");
// Create a NotificationListener
NotificationListener listener = new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
// Handle the notification here
}
};
// Remove the listener from the MBean
mbsc.removeNotificationListener(mbeanName, listener);
// Close the connector
connector.close();
}
}