我正在使用Spring jmx
创建jmx
客户端,该客户端可以与Cassandra
群集交互以获取mbean对象属性Livedicsspaceused。
所以这个Cassandra
集群有3个节点,因此有不同的serviceUrl(每个都有不同的ip地址)。
现在我意识到在创建MBeanServerConnectionFactoryBean bean时我只能指定一个服务URl,如下所示:
@Bean
MBeanServerConnectionFactoryBean getConnector() {
MBeanServerConnectionFactoryBean mBeanfactory = new MBeanServerConnectionFactoryBean();
try {
mBeanfactory.setServiceUrl("serviceUrl1");
} catch (MalformedURLException e) {
e.printStackTrace();
}
mBeanfactory.setConnectOnStartup(false);
return mBeanfactory;
}
然后在main中我正在访问如下:
objectName = newObjectName(QueueServicesConstant.MBEAN_OBJ_NAME_LIVE_DISC_USED);
long count = (Long)mBeanFactory.getObject().getAttribute(objectName, QueueServicesConstant.MBEAN_ATTR_NAME_COUNT);
如何在所有三个节点中获得此值?
答案 0 :(得分:1)
您需要3个不同的连接器。
或者您可以使用类似Jolokia Proxy的内容来访问多个服务器(使用REST而不是JSR 160)。
答案 1 :(得分:0)
这就是我解决问题的方法..而不是使用Spring-JMX,我直接使用javax.management apis ..所以我的代码下面会得到任何一个连接器,这将足以为我提供正确的属性值但如果无法从一个服务器节点获取连接器,它将尝试连接到ohther节点。
@SuppressWarnings("restriction")
private Object getMbeanAttributeValue(String MbeanObectName,
String attributeName) throws IOException,
AttributeNotFoundException, InstanceNotFoundException,
MBeanException, ReflectionException, MalformedObjectNameException {
Object attributeValue = null;
JMXConnector jmxc = null;
try {
State state = metaTemplate.getSession().getState();
List<String> serviceUrlList = getJmxServiceUrlList(state
.getConnectedHosts());
jmxc = getJmxConnector(serviceUrlList);
ObjectName objectName = new ObjectName(MbeanObectName);
MBeanServerConnection mbsConnection = jmxc
.getMBeanServerConnection();
attributeValue = mbsConnection.getAttribute(objectName,
attributeName);
} finally {
if (jmxc != null)
jmxc.close();
}
return attributeValue;
}
// This will provide any one of the JMX Connector of cassandra cluster
@SuppressWarnings("restriction")
private JMXConnector getJmxConnector(List<String> serviceUrlList)
throws IOException {
JMXConnector jmxc = null;
for (String serviceUrl : serviceUrlList) {
JMXServiceURL url;
try {
url = new JMXServiceURL(serviceUrl);
jmxc = JMXConnectorFactory.connect(url, null);
return jmxc;
} catch (IOException e) {
log.error(
"getJmxConnector: Error while connecting to JMX sereice {} ",
serviceUrl, e.getMessage());
}
}
throw new IOException(
"Not able to connect to any of Cassandra JMX connector.");
}