##### Coverage Report - ApolloConfigDemo

Classes in this File | Line Coverage | Branch Coverage | Complexity
ApolloConfigDemo |
0% | 0/20
0% | 0/6
| 2.25;2.25
ApolloConfigDemo$1 |
0% | 0/9
0% | 0/2
| 2.25;2.25

1 |  |
     import com.ctrip.framework.apollo.Config;
2 |  |
     import com.ctrip.framework.apollo.ConfigChangeListener;
3 |  |
     import com.ctrip.framework.apollo.ConfigService;
4 |  |
     import com.ctrip.framework.apollo.model.ConfigChange;
5 |  |
     import com.ctrip.framework.apollo.model.ConfigChangeEvent;
6 |  |
7 |  |
     import org.slf4j.Logger;
8 |  |
     import org.slf4j.LoggerFactory;
9 |  |
10 |  |
     import java.io.BufferedReader;
11 |  |
     import java.io.IOException;
12 |  |
     import java.io.InputStreamReader;
13 |  |
14 |  |
     /**
15 |  |
      * @author Jason Song(song_s@ctrip.com)
16 |  |
      */
17 | 0 |
     public class ApolloConfigDemo {
18 | 0 |
       private static final Logger logger = LoggerFactory.getLogger(ApolloConfigDemo.class);
19 |  |
       private Config config;
20 |  |
21 | 0 |
       public ApolloConfigDemo() {
22 | 0 |
         config = ConfigService.getAppConfig();
23 | 0 |
         config.addChangeListener(new ConfigChangeListener() {
24 |  |
           @Override
25 |  |
           public void onChange(ConfigChangeEvent changeEvent) {
26 | 0 |
             logger.info("Changes for namespace {}", changeEvent.getNamespace());
27 | 0 |
             for (String key : changeEvent.changedKeys()) {
28 | 0 |
               ConfigChange change = changeEvent.getChange(key);
29 | 0 |
               logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
30 | 0 |
                   change.getPropertyName(), change.getOldValue(), change.getNewValue(),
31 | 0 |
                   change.getChangeType());
32 | 0 |
             }
33 | 0 |
           }
34 |  |
         });
35 | 0 |
       }
36 |  |
37 |  |
       private String getConfig(String key) {
38 | 0 |
         String result = config.getProperty(key, "undefined");
39 | 0 |
         logger.info(String.format("Loading key: %s with value: %s", key, result));
40 | 0 |
         return result;
41 |  |
       }
42 |  |
43 |  |
       public static void main(String[] args) throws IOException {
44 | 0 |
         ApolloConfigDemo apolloConfigDemo = new ApolloConfigDemo();
45 | 0 |
         System.out.println(
46 |  |
             "Apollo Config Demo. Please input key to get the value.");
47 |  |
         while (true) {
48 | 0 |
           System.out.print("> ");
49 | 0 |
           String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
50 | 0 |
           if (input == null || input.length() == 0) {
51 | 0 |
             continue;
52 |  |
           }
53 | 0 |
           input = input.trim();
54 | 0 |
           if (input.equalsIgnoreCase("quit")) {
55 | 0 |
             System.exit(0);
56 |  |
           }
57 | 0 |
           apolloConfigDemo.getConfig(input);
58 | 0 |
         }
59 |  |
       }
60 |  |
     }