Skip to content

Commit

Permalink
Shut down client on stop.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Oct 19, 2023
1 parent 522905b commit dbbde33
Showing 1 changed file with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.Lifecycle;
import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;

Expand All @@ -43,7 +44,7 @@
*
* @author Spencer Gibb
*/
public class CloudEurekaClient extends DiscoveryClient {
public class CloudEurekaClient extends DiscoveryClient implements Lifecycle {

private static final Log log = LogFactory.getLog(CloudEurekaClient.class);

Expand All @@ -57,6 +58,10 @@ public class CloudEurekaClient extends DiscoveryClient {

private final AtomicReference<EurekaHttpClient> eurekaHttpClient = new AtomicReference<>();

private final Object lifecycleMonitor = new Object();

private volatile boolean running = true;

public CloudEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config,
TransportClientFactories transportClientFactories, ApplicationEventPublisher publisher) {
this(applicationInfoManager, config, transportClientFactories, null, publisher);
Expand All @@ -68,8 +73,8 @@ public CloudEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaCl
super(applicationInfoManager, config, transportClientFactories, args);
this.applicationInfoManager = applicationInfoManager;
this.publisher = publisher;
this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport");
ReflectionUtils.makeAccessible(this.eurekaTransportField);
eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport");
ReflectionUtils.makeAccessible(eurekaTransportField);
}

public ApplicationInfoManager getApplicationInfoManager() {
Expand All @@ -90,20 +95,19 @@ public InstanceInfo getInstanceInfo(String appname, String instanceId) {
}

EurekaHttpClient getEurekaHttpClient() {
if (this.eurekaHttpClient.get() == null) {
if (eurekaHttpClient.get() == null) {
try {
Object eurekaTransport = this.eurekaTransportField.get(this);
Object eurekaTransport = eurekaTransportField.get(this);

Check warning on line 100 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L100

Added line #L100 was not covered by tests
Field registrationClientField = ReflectionUtils.findField(eurekaTransport.getClass(),
"registrationClient");
ReflectionUtils.makeAccessible(registrationClientField);
this.eurekaHttpClient.compareAndSet(null,
(EurekaHttpClient) registrationClientField.get(eurekaTransport));
eurekaHttpClient.compareAndSet(null, (EurekaHttpClient) registrationClientField.get(eurekaTransport));

Check warning on line 104 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L104

Added line #L104 was not covered by tests
}
catch (IllegalAccessException e) {
log.error("error getting EurekaHttpClient", e);
}
}
return this.eurekaHttpClient.get();
return eurekaHttpClient.get();

Check warning on line 110 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L110

Added line #L110 was not covered by tests
}

public void setStatus(InstanceStatus newStatus, InstanceInfo info) {
Expand All @@ -114,12 +118,37 @@ public void setStatus(InstanceStatus newStatus, InstanceInfo info) {
protected void onCacheRefreshed() {
super.onCacheRefreshed();

if (this.cacheRefreshedCount != null) { // might be called during construction and
if (cacheRefreshedCount != null) { // might be called during construction and
// will be null
long newCount = this.cacheRefreshedCount.incrementAndGet();
long newCount = cacheRefreshedCount.incrementAndGet();

Check warning on line 123 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L123

Added line #L123 was not covered by tests
log.trace("onCacheRefreshed called with count: " + newCount);
this.publisher.publishEvent(new HeartbeatEvent(this, newCount));
publisher.publishEvent(new HeartbeatEvent(this, newCount));

Check warning on line 125 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L125

Added line #L125 was not covered by tests
}
}

Check warning on line 127 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L127

Added line #L127 was not covered by tests

@Override
public void start() {
synchronized (lifecycleMonitor) {

Check warning on line 131 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L131

Added line #L131 was not covered by tests
if (!isRunning()) {
running = true;

Check warning on line 133 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L133

Added line #L133 was not covered by tests
}
}
}

@Override
public void stop() {
synchronized (lifecycleMonitor) {
if (isRunning()) {
applicationInfoManager.refreshLeaseInfoIfRequired();
shutdown();
running = false;
}
}
}

@Override
public boolean isRunning() {
return running;
}

}

0 comments on commit dbbde33

Please sign in to comment.