@Test public void unregisterWorkers() throws Exception { WorkerPool workerPool = getWorkerPool(); WorkerPool.Instances<ServiceStub> instances = workerPool.getInstances(ServiceStub.class); ServiceStub worker_1 = instances.get(0); ServiceStub worker_2 = instances.get(1); ServiceStub worker_3 = instances.get(2); context.checking(new Expectations() {{ oneOf(mBeanServer).unregisterMBean( with(objectname(domain + ":type=ServiceStub,annotation=BasicService,scope=Worker,workerId=worker-0"))); oneOf(mBeanServer).unregisterMBean( with(objectname(domain + ":type=ServiceStub,annotation=BasicService,scope=Worker,workerId=worker-1"))); oneOf(mBeanServer).unregisterMBean( with(objectname(domain + ":type=ServiceStub,annotation=BasicService,scope=Worker,workerId=worker-2"))); oneOf(mBeanServer).unregisterMBean( with(objectname(domain + ":type=ServiceStub,annotation=BasicService,scope=Worker"))); }}); BasicService basicServiceAnnotation = createBasicServiceAnnotation(); Key<?> key = Key.of(ServiceStub.class, basicServiceAnnotation); jmxRegistry.unregisterWorkers(workerPool, key, asList(worker_1, worker_2, worker_3)); }
public void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances) { if (poolInstances.size() == 0) { return; } if (poolInstances.stream().map(Object::getClass).collect(toSet()).size() != 1) { return; } if (!isJmxBean(poolInstances.get(0).getClass())) { return; } String commonName; try { commonName = createNameForKey(key, pool); } catch (ReflectiveOperationException e) { String msg = format("Error during generation name for pool of instances with key %s", key.toString()); logger.error(msg, e); return; } for (int i = 0; i < poolInstances.size(); i++) { try { String workerName = createWorkerName(commonName, i); ObjectName objectName = new ObjectName(workerName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister mbean for worker" + " of pool of instances with key %s. Worker id is \"%d\"", key.toString(), i); logger.error(msg, e); } } try { ObjectName objectName = new ObjectName(commonName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister aggregated mbean for pool of instances " + "with key %s.", key.toString()); logger.error(msg, e); } }
JmxRegistry implements JmxRegistryMXBean { public void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances) { if (poolInstances.size() == 0) { return; } if (poolInstances.stream().map(Object::getClass).collect(toSet()).size() != 1) { return; } if (!isJmxBean(poolInstances.get(0).getClass())) { return; } String commonName; try { commonName = createNameForKey(key, pool); } catch (ReflectiveOperationException e) { String msg = format("Error during generation name for pool of instances with key %s", key.toString()); logger.error(msg, e); return; } for (int i = 0; i < poolInstances.size(); i++) { try { String workerName = createWorkerName(commonName, i); ObjectName objectName = new ObjectName(workerName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister mbean for worker" + " of pool of instances with key %s. Worker id is \"%d\"", key.toString(), i); logger.error(msg, e); } } try { ObjectName objectName = new ObjectName(commonName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister aggregated mbean for pool of instances " + "with key %s.", key.toString()); logger.error(msg, e); } } }
JmxRegistry implements JmxRegistryMXBean { public void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances) { if (poolInstances.size() == 0) { return; } if (poolInstances.stream().map(Object::getClass).collect(toSet()).size() != 1) { return; } if (!isJmxBean(poolInstances.get(0).getClass())) { return; } String commonName; try { commonName = createNameForKey(key, pool); } catch (ReflectiveOperationException e) { String msg = format("Error during generation name for pool of instances with key %s", key.toString()); logger.error(msg, e); return; } for (int i = 0; i < poolInstances.size(); i++) { try { String workerName = createWorkerName(commonName, i); ObjectName objectName = new ObjectName(workerName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister mbean for worker" + " of pool of instances with key %s. Worker id is \"%d\"", key.toString(), i); logger.error(msg, e); } } try { ObjectName objectName = new ObjectName(commonName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister aggregated mbean for pool of instances " + "with key %s.", key.toString()); logger.error(msg, e); } } private  JmxRegistry(@NotNull MBeanServer mbs,
			DynamicMBeanFactory mbeanFactory,
			Map<Key<?>, String> keyToObjectNames,
			Map<Type, JmxCustomTypeAdapter<?>> customTypes); }
JmxRegistry implements JmxRegistryMXBean { public void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances) { if (poolInstances.size() == 0) { return; } if (poolInstances.stream().map(Object::getClass).collect(toSet()).size() != 1) { return; } if (!isJmxBean(poolInstances.get(0).getClass())) { return; } String commonName; try { commonName = createNameForKey(key, pool); } catch (ReflectiveOperationException e) { String msg = format("Error during generation name for pool of instances with key %s", key.toString()); logger.error(msg, e); return; } for (int i = 0; i < poolInstances.size(); i++) { try { String workerName = createWorkerName(commonName, i); ObjectName objectName = new ObjectName(workerName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister mbean for worker" + " of pool of instances with key %s. Worker id is \"%d\"", key.toString(), i); logger.error(msg, e); } } try { ObjectName objectName = new ObjectName(commonName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister aggregated mbean for pool of instances " + "with key %s.", key.toString()); logger.error(msg, e); } } private  JmxRegistry(@NotNull MBeanServer mbs,
			DynamicMBeanFactory mbeanFactory,
			Map<Key<?>, String> keyToObjectNames,
			Map<Type, JmxCustomTypeAdapter<?>> customTypes); static JmxRegistry create(MBeanServer mbs, DynamicMBeanFactory mbeanFactory); static JmxRegistry create(MBeanServer mbs,
			DynamicMBeanFactory mbeanFactory,
			Map<Key<?>, String> keyToObjectNames,
			Map<Type, JmxCustomTypeAdapter<?>> customTypes); JmxRegistry withScopes(boolean withScopes); void addWorkerPoolKey(WorkerPool workerPool, Key<?> workerPoolKey); void registerSingleton(@NotNull Key<?> key, @NotNull Object singletonInstance, @NotNull JmxBeanSettings settings); void unregisterSingleton(@NotNull Key<?> key, Object singletonInstance); void registerWorkers(@NotNull WorkerPool pool, Key<?> key, @NotNull List<?> poolInstances, JmxBeanSettings settings); void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances); void unregisterAll(); @Override int getRegisteredSingletons(); @Override int getRegisteredPools(); @Override int getTotallyRegisteredMBeans(); @Override String getRefreshPeriod(); @Override void setRefreshPeriod(String refreshPeriod); @Override int getMaxRefreshesPerOneCycle(); @Override void setMaxRefreshesPerOneCycle(int maxRefreshesPerOneCycle); @Override String[] getRefreshStats(); }
JmxRegistry implements JmxRegistryMXBean { public void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances) { if (poolInstances.size() == 0) { return; } if (poolInstances.stream().map(Object::getClass).collect(toSet()).size() != 1) { return; } if (!isJmxBean(poolInstances.get(0).getClass())) { return; } String commonName; try { commonName = createNameForKey(key, pool); } catch (ReflectiveOperationException e) { String msg = format("Error during generation name for pool of instances with key %s", key.toString()); logger.error(msg, e); return; } for (int i = 0; i < poolInstances.size(); i++) { try { String workerName = createWorkerName(commonName, i); ObjectName objectName = new ObjectName(workerName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister mbean for worker" + " of pool of instances with key %s. Worker id is \"%d\"", key.toString(), i); logger.error(msg, e); } } try { ObjectName objectName = new ObjectName(commonName); mbs.unregisterMBean(objectName); registeredObjectNames.remove(objectName); } catch (JMException e) { String msg = format("Error during attempt to unregister aggregated mbean for pool of instances " + "with key %s.", key.toString()); logger.error(msg, e); } } private  JmxRegistry(@NotNull MBeanServer mbs,
			DynamicMBeanFactory mbeanFactory,
			Map<Key<?>, String> keyToObjectNames,
			Map<Type, JmxCustomTypeAdapter<?>> customTypes); static JmxRegistry create(MBeanServer mbs, DynamicMBeanFactory mbeanFactory); static JmxRegistry create(MBeanServer mbs,
			DynamicMBeanFactory mbeanFactory,
			Map<Key<?>, String> keyToObjectNames,
			Map<Type, JmxCustomTypeAdapter<?>> customTypes); JmxRegistry withScopes(boolean withScopes); void addWorkerPoolKey(WorkerPool workerPool, Key<?> workerPoolKey); void registerSingleton(@NotNull Key<?> key, @NotNull Object singletonInstance, @NotNull JmxBeanSettings settings); void unregisterSingleton(@NotNull Key<?> key, Object singletonInstance); void registerWorkers(@NotNull WorkerPool pool, Key<?> key, @NotNull List<?> poolInstances, JmxBeanSettings settings); void unregisterWorkers(WorkerPool pool, @NotNull Key<?> key, List<?> poolInstances); void unregisterAll(); @Override int getRegisteredSingletons(); @Override int getRegisteredPools(); @Override int getTotallyRegisteredMBeans(); @Override String getRefreshPeriod(); @Override void setRefreshPeriod(String refreshPeriod); @Override int getMaxRefreshesPerOneCycle(); @Override void setMaxRefreshesPerOneCycle(int maxRefreshesPerOneCycle); @Override String[] getRefreshStats();  }