NacosDynamicConfiguration implements DynamicConfiguration { @Override public void addListener(String key, String group, ConfigurationListener listener) { String resolvedGroup = resolveGroup(group); String listenerKey = buildListenerKey(key, group); NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(listenerKey, k -> createTargetListener(key, resolvedGroup)); nacosConfigListener.addListener(listener); try { configService.addListener(key, resolvedGroup, nacosConfigListener); } catch (NacosException e) { logger.error(e.getMessage()); } } NacosDynamicConfiguration(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); @Override boolean publishConfig(String key, String group, String content); @Override long getDefaultTimeout(); @Override SortedSet<String> getConfigKeys(String group); @Override boolean removeConfig(String key, String group);  }
@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.tag-router", listener3); config.addListener("testapp.tag-router", listener4); put("AService.configurators", "new value1"); Thread.sleep(200); put("testapp.tag-router", "new value2"); Thread.sleep(200); put("testapp", "new value3"); Thread.sleep(5000); latch.await(); Assertions.assertEquals(1, listener1.getCount("AService.configurators")); Assertions.assertEquals(1, listener2.getCount("AService.configurators")); Assertions.assertEquals(1, listener3.getCount("testapp.tag-router")); Assertions.assertEquals(1, listener4.getCount("testapp.tag-router")); Assertions.assertEquals("new value1", listener1.getValue()); Assertions.assertEquals("new value1", listener2.getValue()); Assertions.assertEquals("new value2", listener3.getValue()); Assertions.assertEquals("new value2", listener4.getValue()); }