javajmx

JMX NotificationListener IllegalArgumentException: Not a NotificationBroadcaster Object


I'm trying to create a Java application that monitors a JMX MBean and sends notifications when certain conditions are met. However, I'm encountering an IllegalArgumentException with the message:

Exception in thread "main" java.lang.IllegalArgumentException: The specified MBean ... is not a NotificationBroadcaster object.

Here's my code:

private static final String JMX_URL = "service:jmx:rmi:///jndi/rmi://IP:PORT/jmxrmi"; 
private static final int ALERT_THRESHOLD = 100;

private MBeanServerConnection connection;
private final NotificationListener listener = this;

public Monitor() throws Exception {
    Map<String, String[]> env = new HashMap<>();
    String[] credentials = {"USERNAME", "PASSWORD"};
    env.put(JMXConnector.CREDENTIALS, credentials);

    JMXServiceURL url = new JMXServiceURL(JMX_URL);
    JMXConnector connector = JMXConnectorFactory.connect(url, env);
    connection = connector.getMBeanServerConnection();
    ObjectName queueObjectName = new ObjectName("myMBeanName");
    connection.addNotificationListener(queueObjectName, listener, null, null);
}

@Override
public void handleNotification(Notification notification, Object handback) {
    // notification logic
}

I'm not sure why I'm getting this error.


Solution

  • The error message is telling you exactly what the problem is, i.e.:

    The specified MBean ... is not a NotificationBroadcaster object.
    

    In other words, the myMBeanName MBean does not implement the javax.management.NotificationBroadcaster interface.

    Instead of listening for notifications I believe you'll simply need to poll the MBean for its values and then sends your notifications when certain conditions are met.