1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
3 import java.math.BigInteger;
6 import org.onap.msb.apiroute.wrapper.consulextend.HealthClient;
7 import org.onap.msb.apiroute.wrapper.consulextend.cache.ServiceHealthCache;
8 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
9 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 import com.orbitz.consul.option.CatalogOptions;
14 import com.orbitz.consul.option.QueryOptions;
16 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
17 private final static Logger LOGGER = LoggerFactory
18 .getLogger(WatchServiceHealthTask.class);
20 private ServiceHealthCache serviceHealthCache = null;
21 private String serviceName="";
23 public String getServiceName() {
27 public WatchServiceHealthTask(final HealthClient healthClient,
28 final String serviceName,final boolean passing,
29 final CatalogOptions catalogOptions, final int watchSeconds,
30 final QueryOptions queryOptions) {
31 initCache(healthClient, serviceName, passing, catalogOptions,
32 watchSeconds, queryOptions);
35 public WatchServiceHealthTask(final HealthClient healthClient,
36 final String serviceName,final boolean passing,
37 final int watchSeconds)
40 initCache(healthClient, serviceName, passing, CatalogOptions.BLANK,
41 watchSeconds, QueryOptions.BLANK);
44 public WatchServiceHealthTask(final HealthClient healthClient,
45 final String serviceName, final int watchSeconds)
48 initCache(healthClient, serviceName, true, CatalogOptions.BLANK,
49 watchSeconds, QueryOptions.BLANK);
52 private ServiceHealthCache initCache(final HealthClient healthClient,
53 final String serviceName,final boolean passing,
54 final CatalogOptions catalogOptions, final int watchSeconds,
55 final QueryOptions queryOptions) {
56 // LOGGER.info("************create {} watch task*****************",serviceName);
57 this.serviceName = serviceName;
58 serviceHealthCache = ServiceHealthCache.newCache(healthClient,
59 serviceName, passing, catalogOptions, watchSeconds,
63 .addListener((Listener<List<ServiceHealth>>) new InternalListener());
65 return serviceHealthCache;
68 public boolean startWatch() {
70 if(serviceHealthCache!=null)
73 serviceHealthCache.start();
74 LOGGER.info("************start {} watch task*****************",serviceName);
76 } catch (Exception e) {
77 // TODO Auto-generated catch block
78 LOGGER.warn("start service watch failed:", e);
86 public boolean stopWatch(){
87 if (serviceHealthCache != null) {
89 serviceHealthCache.stop();
90 LOGGER.info("************stop {} watch task*****************",serviceName);
92 } catch (Exception e) {
93 // TODO Auto-generated catch block
94 LOGGER.warn("stop service watch failed:", e);
102 public boolean resetIndex()
104 if (LOGGER.isDebugEnabled()) {
105 LOGGER.debug("reset " + serviceName + " consul index");
109 serviceHealthCache.updateIndex(BigInteger.valueOf(0));
113 for (WatchTask.Filter<List<ServiceHealth>> filter : getAllFilters()) {
114 if (filter instanceof ServiceModifyIndexFilter) {
115 if (LOGGER.isDebugEnabled()) {
116 LOGGER.debug("reset " + serviceName + " modify index");
118 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
122 if (LOGGER.isDebugEnabled()) {
123 LOGGER.debug("reset modify index.did not find filter:" + serviceName);