EtcdDynamicConfiguration implements DynamicConfiguration { @Override public void addListener(String key, String group, ConfigurationListener listener) { if (watchListenerMap.get(listener) == null) { EtcdConfigWatcher watcher = new EtcdConfigWatcher(key, group, listener); watchListenerMap.put(listener, watcher); watcher.watch(); } } EtcdDynamicConfiguration(URL url); @Override void addListener(String key, String group, ConfigurationListener listener); @Override void removeListener(String key, String group, ConfigurationListener listener); @Override String getConfig(String key, String group, long timeout); @Override Object getInternalProperty(String key);  }
@Test public void testAddListener() throws Exception { CountDownLatch latch = new CountDownLatch(4); TestListener listener1 = new TestListener(latch); TestListener listener2 = new TestListener(latch); TestListener listener3 = new TestListener(latch); TestListener listener4 = new TestListener(latch); config.addListener("AService.configurators", listener1); config.addListener("AService.configurators", listener2); config.addListener("testapp.tagrouters", listener3); config.addListener("testapp.tagrouters", listener4); put("/dubbo/config/AService/configurators", "new value1"); Thread.sleep(200); put("/dubbo/config/testapp/tagrouters", "new value2"); Thread.sleep(200); put("/dubbo/config/testapp", "new value3"); Thread.sleep(1000); Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); Assert.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators")); Assert.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators")); Assert.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters")); Assert.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters")); Assert.assertEquals("new value1", listener1.getValue()); Assert.assertEquals("new value1", listener2.getValue()); Assert.assertEquals("new value2", listener3.getValue()); Assert.assertEquals("new value2", listener4.getValue()); }