RpcConfigs { public static synchronized void subscribe(String key, RpcConfigListener listener) { List<RpcConfigListener> listeners = CFG_LISTENER.get(key); if (listeners == null) { listeners = new ArrayList<RpcConfigListener>(); CFG_LISTENER.put(key, listeners); } listeners.add(listener); }  static void putValue(String key, Object newValue); static boolean getBooleanValue(String primaryKey); static boolean getBooleanValue(String primaryKey, String secondaryKey); static int getIntValue(String primaryKey); static int getIntValue(String primaryKey, String secondaryKey); static T getEnumValue(String primaryKey, Class<T> enumClazz); static String getStringValue(String primaryKey); static String getStringValue(String primaryKey, String secondaryKey); static List getListValue(String primaryKey); static T getOrDefaultValue(String primaryKey, T defaultValue); static synchronized void subscribe(String key, RpcConfigListener listener); static synchronized void unSubscribe(String key, RpcConfigListener listener);  }
@Test public void subscribe() { RpcConfigs.putValue("ps001", ProviderStatus.RECOVERING.toString()); Assert.assertEquals(ProviderStatus.RECOVERING, RpcConfigs.getEnumValue("ps001", ProviderStatus.class)); try { Assert.assertEquals(ProviderStatus.RECOVERING, RpcConfigs.getEnumValue("ps002", ProviderStatus.class)); Assert.fail(); } catch (Exception e) { Assert.assertTrue(e instanceof SofaRpcRuntimeException); } finally { RpcConfigs.putValue("ps001", ""); } String testSubKey = "testSubKey"; RpcConfigs.putValue(testSubKey, "111"); String protocol = RpcConfigs.getStringValue(testSubKey); final Object[] values = new Object[2]; RpcConfigs.RpcConfigListener listener = new RpcConfigs.RpcConfigListener() { @Override public void onChange(Object oldValue, Object newValue) { values[0] = oldValue; values[1] = newValue; } }; try { RpcConfigs.subscribe(testSubKey, listener); RpcConfigs.putValue(testSubKey, "xxx"); Assert.assertEquals(protocol, values[0]); Assert.assertEquals("xxx", values[1]); RpcConfigs.removeValue(testSubKey); } finally { RpcConfigs.removeValue(testSubKey); RpcConfigs.unSubscribe(testSubKey, listener); } }