Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchCatalogServicesTask.java
1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
2
3 import org.apache.http.HttpEntity;
4 import org.onap.msb.apiroute.wrapper.consulextend.CatalogClient;
5 import org.onap.msb.apiroute.wrapper.consulextend.cache.ServicesCatalogCache;
6 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import com.orbitz.consul.option.CatalogOptions;
11 import com.orbitz.consul.option.QueryOptions;
12
13 public class WatchCatalogServicesTask  extends WatchTask<HttpEntity> {
14
15         private final static Logger LOGGER = LoggerFactory
16                         .getLogger(WatchCatalogServicesTask.class);
17         
18         private ServicesCatalogCache servicesCache = null;
19         
20         public WatchCatalogServicesTask(
21                         final CatalogClient catalogClient,
22             final CatalogOptions catalogOptions,
23             final QueryOptions queryOptions,
24             final int watchSeconds)
25         {
26                 initCache(catalogClient,catalogOptions,queryOptions,watchSeconds);
27         }
28         
29         public WatchCatalogServicesTask(
30                         final CatalogClient catalogClient,
31             final int watchSeconds)
32         {
33                 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,watchSeconds);
34         }
35         
36         public WatchCatalogServicesTask(
37                         final CatalogClient catalogClient)
38         {
39                 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,10);
40         }
41         
42         private ServicesCatalogCache initCache(final CatalogClient catalogClient,
43             final CatalogOptions catalogOptions,
44             final QueryOptions queryOptions,
45             final int watchSeconds) {
46                 LOGGER.info("************create all services watch task*****************");
47                 servicesCache = ServicesCatalogCache.newCache(catalogClient,
48                                 catalogOptions, queryOptions, watchSeconds);
49
50                 servicesCache
51                 .addListener((Listener<HttpEntity>) new InternalListener());
52
53                 return servicesCache;
54         }
55         
56         @Override
57         public boolean startWatch() {
58                 // TODO Auto-generated method stub
59                 if(servicesCache!=null)
60                 {
61                         try {
62                                 servicesCache.start();
63                                 LOGGER.info("************start all services watch task*****************");
64                                 return true;
65                         } catch (Exception e) {
66                                 // TODO Auto-generated catch block
67                                 LOGGER.warn("start service list watch failed:", e);
68                         }
69                 }
70                 
71                 return false;
72         }
73
74         @Override
75         public boolean stopWatch() {
76                 // TODO Auto-generated method stub
77                 if (servicesCache != null) {
78                         try {
79                                 servicesCache.stop();
80                                 LOGGER.info("************stop all services watch task*****************");
81                                 return true;
82                         } catch (Exception e) {
83                                 // TODO Auto-generated catch block
84                                 LOGGER.warn("stop service list watch failed:", e);
85                         }
86                 }
87                 return false;
88         }
89
90 }