com.ctrip.framework.apollo.ConfigChangeListener Java Examples

The following examples show how to use com.ctrip.framework.apollo.ConfigChangeListener. 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: ApolloCenterRepository.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
    String apolloKey = ConfigKeyUtils.pathToKey(key);
    caches.put(apolloKey, dataChangedEventListener);
    ConfigChangeListener listener = changeEvent -> {
        for (String changeKey : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(changeKey);
            DataChangedEvent.ChangedType changedType = getChangedType(change.getChangeType());
            if (DataChangedEvent.ChangedType.IGNORED == changedType) {
                continue;
            }
            if (caches.get(changeKey) == null) {
                continue;
            }
            caches.get(changeKey).onChange(new DataChangedEvent(ConfigKeyUtils.keyToPath(changeKey), change.getNewValue(), changedType));
        }
    };
    configWrapper.addChangeListener(listener, Collections.singleton(apolloKey), Collections.singleton(apolloKey));
}
 
Example #2
Source File: SimpleApolloConfigDemo.java    From apollo with Apache License 2.0 6 votes vote down vote up
public SimpleApolloConfigDemo() {
  ConfigChangeListener changeListener = new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      logger.info("Changes for namespace {}", changeEvent.getNamespace());
      for (String key : changeEvent.changedKeys()) {
        ConfigChange change = changeEvent.getChange(key);
        logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
            change.getPropertyName(), change.getOldValue(), change.getNewValue(),
            change.getChangeType());
      }
    }
  };
  config = ConfigService.getAppConfig();
  config.addChangeListener(changeListener);
}
 
Example #3
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 #4
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 #5
Source File: ConfigPropertySourceTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddChangeListener() throws Exception {
  ConfigChangeListener someListener = mock(ConfigChangeListener.class);
  ConfigChangeListener anotherListener = mock(ConfigChangeListener.class);

  final List<ConfigChangeListener> listeners = Lists.newArrayList();

  doAnswer(new Answer() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      listeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class));

      return Void.class;
    }
  }).when(someConfig).addChangeListener(any(ConfigChangeListener.class));

  configPropertySource.addChangeListener(someListener);
  configPropertySource.addChangeListener(anotherListener);

  assertEquals(2, listeners.size());
  assertTrue(listeners.containsAll(Lists.newArrayList(someListener, anotherListener)));
}
 
Example #6
Source File: AbstractConfig.java    From apollo with Apache License 2.0 6 votes vote down vote up
protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
  for (final ConfigChangeListener listener : m_listeners) {
    // check whether the listener is interested in this change event
    if (!isConfigChangeListenerInterested(listener, changeEvent)) {
      continue;
    }
    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();
        }
      }
    });
  }
}
 
Example #7
Source File: JavaConfigAnnotationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testApolloConfigChangeListenerWithInterestedKeys() throws Exception {
  Config applicationConfig = mock(Config.class);
  Config fxApolloConfig = mock(Config.class);

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
  mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);

  TestApolloConfigChangeListenerWithInterestedKeysBean bean = getBean(
      TestApolloConfigChangeListenerWithInterestedKeysBean.class, AppConfig8.class);

  final ArgumentCaptor<Set> applicationConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);
  final ArgumentCaptor<Set> fxApolloConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);

  verify(applicationConfig, times(2))
      .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), anySetOf(String.class));

  verify(fxApolloConfig, times(1))
      .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), anySetOf(String.class));

  assertEquals(2, applicationConfigInterestedKeys.getAllValues().size());

  Set<String> result = Sets.newHashSet();
  for (Set interestedKeys : applicationConfigInterestedKeys.getAllValues()) {
    result.addAll(interestedKeys);
  }
  assertEquals(Sets.newHashSet("someKey", "anotherKey"), result);

  assertEquals(1, fxApolloConfigInterestedKeys.getAllValues().size());

  assertEquals(asList(Sets.newHashSet("anotherKey")), fxApolloConfigInterestedKeys.getAllValues());
}
 
Example #8
Source File: ApolloConfigWrapperTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertAddChangeListenerWithInterestedKeyPrefixes() throws Exception {
    final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
    ConfigChangeListener listener = future::set;
    configWrapper.addChangeListener(listener, Collections.singleton("test.children.1"), Collections.singleton("test.children.2"));
    embeddedApollo.addOrModifyProperty("orchestration", "test.children.2.1", "value4");
    ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
    assertTrue(changeEvent.isChanged("test.children.2.1"));
    assertThat(changeEvent.getChange("test.children.2.1").getNewValue(), is("value4"));
    assertThat(changeEvent.getChange("test.children.2.1").getChangeType(), is(PropertyChangeType.ADDED));
}
 
Example #9
Source File: ApolloConfigWrapperTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertAddChangeListener() throws Exception {
    final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
    ConfigChangeListener listener = future::set;
    configWrapper.addChangeListener(listener, Collections.singleton("test.children.2"));
    embeddedApollo.addOrModifyProperty("orchestration", "test.children.2", "value3");
    ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
    assertTrue(changeEvent.isChanged("test.children.2"));
    assertThat(changeEvent.getChange("test.children.2").getOldValue(), is("value2"));
    assertThat(changeEvent.getChange("test.children.2").getNewValue(), is("value3"));
    assertThat(changeEvent.getChange("test.children.2").getChangeType(), is(PropertyChangeType.MODIFIED));
}
 
Example #10
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 #11
Source File: XMLConfigAnnotationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testApolloConfigChangeListenerWithInterestedKeys() throws Exception {
  Config applicationConfig = mock(Config.class);
  Config fxApolloConfig = mock(Config.class);

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
  mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);

  TestApolloConfigChangeListenerWithInterestedKeysBean bean = getBean(
      "spring/XmlConfigAnnotationTest6.xml", TestApolloConfigChangeListenerWithInterestedKeysBean.class);

  final ArgumentCaptor<Set> applicationConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);
  final ArgumentCaptor<Set> fxApolloConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);

  verify(applicationConfig, times(2))
      .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), anySetOf(String.class));

  verify(fxApolloConfig, times(1))
      .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), anySetOf(String.class));

  assertEquals(2, applicationConfigInterestedKeys.getAllValues().size());

  Set<String> result = Sets.newHashSet();
  for (Set interestedKeys : applicationConfigInterestedKeys.getAllValues()) {
    result.addAll(interestedKeys);
  }
  assertEquals(Sets.newHashSet("someKey", "anotherKey"), result);

  assertEquals(1, fxApolloConfigInterestedKeys.getAllValues().size());

  assertEquals(asList(Sets.newHashSet("anotherKey")), fxApolloConfigInterestedKeys.getAllValues());
}
 
Example #12
Source File: AbstractConfig.java    From apollo with Apache License 2.0 5 votes vote down vote up
private boolean isConfigChangeListenerInterested(ConfigChangeListener configChangeListener, ConfigChangeEvent configChangeEvent) {
  Set<String> interestedKeys = m_interestedKeys.get(configChangeListener);
  Set<String> interestedKeyPrefixes = m_interestedKeyPrefixes.get(configChangeListener);

  if ((interestedKeys == null || interestedKeys.isEmpty())
      && (interestedKeyPrefixes == null || interestedKeyPrefixes.isEmpty())) {
    return true; // no interested keys means interested in all keys
  }

  if (interestedKeys != null) {
    for (String interestedKey : interestedKeys) {
      if (configChangeEvent.isChanged(interestedKey)) {
        return true;
      }
    }
  }

  if (interestedKeyPrefixes != null) {
    for (String prefix : interestedKeyPrefixes) {
      for (final String changedKey : configChangeEvent.changedKeys()) {
        if (changedKey.startsWith(prefix)) {
          return true;
        }
      }
    }
  }

  return false;
}
 
Example #13
Source File: AbstractConfig.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Override
public void addChangeListener(ConfigChangeListener listener, Set<String> interestedKeys, Set<String> interestedKeyPrefixes) {
  if (!m_listeners.contains(listener)) {
    m_listeners.add(listener);
    if (interestedKeys != null && !interestedKeys.isEmpty()) {
      m_interestedKeys.put(listener, Sets.newHashSet(interestedKeys));
    }
    if (interestedKeyPrefixes != null && !interestedKeyPrefixes.isEmpty()) {
      m_interestedKeyPrefixes.put(listener, Sets.newHashSet(interestedKeyPrefixes));
    }
  }
}
 
Example #14
Source File: ApolloDataSource.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initializeConfigChangeListener() {
    configChangeListener = new ConfigChangeListener() {
        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            ConfigChange change = changeEvent.getChange(ruleKey);
            //change is never null because the listener will only notify for this key
            if (change != null) {
                RecordLog.info("[ApolloDataSource] Received config changes: " + change.toString());
            }
            loadAndUpdateRules();
        }
    };
    config.addChangeListener(configChangeListener, Sets.newHashSet(ruleKey));
}
 
Example #15
Source File: ApolloDataSource.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initializeConfigChangeListener() {
    configChangeListener = new ConfigChangeListener() {
        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            ConfigChange change = changeEvent.getChange(ruleKey);
            //change is never null because the listener will only notify for this key
            if (change != null) {
                RecordLog.info("[ApolloDataSource] Received config changes: " + change.toString());
            }
            loadAndUpdateRules();
        }
    };
    config.addChangeListener(configChangeListener, Sets.newHashSet(ruleKey));
}
 
Example #16
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #17
Source File: AbstractConfig.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean removeChangeListener(ConfigChangeListener listener) {
  m_interestedKeys.remove(listener);
  m_interestedKeyPrefixes.remove(listener);
  return m_listeners.remove(listener);
}
 
Example #18
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 #19
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));
}
 
Example #20
Source File: AbstractConfig.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Override
public void addChangeListener(ConfigChangeListener listener, Set<String> interestedKeys) {
  addChangeListener(listener, interestedKeys, null);
}
 
Example #21
Source File: AbstractConfig.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Override
public void addChangeListener(ConfigChangeListener listener) {
  addChangeListener(listener, null);
}
 
Example #22
Source File: ConfigPropertySource.java    From apollo with Apache License 2.0 4 votes vote down vote up
public void addChangeListener(ConfigChangeListener listener) {
  this.source.addChangeListener(listener);
}
 
Example #23
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #24
Source File: ApolloConfigWrapper.java    From shardingsphere with Apache License 2.0 2 votes vote down vote up
/**
 * Add config change listener.
 * 
 * @param listener listener
 * @param interestedKeys monitor keys
 */
public void addChangeListener(final ConfigChangeListener listener, final Set<String> interestedKeys) {
    apolloConfig.addChangeListener(listener, interestedKeys);
}
 
Example #25
Source File: ApolloConfigWrapper.java    From shardingsphere with Apache License 2.0 2 votes vote down vote up
/**
 * Add config change listener.
 *
 * @param listener listener
 * @param interestedKeys monitor keys
 * @param interestedKeyPrefixes monitor key prefixes
 */
public void addChangeListener(final ConfigChangeListener listener, final Set<String> interestedKeys, final Set<String> interestedKeyPrefixes) {
    apolloConfig.addChangeListener(listener, interestedKeys, interestedKeyPrefixes);
}