1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #1823 from yuhuangbin/nacos

Optimize the way to get NacosWatch#ThreadPoolTaskScheduler
This commit is contained in:
余黄彬 2020-11-20 14:54:12 +08:00 committed by GitHub
commit e8de5cc8aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View File

@ -32,7 +32,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/** /**
* @author xiaojing * @author xiaojing
@ -59,7 +59,7 @@ public class NacosDiscoveryClientConfiguration {
matchIfMissing = true) matchIfMissing = true)
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager, public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties, NacosDiscoveryProperties nacosDiscoveryProperties,
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) { ObjectProvider<ThreadPoolTaskScheduler> taskExecutorObjectProvider) {
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties, return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
taskExecutorObjectProvider); taskExecutorObjectProvider);
} }

View File

@ -27,7 +27,6 @@ import java.util.concurrent.atomic.AtomicLong;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager; import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event; import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.listener.EventListener;
@ -41,7 +40,6 @@ import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.SmartLifecycle; import org.springframework.context.SmartLifecycle;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/** /**
@ -66,14 +64,15 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
private final NacosDiscoveryProperties properties; private final NacosDiscoveryProperties properties;
private final TaskScheduler taskScheduler; private final ThreadPoolTaskScheduler taskScheduler;
public NacosWatch(NacosServiceManager nacosServiceManager, public NacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties properties, NacosDiscoveryProperties properties,
ObjectProvider<TaskScheduler> taskScheduler) { ObjectProvider<ThreadPoolTaskScheduler> taskScheduler) {
this.nacosServiceManager = nacosServiceManager; this.nacosServiceManager = nacosServiceManager;
this.properties = properties; this.properties = properties;
this.taskScheduler = taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler); this.taskScheduler = taskScheduler.stream().findAny()
.orElseGet(NacosWatch::getTaskScheduler);
} }
private static ThreadPoolTaskScheduler getTaskScheduler() { private static ThreadPoolTaskScheduler getTaskScheduler() {
@ -156,7 +155,7 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
if (this.watchFuture != null) { if (this.watchFuture != null) {
// shutdown current user-thread, // shutdown current user-thread,
// then the other daemon-threads will terminate automatic. // then the other daemon-threads will terminate automatic.
((ThreadPoolTaskScheduler) this.taskScheduler).shutdown(); this.taskScheduler.shutdown();
this.watchFuture.cancel(true); this.watchFuture.cancel(true);
} }
@ -167,7 +166,7 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
namingService.unsubscribe(properties.getService(), properties.getGroup(), namingService.unsubscribe(properties.getService(), properties.getGroup(),
Arrays.asList(properties.getClusterName()), eventListener); Arrays.asList(properties.getClusterName()), eventListener);
} }
catch (NacosException e) { catch (Exception e) {
log.error("namingService unsubscribe failed, properties:{}", properties, log.error("namingService unsubscribe failed, properties:{}", properties,
e); e);
} }