Example usage for javax.management JMX newMBeanProxy

List of usage examples for javax.management JMX newMBeanProxy

Introduction

In this page you can find the example usage for javax.management JMX newMBeanProxy.

Prototype

public static <T> T newMBeanProxy(MBeanServerConnection connection, ObjectName objectName,
        Class<T> interfaceClass) 

Source Link

Document

Make a proxy for a Standard MBean in a local or remote MBean Server.

If you have an MBean Server mbs containing an MBean with ObjectName name , and if the MBean's management interface is described by the Java interface MyMBean , you can construct a proxy for the MBean like this:

 MyMBean proxy = JMX.newMBeanProxy(mbs, name, MyMBean.class); 

Suppose, for example, MyMBean looks like this:

 public interface MyMBean { public String getSomeAttribute(); public void setSomeAttribute(String value); public void someOperation(String param1, int param2); } 

Then you can execute:

  • proxy.getSomeAttribute() which will result in a call to mbs.

    Usage

    From source file:com.adaptris.core.runtime.AdapterRegistryTest.java

    public void testProxy_Destroy_With_GetConfigurationURL() throws Exception {
        AdapterRegistry adapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, adapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        File filename = deleteLater(adapter);
        DefaultMarshaller.getDefaultMarshaller().marshal(adapter, filename);
        URLString expectedURL = new URLString(filename);
        ObjectName objName = registry.createAdapter(expectedURL);
        assertNotNull(objName);//from  w ww.j  a  v a 2s .c o m
        assertTrue(mBeanServer.isRegistered(objName));
        assertEquals(expectedURL, registry.getConfigurationURL(objName));
        registry.destroyAdapter(objName);
        assertEquals(expectedURL, registry.getConfigurationURL(objName));
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    /**
     * Create a connection to the JMX agent and setup the M[X]Bean proxies.
     *
     * @throws IOException on connection failures
     *///from   ww  w .jav  a  2s.c  o  m
    private void connect() throws IOException {
        JMXServiceURL jmxUrl = new JMXServiceURL(String.format(fmtUrl, host, port));
        Map<String, Object> env = new HashMap<String, Object>();
        if (username != null) {
            String[] creds = { username, password };
            env.put(JMXConnector.CREDENTIALS, creds);
        }
    
        env.put("com.sun.jndi.rmi.factory.socket", getRMIClientSocketFactory());
    
        jmxc = JMXConnectorFactory.connect(jmxUrl, env);
        mbeanServerConn = jmxc.getMBeanServerConnection();
    
        try {
            ObjectName name = new ObjectName(ssObjName);
            ssProxy = JMX.newMBeanProxy(mbeanServerConn, name, StorageServiceMBean.class);
            name = new ObjectName(MessagingService.MBEAN_NAME);
            msProxy = JMX.newMBeanProxy(mbeanServerConn, name, MessagingServiceMBean.class);
            name = new ObjectName(StreamManagerMBean.OBJECT_NAME);
            streamProxy = JMX.newMBeanProxy(mbeanServerConn, name, StreamManagerMBean.class);
            name = new ObjectName(CompactionManager.MBEAN_OBJECT_NAME);
            compactionProxy = JMX.newMBeanProxy(mbeanServerConn, name, CompactionManagerMBean.class);
            name = new ObjectName(FailureDetector.MBEAN_NAME);
            fdProxy = JMX.newMBeanProxy(mbeanServerConn, name, FailureDetectorMBean.class);
            name = new ObjectName(CacheService.MBEAN_NAME);
            cacheService = JMX.newMBeanProxy(mbeanServerConn, name, CacheServiceMBean.class);
            name = new ObjectName(StorageProxy.MBEAN_NAME);
            spProxy = JMX.newMBeanProxy(mbeanServerConn, name, StorageProxyMBean.class);
            name = new ObjectName(HintedHandOffManager.MBEAN_NAME);
            hhProxy = JMX.newMBeanProxy(mbeanServerConn, name, HintedHandOffManagerMBean.class);
            name = new ObjectName(GCInspector.MBEAN_NAME);
            gcProxy = JMX.newMBeanProxy(mbeanServerConn, name, GCInspectorMXBean.class);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException("Invalid ObjectName? Please report this as a bug.", e);
        }
    
        memProxy = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn, ManagementFactory.MEMORY_MXBEAN_NAME,
                MemoryMXBean.class);
        runtimeProxy = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn,
                ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    public CacheServiceMBean getCacheServiceMBean() {
        String cachePath = "org.apache.cassandra.db:type=Caches";
    
        try {//from  www  .  ja  v a 2s  . co  m
            return JMX.newMBeanProxy(mbeanServerConn, new ObjectName(cachePath), CacheServiceMBean.class);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    public void setCacheCapacities(int keyCacheCapacity, int rowCacheCapacity, int counterCacheCapacity) {
        try {/*from www  .j  a  v  a 2  s. com*/
            String keyCachePath = "org.apache.cassandra.db:type=Caches";
            CacheServiceMBean cacheMBean = JMX.newMBeanProxy(mbeanServerConn, new ObjectName(keyCachePath),
                    CacheServiceMBean.class);
            cacheMBean.setKeyCacheCapacityInMB(keyCacheCapacity);
            cacheMBean.setRowCacheCapacityInMB(rowCacheCapacity);
            cacheMBean.setCounterCacheCapacityInMB(counterCacheCapacity);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    public void setCacheKeysToSave(int keyCacheKeysToSave, int rowCacheKeysToSave, int counterCacheKeysToSave) {
        try {/*  ww w  .  ja va  2  s .  c o  m*/
            String keyCachePath = "org.apache.cassandra.db:type=Caches";
            CacheServiceMBean cacheMBean = JMX.newMBeanProxy(mbeanServerConn, new ObjectName(keyCachePath),
                    CacheServiceMBean.class);
            cacheMBean.setKeyCacheKeysToSave(keyCacheKeysToSave);
            cacheMBean.setRowCacheKeysToSave(rowCacheKeysToSave);
            cacheMBean.setCounterCacheKeysToSave(counterCacheKeysToSave);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    public EndpointSnitchInfoMBean getEndpointSnitchInfoProxy() {
        try {/*from ww  w .  ja v a 2  s  .  co m*/
            return JMX.newMBeanProxy(mbeanServerConn,
                    new ObjectName("org.apache.cassandra.db:type=EndpointSnitchInfo"),
                    EndpointSnitchInfoMBean.class);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    public ColumnFamilyStoreMBean getCfsProxy(String ks, String cf) {
        ColumnFamilyStoreMBean cfsProxy = null;
        try {// www  .  j a  va 2 s  .  c o  m
            String type = cf.contains(".") ? "IndexColumnFamilies" : "ColumnFamilies";
            Set<ObjectName> beans = mbeanServerConn.queryNames(
                    new ObjectName(
                            "org.apache.cassandra.db:type=*" + type + ",keyspace=" + ks + ",columnfamily=" + cf),
                    null);
    
            if (beans.isEmpty())
                throw new MalformedObjectNameException("couldn't find that bean");
            assert beans.size() == 1;
            for (ObjectName bean : beans)
                cfsProxy = JMX.newMBeanProxy(mbeanServerConn, bean, ColumnFamilyStoreMBean.class);
        } catch (MalformedObjectNameException mone) {
            System.err.println("ColumnFamilyStore for " + ks + "/" + cf + " not found.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("ColumnFamilyStore for " + ks + "/" + cf + " not found: " + e);
            System.exit(1);
        }
    
        return cfsProxy;
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    /**
     * Retrieve cache metrics based on the cache type (KeyCache, RowCache, or CounterCache)
     * @param cacheType KeyCach, RowCache, or CounterCache
     * @param metricName Capacity, Entries, HitRate, Size, Requests or Hits.
     *//*from   w w w  .j a v  a2s  .  c  o  m*/
    public Object getCacheMetric(String cacheType, String metricName) {
        try {
            switch (metricName) {
            case "Capacity":
            case "Entries":
            case "HitRate":
            case "Size":
                return JMX.newMBeanProxy(mbeanServerConn, new ObjectName(
                        "org.apache.cassandra.metrics:type=Cache,scope=" + cacheType + ",name=" + metricName),
                        JmxReporter.GaugeMBean.class).getValue();
            case "Requests":
            case "Hits":
                return JMX.newMBeanProxy(mbeanServerConn, new ObjectName(
                        "org.apache.cassandra.metrics:type=Cache,scope=" + cacheType + ",name=" + metricName),
                        JmxReporter.MeterMBean.class).getCount();
            default:
                throw new RuntimeException("Unknown cache metric name.");
    
            }
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    /**
     * Retrieve ColumnFamily metrics//from   ww w .j  a  v a2 s  .  co  m
     * @param ks Keyspace for which stats are to be displayed.
     * @param cf ColumnFamily for which stats are to be displayed.
     * @param metricName View {@link org.apache.cassandra.metrics.ColumnFamilyMetrics}.
     */
    public Object getColumnFamilyMetric(String ks, String cf, String metricName) {
        try {
            String type = cf.contains(".") ? "IndexColumnFamily" : "ColumnFamily";
            ObjectName oName = new ObjectName(String.format(
                    "org.apache.cassandra.metrics:type=%s,keyspace=%s,scope=%s,name=%s", type, ks, cf, metricName));
            switch (metricName) {
            case "BloomFilterDiskSpaceUsed":
            case "BloomFilterFalsePositives":
            case "BloomFilterFalseRatio":
            case "BloomFilterOffHeapMemoryUsed":
            case "IndexSummaryOffHeapMemoryUsed":
            case "CompressionMetadataOffHeapMemoryUsed":
            case "CompressionRatio":
            case "EstimatedColumnCountHistogram":
            case "EstimatedRowSizeHistogram":
            case "EstimatedRowCount":
            case "KeyCacheHitRate":
            case "LiveSSTableCount":
            case "MaxRowSize":
            case "MeanRowSize":
            case "MemtableColumnsCount":
            case "MemtableLiveDataSize":
            case "MemtableOffHeapSize":
            case "MinRowSize":
            case "RecentBloomFilterFalsePositives":
            case "RecentBloomFilterFalseRatio":
            case "SnapshotsSize":
                return JMX.newMBeanProxy(mbeanServerConn, oName, JmxReporter.GaugeMBean.class).getValue();
            case "LiveDiskSpaceUsed":
            case "MemtableSwitchCount":
            case "SpeculativeRetries":
            case "TotalDiskSpaceUsed":
            case "WriteTotalLatency":
            case "ReadTotalLatency":
            case "PendingFlushes":
                return JMX.newMBeanProxy(mbeanServerConn, oName, JmxReporter.CounterMBean.class).getCount();
            case "ReadLatency":
            case "CoordinatorReadLatency":
            case "CoordinatorScanLatency":
            case "WriteLatency":
                return JMX.newMBeanProxy(mbeanServerConn, oName, JmxReporter.TimerMBean.class);
            case "LiveScannedHistogram":
            case "SSTablesPerReadHistogram":
            case "TombstoneScannedHistogram":
                return JMX.newMBeanProxy(mbeanServerConn, oName, JmxReporter.HistogramMBean.class);
            default:
                throw new RuntimeException("Unknown column family metric.");
            }
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }
    

    From source file:org.apache.cassandra.tools.NodeProbe.java

    /**
     * Retrieve Proxy metrics//w ww . j  a  v  a  2 s . c  o  m
     * @param scope RangeSlice, Read or Write
     */
    public JmxReporter.TimerMBean getProxyMetric(String scope) {
        try {
            return JMX.newMBeanProxy(mbeanServerConn,
                    new ObjectName(
                            "org.apache.cassandra.metrics:type=ClientRequest,scope=" + scope + ",name=Latency"),
                    JmxReporter.TimerMBean.class);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }