Java Code Examples for com.ctrip.framework.apollo.Config#addChangeListener()

The following examples show how to use com.ctrip.framework.apollo.Config#addChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApolloMockServerApiTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateSamePropertyTwice() throws Exception {
  String someNewValue = "someNewValue";

  Config otherConfig = ConfigService.getConfig(anotherNamespace);

  final Semaphore changes = new Semaphore(0);

  otherConfig.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      changes.release();
    }
  });

  assertEquals("otherValue3", otherConfig.getProperty("key3", null));

  embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue);
  embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue);

  assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
  assertEquals(someNewValue, otherConfig.getProperty("key3", null));
  assertEquals(0, changes.availablePermits());
}
 
Example 2
Source File: ApolloMockServerApiTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 3
Source File: ApolloConfigManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
public void init(JbootConfigManager configManager) {

        ApolloServerConfig apolloServerConfig = configManager.get(ApolloServerConfig.class);
        if (!apolloServerConfig.isEnable() || !apolloServerConfig.isConfigOk()){
            return;
        }

        Config config = getDefaultConfig();

        Set<String> propNames = config.getPropertyNames();
        if (propNames != null && !propNames.isEmpty()) {
            for (String name : propNames) {
                String value = config.getProperty(name, null);
                configManager.setRemoteProperty(name, value);
            }
        }

        config.addChangeListener(changeEvent -> {
            for (String key : changeEvent.changedKeys()) {
                ConfigChange change = changeEvent.getChange(key);
                configManager.setRemoteProperty(change.getPropertyName(), change.getNewValue());
            }

            configManager.notifyChangeListeners(changeEvent.changedKeys());
        });

    }
 
Example 4
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testLongPollRefresh() throws Exception {
  final String someKey = "someKey";
  final String someValue = "someValue";
  final String anotherValue = "anotherValue";
  long someNotificationId = 1;

  long pollTimeoutInMS = 50;
  Map<String, String> configurations = Maps.newHashMap();
  configurations.put(someKey, someValue);
  ApolloConfig apolloConfig = assembleApolloConfig(configurations);
  ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
  ContextHandler pollHandler =
      mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK,
          Lists.newArrayList(
              new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)),
          false);

  startServerWithHandlers(configHandler, pollHandler);

  Config config = ConfigService.getAppConfig();
  assertEquals(someValue, config.getProperty(someKey, null));

  final SettableFuture<Boolean> longPollFinished = SettableFuture.create();

  config.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      longPollFinished.set(true);
    }
  });

  apolloConfig.getConfigurations().put(someKey, anotherValue);

  longPollFinished.get(pollTimeoutInMS * 20, TimeUnit.MILLISECONDS);

  assertEquals(anotherValue, config.getProperty(someKey, null));
}
 
Example 5
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
public void testRefreshConfig() throws Exception {
  final String someKey = "someKey";
  final String someValue = "someValue";
  final String anotherValue = "anotherValue";

  int someRefreshInterval = 500;
  TimeUnit someRefreshTimeUnit = TimeUnit.MILLISECONDS;

  setRefreshInterval(someRefreshInterval);
  setRefreshTimeUnit(someRefreshTimeUnit);

  Map<String, String> configurations = Maps.newHashMap();
  configurations.put(someKey, someValue);
  ApolloConfig apolloConfig = assembleApolloConfig(configurations);
  ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
  startServerWithHandlers(handler);

  Config config = ConfigService.getAppConfig();
  final List<ConfigChangeEvent> changeEvents = Lists.newArrayList();

  final SettableFuture<Boolean> refreshFinished = SettableFuture.create();
  config.addChangeListener(new ConfigChangeListener() {
    AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      //only need to assert once
      if (counter.incrementAndGet() > 1) {
        return;
      }
      assertEquals(1, changeEvent.changedKeys().size());
      assertTrue(changeEvent.isChanged(someKey));
      assertEquals(someValue, changeEvent.getChange(someKey).getOldValue());
      assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue());
      // if there is any assertion failed above, this line won't be executed
      changeEvents.add(changeEvent);
      refreshFinished.set(true);
    }
  });

  apolloConfig.getConfigurations().put(someKey, anotherValue);

  refreshFinished.get(someRefreshInterval * 5, someRefreshTimeUnit);

  assertThat(
      "Change event's size should equal to one or there must be some assertion failed in change listener",
      1, equalTo(changeEvents.size()));
  assertEquals(anotherValue, config.getProperty(someKey, null));
}
 
Example 6
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
public void testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified()
    throws Exception {
  final String someKey = "someKey";
  final String someValue = "someValue";
  final String anotherValue = "anotherValue";
  long someNotificationId = 1;

  long pollTimeoutInMS = 50;
  Map<String, String> configurations = Maps.newHashMap();
  configurations.put(someKey, someValue);
  ApolloConfig apolloConfig = assembleApolloConfig(configurations);
  ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
  ContextHandler pollHandler =
      mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK,
          Lists.newArrayList(
              new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)),
          false);

  startServerWithHandlers(configHandler, pollHandler);

  Config someOtherConfig = ConfigService.getConfig(someOtherNamespace);
  Config config = ConfigService.getAppConfig();
  assertEquals(someValue, config.getProperty(someKey, null));
  assertEquals(someValue, someOtherConfig.getProperty(someKey, null));

  final SettableFuture<Boolean> longPollFinished = SettableFuture.create();

  config.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      longPollFinished.set(true);
    }
  });

  apolloConfig.getConfigurations().put(someKey, anotherValue);

  longPollFinished.get(5000, TimeUnit.MILLISECONDS);

  assertEquals(anotherValue, config.getProperty(someKey, null));

  TimeUnit.MILLISECONDS.sleep(pollTimeoutInMS * 10);
  assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
}