From 3fe7725fca92b00a5ff1c2786ee28f7f184c6824 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 23 Oct 2019 14:03:39 -0400 Subject: [PATCH] Removes Objects.requireNonNull() --- ...torLoadBalancerExchangeFilterFunction.java | 5 ++-- .../CachingServiceInstanceListSupplier.java | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactorLoadBalancerExchangeFilterFunction.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactorLoadBalancerExchangeFilterFunction.java index 16f4ab8b..9b70eef3 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactorLoadBalancerExchangeFilterFunction.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactorLoadBalancerExchangeFilterFunction.java @@ -17,7 +17,6 @@ package org.springframework.cloud.client.loadbalancer.reactive; import java.net.URI; -import java.util.Objects; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -41,7 +40,7 @@ import org.springframework.web.reactive.function.client.ExchangeFunction; public class ReactorLoadBalancerExchangeFilterFunction implements ExchangeFilterFunction { private static final Log LOG = LogFactory - .getLog(LoadBalancerExchangeFilterFunction.class); + .getLog(ReactorLoadBalancerExchangeFilterFunction.class); private final ReactiveLoadBalancer.Factory loadBalancerFactory; @@ -78,7 +77,7 @@ public class ReactorLoadBalancerExchangeFilterFunction implements ExchangeFilter if (LOG.isDebugEnabled()) { LOG.debug(String.format( "Load balancer has retrieved the instance for service %s: %s", - serviceId, Objects.requireNonNull(instance).getUri())); + serviceId, instance.getUri())); } ClientRequest newRequest = buildClientRequest(request, LoadBalancerUriTools.reconstructURI(instance, originalUrl)); diff --git a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/CachingServiceInstanceListSupplier.java b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/CachingServiceInstanceListSupplier.java index d5a20bf1..716d914f 100644 --- a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/CachingServiceInstanceListSupplier.java +++ b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/CachingServiceInstanceListSupplier.java @@ -17,8 +17,9 @@ package org.springframework.cloud.loadbalancer.core; import java.util.List; -import java.util.Objects; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import reactor.cache.CacheFlux; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -38,6 +39,9 @@ import org.springframework.cloud.client.ServiceInstance; */ public class CachingServiceInstanceListSupplier implements ServiceInstanceListSupplier { + private static final Log log = LogFactory + .getLog(CachingServiceInstanceListSupplier.class); + /** * Name of the service cache instance. */ @@ -55,8 +59,13 @@ public class CachingServiceInstanceListSupplier implements ServiceInstanceListSu this.serviceInstances = CacheFlux.lookup(key -> { // TODO: configurable cache name Cache cache = cacheManager.getCache(SERVICE_INSTANCE_CACHE_NAME); - List list = Objects.requireNonNull(cache).get(key, - List.class); + if (cache == null) { + if (log.isErrorEnabled()) { + log.error("Unable to find cache: " + SERVICE_INSTANCE_CACHE_NAME); + } + return Mono.empty(); + } + List list = cache.get(key, List.class); if (list == null || list.isEmpty()) { return Mono.empty(); } @@ -66,7 +75,14 @@ public class CachingServiceInstanceListSupplier implements ServiceInstanceListSu .doOnNext(instances -> { Cache cache = cacheManager .getCache(SERVICE_INSTANCE_CACHE_NAME); - Objects.requireNonNull(cache).put(key, instances); + if (cache == null) { + if (log.isErrorEnabled()) { + log.error("Unable to find cache for writing: " + SERVICE_INSTANCE_CACHE_NAME); + } + } + else { + cache.put(key, instances); + } }).then()); }