Java ActiveMQ NotificationListener IllegalArgumentException:不是NotificationBroadcaster对象

问题描述 投票:0回答:1

我正在尝试创建一个 Java 应用程序,用于监视 ActiveMQ Classic 5.17 上的队列并在满足某些条件时发送通知。但是,我遇到了

IllegalArgumentException
,其中包含以下消息:

Exception in thread "main" java.lang.IllegalArgumentException: The specified MBean [org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName= QUEUE_LOG] is not a NotificationBroadcaster object.

这是我的代码:

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

private MBeanServerConnection connection;
private final NotificationListener listener = this;

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

    JMXServiceURL url = new JMXServiceURL(ACTIVEMQ_BROKER_URL);
    JMXConnector connector = JMXConnectorFactory.connect(url, env);
    connection = connector.getMBeanServerConnection();
    ObjectName queueObjectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + QUEUE_NAME);
    connection.addNotificationListener(queueObjectName, listener, null, null);
}

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

我不确定为什么会出现此错误。

java notifications activemq-classic message-queue
1个回答
0
投票

错误消息准确地告诉您问题是什么,即:

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

换句话说,

org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName= QUEUE_LOG
MBean 未实现
javax.management.NotificationBroadcaster
接口。

我相信您只需轮询 MBean 的值,然后在满足某些条件时发送通知,而不是监听通知。

© www.soinside.com 2019 - 2024. All rights reserved.