From d443a198a2954fbe0d943a6977337581f866b62d Mon Sep 17 00:00:00 2001 From: nicu marasoiu Date: Fri, 13 Mar 2015 10:51:12 +0200 Subject: [PATCH] @Value for properties --- .../cloud/consul/discovery/TtlScheduler.java | 85 ++++++++++--------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java index 201d13a5..db64ef51 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java @@ -5,10 +5,13 @@ import com.ecwid.consul.v1.agent.model.NewService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.scheduling.annotation.Scheduled; +import javax.annotation.PostConstruct; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; @@ -17,51 +20,55 @@ import java.util.concurrent.atomic.AtomicBoolean; * Created by nicu on 11.03.2015. */ @Slf4j -public class TtlScheduler implements ApplicationContextAware { - private static final int DEFAULT_TTL = 3; // must be > 1 - public static final int HEARTBEAT_INTERVAL_RATIO = 2 / 3; +@ConfigurationProperties +public class TtlScheduler { - private final Map serviceHeartbeats = new ConcurrentHashMap<>(); - private final AtomicBoolean heartbeatingNow = new AtomicBoolean(); - private volatile int ttl; - private volatile int heartbeatInterval; + private final Map serviceHeartbeats = new ConcurrentHashMap<>(); + private final AtomicBoolean heartbeatingNow = new AtomicBoolean(); - @Autowired - private ConsulClient client; + @Value("${consul.ttl:3}") + private volatile int ttl; - @Override - public void setApplicationContext(ApplicationContext context) throws BeansException { - ttl = context.getEnvironment().getProperty("consul.ttl", Integer.class, DEFAULT_TTL); - ttl = Math.min(2, ttl); - // heartbeat at 2/3 ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now - heartbeatInterval = Math.max(ttl - 1, Math.min(ttl * HEARTBEAT_INTERVAL_RATIO, 1)); + @Value("${consul.heartbeatIntervalRatio:0.6}") + private volatile int heartbeatIntervalRatio; + + private volatile int heartbeatInterval; + + @Autowired + private ConsulClient client; + + @PostConstruct + public void computeHeartbeatInterval() { + // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now + heartbeatInterval = Math.max(ttl - 1, Math.min(ttl * heartbeatIntervalRatio, 1)); } - /** - * Add a service to the checks loop. - */ - public void add(final NewService service) { - serviceHeartbeats.put(service.getId(), 0L); - } + /** + * Add a service to the checks loop. + */ + public void add(final NewService service) { + serviceHeartbeats.put(service.getId(), 0L); + } - public void remove(String serviceId) { - serviceHeartbeats.remove(serviceId); - } + public void remove(String serviceId) { + serviceHeartbeats.remove(serviceId); + } - public int getTTL() { - return ttl; - } + public int getTTL() { + return ttl; + } - @Scheduled(initialDelay = 0, fixedRate = 100) - void heartbeatServices() { - if (heartbeatingNow.compareAndSet(false, true)) { - for (String serviceId : serviceHeartbeats.keySet()) { - long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId); - if(latestHeartbeatDoneForService + heartbeatInterval <= System.currentTimeMillis()) { - client.agentCheckPass(serviceId); - serviceHeartbeats.put(serviceId, System.currentTimeMillis()); - } - } - } - } + @Scheduled(initialDelay = 0, fixedRate = 100) + private void heartbeatServices() { + if (heartbeatingNow.compareAndSet(false, true)) { + for (String serviceId : serviceHeartbeats.keySet()) { + long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId); + if (latestHeartbeatDoneForService + heartbeatInterval <= System + .currentTimeMillis()) { + client.agentCheckPass(serviceId); + serviceHeartbeats.put(serviceId, System.currentTimeMillis()); + } + } + } + } } \ No newline at end of file