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_PersistAdapter_Bean_to_URL() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        File filename = deleteLater(adapter);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        assertNotNull(objName);/*from w  ww .j a v a 2  s  . c  om*/
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        registry.persistAdapter(manager, new URLString(filename));
        Adapter marshalledAdapter = (Adapter) DefaultMarshaller.getDefaultMarshaller().unmarshal(filename);
        assertRoundtripEquality(adapter, marshalledAdapter);
    }
    

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

    public void testMBean_RemoveDigestEntry_ByMessageId_WithDelete() throws Exception {
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName);
        StandardMessageErrorDigester digester = createDigester();
        digester.setUniqueId(getName());/* ww w  .  java  2s. com*/
        adapter.setMessageErrorDigester(digester);
        List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
        File tempDir = Files.createTempDirectory(this.getClass().getSimpleName()).toFile();
        tempDir.deleteOnExit();
        cleaner.track(tempDir, digester, FileDeleteStrategy.FORCE);
        try {
            start(adapter);
            register(mBeans);
            ObjectName adapterObj = createAdapterObjectName(adapterName);
            ObjectName digesterObj = createMessageErrorDigestObjectName(adapterName, getName());
    
            StandardMessageErrorDigesterJmxMBean errDigester = JMX.newMBeanProxy(mBeanServer, digesterObj,
                    StandardMessageErrorDigesterJmxMBean.class);
            // The files don't actually exist; so they won't be deleted;
            List<AdaptrisMessage> msgs = createAndDigest(digester, "testDigest", tempDir.getCanonicalPath());
    
            assertNotNull(errDigester.getDigest());
            assertEquals(5, errDigester.getDigest().size());
            assertEquals(5, errDigester.getTotalErrorCount());
            errDigester.remove(msgs.get(0).getUniqueId(), true);
            assertEquals(4, errDigester.getDigest().size());
            assertEquals(5, errDigester.getTotalErrorCount());
        } finally {
            stop(adapter);
        }
    }
    

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

    public void testProxy_PersistAdapter_Bean_to_URL_String() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        File filename = deleteLater(adapter);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        assertNotNull(objName);/*from  www  . j av a2  s.  c  o  m*/
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        registry.persistAdapter(manager, filename.toURI().toString());
        Adapter marshalledAdapter = (Adapter) DefaultMarshaller.getDefaultMarshaller().unmarshal(filename);
        assertRoundtripEquality(adapter, marshalledAdapter);
    }
    

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

    public void testProxy_PersistAdapter_ObjectName_To_URL() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        File filename = deleteLater(adapter);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        registry.persistAdapter(objName, new URLString(filename));
        Adapter marshalledAdapter = (Adapter) DefaultMarshaller.getDefaultMarshaller().unmarshal(filename);
        assertRoundtripEquality(adapter, marshalledAdapter);
    }
    

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

    public void testProxy_PersistAdapter_ObjectName_To_URL_String() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        File filename = deleteLater(adapter);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        registry.persistAdapter(objName, filename.toURI().toString());
        Adapter marshalledAdapter = (Adapter) DefaultMarshaller.getDefaultMarshaller().unmarshal(filename);
        assertRoundtripEquality(adapter, marshalledAdapter);
    }
    

    From source file:com.adaptris.core.fs.FsConsumerCase.java

    public void testFsMonitor() throws Exception {
        String subdir = new GuidGenerator().safeUUID();
    
        FsConsumerImpl fs = createConsumer(subdir);
        fs.setUniqueId(getName());/*  ww w. j  a va  2 s  .co  m*/
        Adapter adapter = new Adapter();
        adapter.setUniqueId(getName());
        Channel channel = new Channel();
        channel.setUniqueId(getName());
        StandardWorkflow wf = new StandardWorkflow();
        wf.setUniqueId(getName());
        wf.setConsumer(fs);
        channel.getWorkflowList().add(wf);
        adapter.getChannelList().add(channel);
    
        AdapterManager am = new AdapterManager(adapter);
        try {
            am.registerMBean();
            am.requestInit();
            String objectNameString = String.format(
                    "com.adaptris:type=ConsumerMonitor,adapter=%s,channel=%s,workflow=%s,id=%s", getName(),
                    getName(), getName(), getName());
            MBeanServer mBeanServer = JmxHelper.findMBeanServer();
            FsConsumerMonitorMBean mbean = JMX.newMBeanProxy(mBeanServer, ObjectName.getInstance(objectNameString),
                    FsConsumerMonitorMBean.class);
            assertEquals(0, mbean.messagesRemaining());
        } finally {
            am.requestClose();
            am.unregisterMBean();
        }
    }
    

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

    public void testDestroyAdapter_MBean() throws Exception {
        AdapterRegistryMBean myAdapterRegistry = AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = myAdapterRegistry.createAdapter(xml);
        assertNotNull(myAdapterRegistry.getBuilder(objName));
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        manager.requestStart();/*from   w w  w  .j  a va  2  s.  c  o  m*/
        myAdapterRegistry.destroyAdapter(manager);
        try {
            myAdapterRegistry.getBuilder(objName);
            fail();
        } catch (InstanceNotFoundException expected) {
    
        }
        assertFalse(mBeanServer.isRegistered(objName));
        assertEquals(0, myAdapterRegistry.getAdapters().size());
    }
    

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

    public void testProxy_DestroyAdapter_MBean() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        assertNotNull(registry.getBuilder(objName));
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        manager.requestStart();/*from w ww .ja  v a 2 s.  c  o  m*/
        registry.destroyAdapter(manager);
        try {
            registry.getBuilder(objName);
            fail();
        } catch (InstanceNotFoundException expected) {
    
        }
        assertFalse(mBeanServer.isRegistered(objName));
        assertEquals(0, registry.getAdapters().size());
    }
    

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

    public void testProxy_DestroyAdapter_MBean_SharedConnection_JNDI() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        AdapterRegistryMBean registry = JMX.newMBeanProxy(mBeanServer, myAdapterRegistry.createObjectName(),
                AdapterRegistryMBean.class);
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        adapter.getSharedComponents().addConnection(new NullConnection(getName()));
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = registry.createAdapter(xml);
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        manager.requestStart();/*  w w  w.j  a  v  a2 s  .  c o m*/
        InitialContext context = new InitialContext(contextEnv);
        context.lookup("adapter:comp/env/" + getName());
        myAdapterRegistry.destroyAdapter(manager);
        try {
            context.lookup("adapter:comp/env/" + getName());
        } catch (NamingException expected) {
    
        }
        assertFalse(mBeanServer.isRegistered(objName));
        assertEquals(0, registry.getAdapters().size());
    }
    

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

    public void testDestroyAdapter_ObjectName() throws Exception {
        AdapterRegistry myAdapterRegistry = (AdapterRegistry) AdapterRegistry
                .findInstance(new JunitBootstrapProperties(new Properties()));
        String adapterName = this.getClass().getSimpleName() + "." + getName();
        Adapter adapter = createAdapter(adapterName, 2, 2);
        String xml = DefaultMarshaller.getDefaultMarshaller().marshal(adapter);
        ObjectName objName = myAdapterRegistry.createAdapter(xml);
        assertTrue(mBeanServer.isRegistered(objName));
        assertEquals(1, myAdapterRegistry.getAdapters().size());
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, objName, AdapterManagerMBean.class);
        manager.requestStart();//  w w w  .jav a2  s. co  m
        myAdapterRegistry.destroyAdapter(objName);
        assertFalse(mBeanServer.isRegistered(objName));
        assertEquals(0, myAdapterRegistry.getAdapters().size());
    }