edit-icon download-icon

Listen for configurations

Last Updated: Jan 17, 2018


Use the ACM dynamic configuration listening API to enable ACM to send configuration change notifications.

  1. public static void addListener(String dataId, ConfigChangeListenerAdapter listener)

Request parameters

Parameter name Parameter type Description
dataId String Configuration ID. Use a naming rule like package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness.
It is recommended to indicate business meaning of the configuration in the “class” section.
Use lower case for all characters. Use alphabetical letters and these four special characters (“.”, “:”, “-“, “_”) only. Up to 256 characters are allowed.
group String Configuration group. To ensure uniqueness, format such as product name: module name (for example, ACM:Test) is preferred.
Use alphabetical letters and these four special characters (“.”, “:”, “-“, “_”) only. Up to 128 characters are allowed.
listener Config Change Listener Listener. Configuration changes go into the callback function of the listener.

Return values

Parameter type Description
String Configuration value. This value is returned through the callback function during initialization or configuration modification.

Request example

  1. // Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.
  2. ConfigService.init("${endpoint}", "${namespace}", "${accessKey}", "${secretKey}");
  3. // Add listeners to the configuration during initialization, which calls back a notification of configuration changes.
  4. ConfigService.addListener("${dataId}", "${group}", new ConfigChangeListener() {
  5. public void receiveConfigInfo(String configInfo) {
  6. // After the configuration is updated, the latest value is returned to the user by this callback function.
  7. System.out.println(configInfo);
  8. }
  9. });
  10. // Keep the main thread alive throughout the test, because the configuration subscription runs in a daemon thread, which exits once the main thread exits. The following code is not required in a real environment.
  11. while (true) {
  12. try {
  13. Thread.sleep(1000);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. }
Thank you! We've received your feedback.