List of usage examples for javax.management JMX newMBeanProxy
public static <T> T newMBeanProxy(MBeanServerConnection connection, ObjectName objectName, Class<T> interfaceClass)
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:
From source file:com.adaptris.core.runtime.FileLogHandlerJmxTest.java
public void testMBean_CleanupLogfiles() throws Exception { Adapter adapter = createAdapter(getName()); FileLogHandler handler = createHandler(); adapter.setLogHandler(handler);//from w w w . j av a2 s.co m AdapterManager adapterManager = new AdapterManager(adapter); ObjectName flhObjName = createFileHandlerObjectName(adapterManager); LogHandlerTest.createLogFiles(LOG_DIRECTORY, LOG_FILE, 10); assertEquals(10, LOG_DIRECTORY.listFiles().length); try { adapterManager.registerMBean(); FileLogHandlerJmxMBean fileLogHandlerProxy = JMX.newMBeanProxy(mBeanServer, flhObjName, FileLogHandlerJmxMBean.class); assertNotNull(fileLogHandlerProxy); fileLogHandlerProxy.cleanupLogfiles(); assertEquals(1, LOG_DIRECTORY.listFiles().length); } finally { adapterManager.unregisterMBean(); } }
From source file:com.adaptris.core.runtime.AdapterComponentCheckerTest.java
public void testCheckInitialised_RetriesConnection() throws Exception { String adapterName = this.getClass().getSimpleName() + "." + getName(); Adapter adapter = createAdapter(adapterName, 2, 2); List<BaseComponentMBean> mBeans = createJmxManagers(adapter); ObjectName objectName = createComponentCheckerObjectName(adapterName); register(mBeans);// w w w .j ava 2 s . com AdapterComponentCheckerMBean manager = JMX.newMBeanProxy(mBeanServer, objectName, AdapterComponentCheckerMBean.class); manager.checkInitialise( DefaultMarshaller.getDefaultMarshaller().marshal(new MockAllowsRetriesConnection())); manager.checkInitialise( DefaultMarshaller.getDefaultMarshaller().marshal(new MockAllowsRetriesConnection(1))); }
From source file:com.adaptris.core.runtime.DefaultFailedMessageRetrierJmxTest.java
public void testMBean_Retry_SerializableMessage() throws Exception { Adapter adapter = createAdapter(getName()); Channel channel = createChannel(getName()); StandardWorkflow wf = createWorkflow(getName()); MockMessageProducer mock = new MockMessageProducer(); wf.setProducer(mock);/*ww w. jav a2s . c om*/ channel.getWorkflowList().add(wf); adapter.getChannelList().add(channel); DefaultFailedMessageRetrier retrier = new DefaultFailedMessageRetrier(); adapter.setFailedMessageRetrier(retrier); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); msg.addMetadata(Workflow.WORKFLOW_ID_KEY, getName() + "@" + getName()); SerializableAdaptrisMessage serialized = new DefaultSerializableMessageTranslator().translate(msg); AdapterManager adapterManager = new AdapterManager(adapter); try { adapterManager.registerMBean(); adapterManager.requestStart(); ObjectName retrierObjName = createRetrierObjectName(adapterManager); DefaultFailedMessageRetrierJmxMBean jmxBean = JMX.newMBeanProxy(mBeanServer, retrierObjName, DefaultFailedMessageRetrierJmxMBean.class); assertTrue(jmxBean.retryMessage(serialized)); waitForMessages(mock, 1); assertEquals(1, mock.messageCount()); } finally { adapterManager.requestClose(); adapterManager.unregisterMBean(); } }
From source file:com.adaptris.core.runtime.AdapterComponentCheckerTest.java
public void testApplyService() throws Exception { String adapterName = this.getClass().getSimpleName() + "." + getName(); Adapter adapter = createAdapter(adapterName, 2, 2); List<BaseComponentMBean> mBeans = createJmxManagers(adapter); ObjectName objectName = createComponentCheckerObjectName(adapterName); register(mBeans);/* ww w . j a va2 s .c om*/ AdapterComponentCheckerMBean manager = JMX.newMBeanProxy(mBeanServer, objectName, AdapterComponentCheckerMBean.class); AdaptrisMessage msg = new DefaultSerializableMessageTranslator() .translate(manager.applyService(createServiceForTests(), createSerializableMessage())); assertTrue(msg.containsKey("key")); assertEquals("value", msg.getMetadataValue("key")); }
From source file:com.adaptris.core.runtime.StandardMessageErrorDigestTest.java
public void testMBean_JMXRegistration() throws Exception { String adapterName = this.getClass().getSimpleName() + "." + getName(); Adapter adapter = createAdapter(adapterName); adapter.setMessageErrorDigester(new StandardMessageErrorDigester(getName())); List<BaseComponentMBean> mBeans = createJmxManagers(adapter); try {//from www . j a v a 2 s .c o m register(mBeans); ObjectName adapterObj = createAdapterObjectName(adapterName); ObjectName digesterObj = createMessageErrorDigestObjectName(adapterName, getName()); StandardMessageErrorDigesterJmxMBean errDigester = JMX.newMBeanProxy(mBeanServer, digesterObj, StandardMessageErrorDigesterJmxMBean.class); AdapterManagerMBean adapterManager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class); assertNotNull(errDigester.getParentObjectName()); assertEquals(adapterObj, errDigester.getParentObjectName()); } finally { } }
From source file:com.spotify.reaper.cassandra.JmxProxy.java
/** * Connect to JMX interface on the given host and port. * * @param handler Implementation of {@link RepairStatusHandler} to process incoming * notifications/*from w w w . ja va 2 s .c o m*/ * of repair events. * @param host hostname or ip address of Cassandra node * @param port port number to use for JMX connection * @param username username to use for JMX authentication * @param password password to use for JMX authentication */ static JmxProxy connect(Optional<RepairStatusHandler> handler, String host, int port, String username, String password) throws ReaperException { ObjectName ssMbeanName; ObjectName cmMbeanName; JMXServiceURL jmxUrl; try { jmxUrl = new JMXServiceURL(String.format(JMX_URL, host, port)); ssMbeanName = new ObjectName(SS_OBJECT_NAME); cmMbeanName = new ObjectName(CompactionManager.MBEAN_OBJECT_NAME); } catch (MalformedURLException | MalformedObjectNameException e) { LOG.error(String.format("Failed to prepare the JMX connection to %s:%s", host, port)); throw new ReaperException("Failure during preparations for JMX connection", e); } try { Map<String, Object> env = new HashMap<String, Object>(); if (username != null && password != null) { String[] creds = { username, password }; env.put(JMXConnector.CREDENTIALS, creds); } JMXConnector jmxConn = JMXConnectorFactory.connect(jmxUrl, env); MBeanServerConnection mbeanServerConn = jmxConn.getMBeanServerConnection(); Object ssProxy = JMX.newMBeanProxy(mbeanServerConn, ssMbeanName, StorageServiceMBean.class); String cassandraVersion = ((StorageServiceMBean) ssProxy).getReleaseVersion(); if (cassandraVersion.startsWith("2.0") || cassandraVersion.startsWith("1.")) { ssProxy = JMX.newMBeanProxy(mbeanServerConn, ssMbeanName, StorageServiceMBean20.class); } CompactionManagerMBean cmProxy = JMX.newMBeanProxy(mbeanServerConn, cmMbeanName, CompactionManagerMBean.class); JmxProxy proxy = new JmxProxy(handler, host, jmxUrl, jmxConn, ssProxy, ssMbeanName, mbeanServerConn, cmProxy); // registering a listener throws bunch of exceptions, so we do it here rather than in the // constructor mbeanServerConn.addNotificationListener(ssMbeanName, proxy, null, null); LOG.debug("JMX connection to {} properly connected: {}", host, jmxUrl.toString()); return proxy; } catch (IOException | InstanceNotFoundException e) { LOG.error("Failed to establish JMX connection to {}:{}", host, port); throw new ReaperException("Failure when establishing JMX connection", e); } }
From source file:com.adaptris.core.runtime.AdapterComponentCheckerTest.java
public void testApplyService_NotService() throws Exception { String adapterName = this.getClass().getSimpleName() + "." + getName(); Adapter adapter = createAdapter(adapterName, 2, 2); List<BaseComponentMBean> mBeans = createJmxManagers(adapter); ObjectName objectName = createComponentCheckerObjectName(adapterName); register(mBeans);/* w ww . j a v a2s.c o m*/ AdapterComponentCheckerMBean manager = JMX.newMBeanProxy(mBeanServer, objectName, AdapterComponentCheckerMBean.class); SerializableMessage msg = createSerializableMessage(); try { manager.applyService("<Document/>", msg, false); fail(); } catch (CoreException expected) { } }
From source file:com.adaptris.core.runtime.DefaultFailedMessageRetrierJmxTest.java
public void testMBean_Retry_SerializableMessage_WorkflowNotFound() throws Exception { Adapter adapter = createAdapter(getName()); Channel c = createChannel(getName()); StandardWorkflow wf = createWorkflow(getName()); MockMessageProducer mock = new MockMessageProducer(); wf.setProducer(mock);/*from w ww . j a v a2 s.c o m*/ c.getWorkflowList().add(wf); adapter.getChannelList().add(c); DefaultFailedMessageRetrier retrier = new DefaultFailedMessageRetrier(); adapter.setFailedMessageRetrier(retrier); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); msg.addMetadata(Workflow.WORKFLOW_ID_KEY, getName() + "@BLAHBLAH"); SerializableAdaptrisMessage serialized = new DefaultSerializableMessageTranslator().translate(msg); AdapterManager adapterManager = new AdapterManager(adapter); try { adapterManager.registerMBean(); adapterManager.requestStart(); ObjectName retrierObjName = createRetrierObjectName(adapterManager); DefaultFailedMessageRetrierJmxMBean jmxBean = JMX.newMBeanProxy(mBeanServer, retrierObjName, DefaultFailedMessageRetrierJmxMBean.class); assertFalse(jmxBean.retryMessage(serialized)); assertEquals(0, mock.messageCount()); } finally { adapterManager.requestClose(); adapterManager.unregisterMBean(); } }
From source file:com.adaptris.core.runtime.AdapterRegistry.java
@Override public void persistAdapter(ObjectName adapter, URLString url) throws CoreException, IOException { persistAdapter(JMX.newMBeanProxy(mBeanServer, adapter, AdapterManagerMBean.class), url); }
From source file:io.mapzone.arena.ArenaPlugin.java
protected void testMBeanConnection() throws Exception { // test connection String port = System.getProperty("com.sun.management.jmxremote.port"); if (port != null) { String url = "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/jmxrmi"; JMXServiceURL serviceUrl = new JMXServiceURL(url); try (JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);) { MBeanServerConnection conn = jmxConnector.getMBeanServerConnection(); Set<ObjectName> beanSet = conn.queryNames(null, null); beanSet.forEach(n -> log.debug(" MBean: " + n)); beanSet = conn.queryNames(ArenaConfigMBean.NAME.get(), null); beanSet.forEach(n -> log.debug(" MBean: " + n)); ArenaConfigMBean arenaConfig = JMX.newMBeanProxy(conn, ArenaConfigMBean.NAME.get(), ArenaConfigMBean.class); arenaConfig.setAppTitle("Arena"); }//ww w .j ava2 s . co m } else { log.info("No jmxremote.port specified."); } }