Adding basic JMX support
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.couchbase.spring.monitor;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Base class to encapsulate common configuration settings.
|
||||
*/
|
||||
public abstract class AbstractMonitor {
|
||||
|
||||
private CouchbaseClient client;
|
||||
|
||||
protected AbstractMonitor(final CouchbaseClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public CouchbaseClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches stats for all nodes.
|
||||
*
|
||||
* @return stats for each node
|
||||
*/
|
||||
protected Map<SocketAddress,Map<String,String>> getStats() {
|
||||
return client.getStats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns stats for an individual node.
|
||||
*
|
||||
* @param node
|
||||
* @return
|
||||
*/
|
||||
protected Map<String, String> getStats(SocketAddress node) {
|
||||
return getStats().get(node);
|
||||
}
|
||||
|
||||
}
|
||||
33
src/main/java/com/couchbase/spring/monitor/ClusterInfo.java
Normal file
33
src/main/java/com/couchbase/spring/monitor/ClusterInfo.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.couchbase.spring.monitor;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.springframework.jmx.export.annotation.ManagedOperation;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
/**
|
||||
* Exposes basic cluster information.
|
||||
*/
|
||||
@ManagedResource(description = "Cluster Information")
|
||||
public class ClusterInfo extends AbstractMonitor {
|
||||
|
||||
public ClusterInfo(final CouchbaseClient client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
@ManagedOperation(description = "Cluster Hostnames")
|
||||
public String getHostNames() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (SocketAddress node : getStats().keySet()) {
|
||||
result.append(node.toString()).append(",");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@ManagedOperation(description = "Number of Nodes")
|
||||
public int getNumberOfNodes() {
|
||||
return getStats().keySet().size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,9 +35,11 @@ import org.springframework.data.annotation.Id;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@@ -145,6 +147,17 @@ public class CouchbaseTemplateTest {
|
||||
assertEquals(votes, response.getVotes());
|
||||
assertEquals(id, response.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void funStuff() {
|
||||
Map<SocketAddress,Map<String,String>> stats = client.getStats();
|
||||
for(Map.Entry<SocketAddress,Map<String,String>> entry : stats.entrySet()) {
|
||||
|
||||
for(Map.Entry<String, String> inner : entry.getValue().entrySet()) {
|
||||
System.out.println(inner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sample document with just an id and property.
|
||||
|
||||
Reference in New Issue
Block a user