当前位置: 首页>>代码示例>>Java>>正文


Java ConfigChangeListener类代码示例

本文整理汇总了Java中com.ctrip.framework.apollo.ConfigChangeListener的典型用法代码示例。如果您正苦于以下问题:Java ConfigChangeListener类的具体用法?Java ConfigChangeListener怎么用?Java ConfigChangeListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ConfigChangeListener类属于com.ctrip.framework.apollo包,在下文中一共展示了ConfigChangeListener类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: fireConfigChange

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
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();
        }
      }
    });
  }
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:22,代码来源:AbstractConfig.java

示例2: SimpleApolloConfigDemo

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
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);
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:17,代码来源:SimpleApolloConfigDemo.java

示例3: processMethods

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
private void processMethods(final Object bean, Method[] declaredMethods) {
  for (final Method method : declaredMethods) {
    ApolloConfigChangeListener annotation = AnnotationUtils.findAnnotation(method, ApolloConfigChangeListener.class);
    if (annotation == null) {
      continue;
    }

    Class<?>[] parameterTypes = method.getParameterTypes();
    Preconditions.checkArgument(parameterTypes.length == 1,
        "Invalid number of parameters: %s for method: %s, should be 1", parameterTypes.length, method);
    Preconditions.checkArgument(ConfigChangeEvent.class.isAssignableFrom(parameterTypes[0]),
        "Invalid parameter type: %s for method: %s, should be ConfigChangeEvent", parameterTypes[0], method);

    ReflectionUtils.makeAccessible(method);
    String[] namespaces = annotation.value();
    for (String namespace : namespaces) {
      Config config = ConfigService.getConfig(namespace);

      config.addChangeListener(new ConfigChangeListener() {
        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
          ReflectionUtils.invokeMethod(method, bean, changeEvent);
        }
      });
    }
  }
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:28,代码来源:ApolloAnnotationProcessor.java

示例4: testLongPollRefresh

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
@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));
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:39,代码来源:ConfigIntegrationTest.java

示例5: addChangeListener

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
@Override
public void addChangeListener(ConfigChangeListener listener) {
  if (!m_listeners.contains(listener)) {
    m_listeners.add(listener);
  }
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:7,代码来源:AbstractConfig.java

示例6: testRefreshConfig

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
@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));
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:51,代码来源:ConfigIntegrationTest.java

示例7: testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified

import com.ctrip.framework.apollo.ConfigChangeListener; //导入依赖的package包/类
@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));
}
 
开发者ID:dewey-its,项目名称:apollo-custom,代码行数:44,代码来源:ConfigIntegrationTest.java


注:本文中的com.ctrip.framework.apollo.ConfigChangeListener类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。