Thanks to the Original Author and source
How to find out how many nodes in the cluster
If you want to know how many nodes there are in the current cluster partition, all you have to do is to ask HAPartition for the node list. HAPartition represents your cluster partition, and it contains all the information you need to know about your cluster and the nodes: their host names, IPs, position in the cluster view.Lets assume you have a service bean that extends from HASingletonSupport. HASingletonSupport in its turn extends from HAServiceMBeanSupport.
HAServiceMBeanSupport is the one who gives you access to HAPartition object.
The code to request for HAPartition object and node list that you see below , you can put somewhere in your service bean:
HAPartition partition = getPartition();ClusterNode object represents your node in the cluster. It contains information about node's host name, its internet address and a few more things. getClusterNodes(), returns to you an array contains as many ClusterNode objects as you have currently in your cluster. So by getting the value of array length, you will know how many nodes your cluster has.
ClusterNode[] nodes = partition.getClusterNodes();
System.out.println(nodes.length);
Another way, is to do practically the same, but to request from a HAPartition a current view of your cluster:
HAPartition partition = getPartition();The view, which is a Vector contains information about node sockets. When printed, it will return to you a String representation of node ip + port: xxx.xxx.xxx.xxx:port. Also by printing size of the Vector, you will get number of nodes in the cluster.
Vector v = partition.getCurrentView();
System.out.println(partition.getCurrentView().size());
for (Object o : v) {
System.out.println(o.toString());
}
Important note:
I noticed there is some delay happening from the time when node leaves the cluster to the time when HAPartition returns an updated view. In another words - after node has left the cluster and topology change has occurred, the HAPartition may return to you an old view still containing the dead node. So be careful.
Also, getPartition() may return null, if super.startService() hasnt been called. Have a look at implementation of HAServiceMBeanSupport and my other post JBoss Clustering - HASingleton service.
No comments:
Post a Comment