mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish spring-cloud-incubator/spring-cloud-alibaba#623 : Optimize implementation and reuse the HeartbeatEvent background thread
This commit is contained in:
@@ -175,6 +175,10 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
applicationContext.addApplicationListener(new ApplicationListener<ServiceInstancesChangedEvent>() {
|
||||
@Override
|
||||
public void onApplicationEvent(ServiceInstancesChangedEvent event) {
|
||||
if (event.isProcessed()) { // If processed, return immediately
|
||||
return;
|
||||
}
|
||||
|
||||
String serviceName = event.getServiceName();
|
||||
Collection<ServiceInstance> serviceInstances = event.getServiceInstances();
|
||||
if (logger.isInfoEnabled()) {
|
||||
@@ -182,6 +186,8 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
serviceName, serviceInstances.size());
|
||||
}
|
||||
subscribeDubboServiceURLs(url, listener, serviceName, s -> serviceInstances);
|
||||
// Mark event to be processed
|
||||
event.process();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -18,6 +18,8 @@ package com.alibaba.cloud.dubbo.registry.event;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -32,6 +34,14 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
|
||||
|
||||
private final Collection<ServiceInstance> serviceInstances;
|
||||
|
||||
/**
|
||||
* Current event has been processed or not.
|
||||
* Typically, Spring Event was based on sync {@link ApplicationEventMulticaster}
|
||||
*
|
||||
* @see SimpleApplicationEventMulticaster
|
||||
*/
|
||||
private boolean processed = false;
|
||||
|
||||
/**
|
||||
* @param serviceName The name of service that was changed
|
||||
* @param serviceInstances all {@link ServiceInstance service instances}
|
||||
@@ -56,4 +66,20 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
|
||||
public Collection<ServiceInstance> getServiceInstances() {
|
||||
return serviceInstances;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark current event being processed
|
||||
*/
|
||||
public void process() {
|
||||
processed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current event has been processed or not
|
||||
*
|
||||
* @return if processed, return <code>true</code>, or <code>false</code>
|
||||
*/
|
||||
public boolean isProcessed() {
|
||||
return processed;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user