# javax.management.remote

***

**1. Connect to a JMX Server**

```java
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

class ConnectToJMXServer {
    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();

        // Close the connector
        connector.close();
    }
}
```

**2. Create a JMX Server**

```java
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();
        }));
    }
}
```

**3. Invoke an Operation on a MBean**

```java
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();
    }
}
```

**4. Get Attribute Value from MBean**

```java
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();
    }
}
```

**5. Set Attribute Value in MBean**

```java
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();
    }
}
```

**6. Add Notification Listener to MBean**

```java
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();
    }
}
```

**7. Remove Notification Listener from MBean**

```java
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();
    }
}
```

\*\*8. Query MBeans with Query Exp
