这通常是由于配置不一致或系统设置造成的。可以尝试在应用程序中启用调试模式并检查是否有错误。同时,可以在代码中使用以下示例来访问ActiveMQ jolokia:
import javax.management.*;
import org.apache.activemq.broker.jmx.*;
public class JolokiaTest {
public static void main(String[] args) throws Exception {
String targetHost = "localhost";
int targetPort = 8161; // change this to the appropriate port
BrokerService brokerService = new BrokerService();
brokerService.start();
JmxJolokiaAgent jolokiaAgent = new JmxJolokiaAgent();
jolokiaAgent.setBrokerService(brokerService);
jolokiaAgent.start();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" +
targetHost + ":" + targetPort + "/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
String attribute = "TotalProducerCount";
Object producerCount = mbsc.getAttribute(mbeanName, attribute);
System.out.println("Producer Count = " + producerCount.toString());
jmxc.close();
brokerService.stop();
}
}
注意更改 targetHost
和 targetPort
,如果需要的话,还需更改 attribute
来获取适当的参数。这应该能够避免在获取ActiveMQ jolokia响应时出现不一致的问题。