678bb870e877c006a6470d0232c60ea7b5d214bf
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.expose;
17
18 import org.apache.http.HttpEntity;
19 import org.onap.msb.apiroute.wrapper.consulextend.CatalogClient;
20 import org.onap.msb.apiroute.wrapper.consulextend.cache.ServicesCatalogCache;
21 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.orbitz.consul.option.CatalogOptions;
26 import com.orbitz.consul.option.QueryOptions;
27
28 public class WatchCatalogServicesTask  extends WatchTask<HttpEntity> {
29
30         private final static Logger LOGGER = LoggerFactory
31                         .getLogger(WatchCatalogServicesTask.class);
32         
33         private ServicesCatalogCache servicesCache = null;
34         
35         public WatchCatalogServicesTask(
36                         final CatalogClient catalogClient,
37             final CatalogOptions catalogOptions,
38             final QueryOptions queryOptions,
39             final int watchSeconds)
40         {
41                 initCache(catalogClient,catalogOptions,queryOptions,watchSeconds);
42         }
43         
44         public WatchCatalogServicesTask(
45                         final CatalogClient catalogClient,
46             final int watchSeconds)
47         {
48                 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,watchSeconds);
49         }
50         
51         public WatchCatalogServicesTask(
52                         final CatalogClient catalogClient)
53         {
54                 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,10);
55         }
56         
57         private ServicesCatalogCache initCache(final CatalogClient catalogClient,
58             final CatalogOptions catalogOptions,
59             final QueryOptions queryOptions,
60             final int watchSeconds) {
61                 LOGGER.info("************create all services watch task*****************");
62                 servicesCache = ServicesCatalogCache.newCache(catalogClient,
63                                 catalogOptions, queryOptions, watchSeconds);
64
65                 servicesCache
66                 .addListener((Listener<HttpEntity>) new InternalListener());
67
68                 return servicesCache;
69         }
70         
71         @Override
72         public boolean startWatch() {
73                 // TODO Auto-generated method stub
74                 if(servicesCache!=null)
75                 {
76                         try {
77                                 servicesCache.start();
78                                 LOGGER.info("************start all services watch task*****************");
79                                 return true;
80                         } catch (Exception e) {
81                                 // TODO Auto-generated catch block
82                                 LOGGER.warn("start service list watch failed:", e);
83                         }
84                 }
85                 
86                 return false;
87         }
88
89         @Override
90         public boolean stopWatch() {
91                 // TODO Auto-generated method stub
92                 if (servicesCache != null) {
93                         try {
94                                 servicesCache.stop();
95                                 LOGGER.info("************stop all services watch task*****************");
96                                 return true;
97                         } catch (Exception e) {
98                                 // TODO Auto-generated catch block
99                                 LOGGER.warn("stop service list watch failed:", e);
100                         }
101                 }
102                 return false;
103         }
104
105 }