1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
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;
25 import com.orbitz.consul.option.CatalogOptions;
26 import com.orbitz.consul.option.QueryOptions;
28 public class WatchCatalogServicesTask extends WatchTask<HttpEntity> {
30 private final static Logger LOGGER = LoggerFactory
31 .getLogger(WatchCatalogServicesTask.class);
33 private ServicesCatalogCache servicesCache = null;
35 public WatchCatalogServicesTask(
36 final CatalogClient catalogClient,
37 final CatalogOptions catalogOptions,
38 final QueryOptions queryOptions,
39 final int watchSeconds)
41 initCache(catalogClient,catalogOptions,queryOptions,watchSeconds);
44 public WatchCatalogServicesTask(
45 final CatalogClient catalogClient,
46 final int watchSeconds)
48 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,watchSeconds);
51 public WatchCatalogServicesTask(
52 final CatalogClient catalogClient)
54 initCache(catalogClient,CatalogOptions.BLANK,QueryOptions.BLANK,10);
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);
66 .addListener((Listener<HttpEntity>) new InternalListener());
72 public boolean startWatch() {
73 // TODO Auto-generated method stub
74 if(servicesCache!=null)
77 servicesCache.start();
78 LOGGER.info("************start all services watch task*****************");
80 } catch (Exception e) {
81 // TODO Auto-generated catch block
82 LOGGER.warn("start service list watch failed:", e);
90 public boolean stopWatch() {
91 // TODO Auto-generated method stub
92 if (servicesCache != null) {
95 LOGGER.info("************stop all services watch task*****************");
97 } catch (Exception e) {
98 // TODO Auto-generated catch block
99 LOGGER.warn("stop service list watch failed:", e);