table.put("alias", ObjectName.quote(alias)); additionalNameKeys(table); ObjectName on = ObjectName.getInstance(getDomain(), table); getMbs().registerMBean(control, on); return getControlId(on); } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | MalformedObjectNameException e) {
@Override public <T> String getControlId(String type, String alias, Class<T> controlInterface) { return getControlId(getObjectNameForInterface(type, alias, controlInterface)); }
private <T> ObjectName getObjectNameForInterface(String type, String alias, Class<T> controlInterface) { try { Set<ObjectName> names = getObjectNamesForInterface(type, alias, controlInterface.getName()); if (names.isEmpty()) return null; if (names.size() != 1) throw new RuntimeException("Alias " + alias + " not unique for type " + type); ObjectName name = null; for (ObjectName on : names) { name = on; break; } return name; } catch (MalformedObjectNameException e) { throw new RuntimeException(e); } }
@Override public <T> T getControl(String type, String alias, Class<T> controlInterface) { MBeanServer mBeanServer = getMbs(); ObjectName name = getObjectNameForInterface(type, alias, controlInterface); return name != null ? JMX.newMXBeanProxy(mBeanServer, name, controlInterface) : null; }
private Set<ObjectName> getObjectNamesForInterface(String type, String alias, String interfaceName) throws MalformedObjectNameException { Hashtable<String,String> table = new Hashtable<>(); table.put("interface", ObjectName.quote(interfaceName)); table.put("type", ObjectName.quote(type)); if (alias != null) table.put("alias", ObjectName.quote(alias)); ObjectName objName = new ObjectName(getDomain(), table); // Add the wildcard for any other properties. objName = new ObjectName(objName.getCanonicalName()+",*"); MBeanServer mBeanServer = getMbs(); return mBeanServer.queryNames(objName, null); } }
public DevelopmentProvider() throws Exception { MetricsSetup.withRegistry(getServices(), new MetricRegistry()). startJMXReporter(JMX_DOMAIN); getServices().addService(ControlService.class, new JMXControlService(JMX_DOMAIN, new Hashtable<>())); StreamScopeSetup.register(getServices()); HttpServer server = HttpServer.getInstance(); getServices().addService(HttpServer.class, server); server.startServer(); }
static void startProvider(DirectProvider provider) throws InterruptedException, ExecutionException { provider.getServices().addService(ControlService.class, new JMXControlService("org.apache.edgent.test.apps.runtime", new Hashtable<>())); AppService.createAndRegister(provider, provider); JobRegistry.createAndRegister(provider.getServices()); }
@Test(expected=RuntimeException.class) public void testDoubleunregister() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); cs.unregister(controlId); cs.unregister(controlId); } }
@Test(expected=RuntimeException.class) public void testDoubleRegister() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); try { cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); } finally { cs.unregister(controlId); } } @Test(expected=RuntimeException.class)
@Test public void testControlObject() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); assertNotNull(controlId); ObjectName on = ObjectName.getInstance(controlId); assertEquals(DOMAIN, on.getDomain()); assertEquals(type, ObjectName.unquote(on.getKeyProperty("type"))); assertEquals(id, ObjectName.unquote(on.getKeyProperty("id"))); assertEquals(alias, ObjectName.unquote(on.getKeyProperty("alias"))); assertEquals(TimerMBean.class.getName(), ObjectName.unquote(on.getKeyProperty("interface"))); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); assertTrue(mbs.isRegistered(on)); cs.unregister(controlId); assertFalse(mbs.isRegistered(on)); }
addKeys.put("device", ObjectName.quote("pi")); ControlService cs = new JMXControlService(DOMAIN, addKeys);