use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class AbstractConfig method fireConfigChange.
protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
for (final ConfigChangeListener listener : m_listeners) {
m_executorService.submit(new Runnable() {
@Override
public void run() {
String listenerName = listener.getClass().getName();
Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
try {
listener.onChange(changeEvent);
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
transaction.setStatus(ex);
Tracer.logError(ex);
logger.error("Failed to invoke config change listener {}", listenerName, ex);
} finally {
transaction.complete();
}
}
});
}
}
use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class AbstractConfigTest method testFireConfigChange_event_notify_once.
@Test
public void testFireConfigChange_event_notify_once() throws ExecutionException, InterruptedException, TimeoutException {
AbstractConfig abstractConfig = new ErrorConfig();
final String namespace = "app-namespace-1";
final String key = "great-key";
final SettableFuture<ConfigChangeEvent> future1 = SettableFuture.create();
final SettableFuture<ConfigChangeEvent> future2 = SettableFuture.create();
final AtomicInteger invokeCount = new AtomicInteger();
final ConfigChangeListener configChangeListener1 = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
invokeCount.incrementAndGet();
future1.set(changeEvent);
}
});
final ConfigChangeListener configChangeListener2 = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
invokeCount.incrementAndGet();
future2.set(changeEvent);
}
});
abstractConfig.addChangeListener(configChangeListener1, Collections.singleton(key));
abstractConfig.addChangeListener(configChangeListener2, Collections.singleton(key));
Map<String, ConfigChange> changes = new HashMap<>();
changes.put(key, new ConfigChange(namespace, key, "old-value", "new-value", PropertyChangeType.MODIFIED));
ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
abstractConfig.fireConfigChange(configChangeEvent);
assertEquals(configChangeEvent, future1.get(500, TimeUnit.MILLISECONDS));
assertEquals(configChangeEvent, future2.get(500, TimeUnit.MILLISECONDS));
assertEquals(2, invokeCount.get());
verify(configChangeListener1, times(1)).onChange(Matchers.eq(configChangeEvent));
verify(configChangeListener2, times(1)).onChange(Matchers.eq(configChangeEvent));
}
use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class AbstractConfigTest method testFireConfigChange_cannot_notify.
/**
* @see AbstractConfig#fireConfigChange(ConfigChangeEvent)
*/
@Test
public void testFireConfigChange_cannot_notify() throws InterruptedException {
AbstractConfig abstractConfig = spy(new ErrorConfig());
final String namespace = "app-namespace-0";
ConfigChangeListener configChangeListener = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
}
});
abstractConfig.addChangeListener(configChangeListener, Collections.singleton("cannot-be-match-key"));
Map<String, ConfigChange> changes = new HashMap<>();
changes.put("key1", new ConfigChange(namespace, "key1", null, "new-value", PropertyChangeType.ADDED));
ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
abstractConfig.fireConfigChange(configChangeEvent);
abstractConfig.fireConfigChange(namespace, changes);
// wait a minute for invoking
Thread.sleep(100);
verify(configChangeListener, times(0)).onChange(Matchers.<ConfigChangeEvent>any());
}
use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testUpdateProperties.
@Test
public void testUpdateProperties() throws Exception {
String someNewValue = "someNewValue";
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
future.set(changeEvent);
}
});
assertEquals("otherValue1", otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
embeddedApollo.addOrModifyProperty(anotherNamespace, "key1", someNewValue);
ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
assertEquals(someNewValue, otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
assertTrue(changeEvent.isChanged("key1"));
}
use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testDeleteSamePropertyTwice.
@Test
public void testDeleteSamePropertyTwice() throws Exception {
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final Semaphore changes = new Semaphore(0);
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
changes.release();
}
});
assertEquals("otherValue6", otherConfig.getProperty("key6", null));
embeddedApollo.deleteProperty(anotherNamespace, "key6");
embeddedApollo.deleteProperty(anotherNamespace, "key6");
assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
assertNull(otherConfig.getProperty("key6", null));
assertEquals(0, changes.availablePermits());
}
Aggregations