From 128a1d7d057e48c3734829195b74bf0cb14eaeca Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Sun, 19 May 2019 22:48:36 +0800 Subject: [PATCH] Polish apache/incubator-dubbo#3984 : Refactor implementation by the event/listener abstract --- .../client/CompositeServiceDiscovery.java | 22 ++++++---- .../client/DefaultServiceInstance.java | 6 ++- .../EventPublishingServiceRegistry.java | 17 -------- .../registry/client/ServiceDiscovery.java | 35 ++++++++++++---- .../registry/client/ServiceRegistry.java | 4 +- .../client/DefaultServiceInstanceTest.java | 8 +++- .../EventPublishingServiceRegistryTest.java | 29 -------------- .../client/InMemoryServiceDiscovery.java | 21 +++++++--- .../registry/client/ServiceDiscoveryTest.java | 40 ++++++++++++++++++- 9 files changed, 108 insertions(+), 74 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/CompositeServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/CompositeServiceDiscovery.java index 9c17622b801..70047d4a9c9 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/CompositeServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/CompositeServiceDiscovery.java @@ -100,13 +100,6 @@ public Page getInstances(String serviceName, int offset, int re return page; } - @Override - public void registerListener(ServiceDiscoveryChangeListener listener) { - for (ServiceDiscovery serviceDiscovery : serviceDiscoveries) { - serviceDiscovery.registerListener(listener); - } - } - @Override public String toString() { return format("%s [composite : %s]", this.getClass().getSimpleName(), valueOf(serviceDiscoveries)); @@ -116,4 +109,19 @@ public String toString() { public int getPriority() { return Integer.MIN_VALUE; } + + @Override + public void addEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + serviceDiscoveries.forEach(serviceDiscovery -> serviceDiscovery.addEventListener(listener)); + } + + @Override + public void removeEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + serviceDiscoveries.forEach(serviceDiscovery -> serviceDiscovery.removeEventListener(listener)); + } + + @Override + public List getAllEventListeners() { + return serviceDiscoveries.stream().findFirst().get().getAllEventListeners(); + } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java index 67d3ba5fb5d..dda1faa680c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java @@ -35,9 +35,9 @@ public class DefaultServiceInstance implements ServiceInstance { private final int port; - private boolean enabled = true; + private boolean enabled; - private boolean healthy = true; + private boolean healthy; private Map metadata = new HashMap<>(); @@ -46,6 +46,8 @@ public DefaultServiceInstance(String id, String serviceName, String host, int po this.serviceName = serviceName; this.host = host; this.port = port; + this.enabled = true; + this.healthy = true; } public DefaultServiceInstance(String serviceName, String host, int port) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistry.java index 12326cf1e0d..1ca805ded26 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistry.java @@ -18,11 +18,9 @@ import org.apache.dubbo.common.event.Event; import org.apache.dubbo.common.event.EventDispatcher; -import org.apache.dubbo.common.event.EventListener; import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; -import java.util.List; import java.util.Optional; import static java.util.Optional.empty; @@ -127,19 +125,4 @@ protected final void executeWithEvents(Optional beforeEvent, * Stops the ServiceRegistry. This is a lifecycle method. */ protected abstract void doStop(); - - @Override - public void addEventListener(EventListener listener) throws NullPointerException, IllegalArgumentException { - eventDispatcher.addEventListener(listener); - } - - @Override - public void removeEventListener(EventListener listener) throws NullPointerException, IllegalArgumentException { - eventDispatcher.removeEventListener(listener); - } - - @Override - public List> getAllEventListeners() { - return eventDispatcher.getAllEventListeners(); - } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java index 910a9471fc1..6e429e7ab4f 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.registry.client; +import org.apache.dubbo.common.event.Listenable; import org.apache.dubbo.common.utils.DefaultPage; import org.apache.dubbo.common.utils.Page; import org.apache.dubbo.registry.client.event.ServiceDiscoveryChangeListener; @@ -26,17 +27,20 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import static java.lang.Integer.compare; import static java.util.Collections.emptyList; +import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableMap; +import static org.apache.dubbo.common.event.EventDispatcher.getDefaultExtension; /** * The common operations of Service Discovery * * @since 2.7.2 */ -public interface ServiceDiscovery extends Comparable { +public interface ServiceDiscovery extends Listenable, Comparable { /** * A human-readable description of the implementation @@ -161,13 +165,6 @@ default Map> getInstances(Iterable service return unmodifiableMap(instances); } - /** - * Register {@link ServiceDiscoveryChangeListener the service chagne event listener} - * - * @param listener {@link ServiceDiscoveryChangeListener the service change event listener} - */ - void registerListener(ServiceDiscoveryChangeListener listener); - /** * The priority of current {@link ServiceDiscovery} * @@ -188,4 +185,26 @@ default int getPriority() { default int compareTo(ServiceDiscovery that) { return compare(this.getPriority(), that.getPriority()); } + + @Override + default void addEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + Listenable.assertListener(listener); + getDefaultExtension().addEventListener(listener); + } + + @Override + default void removeEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + Listenable.assertListener(listener); + getDefaultExtension().removeEventListener(listener); + } + + @Override + default List getAllEventListeners() { + return unmodifiableList(getDefaultExtension() + .getAllEventListeners() + .stream() + .filter(listener -> listener instanceof ServiceDiscoveryChangeListener) + .map(listener -> ServiceDiscoveryChangeListener.class.cast(listener)) + .collect(Collectors.toList())); + } } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceRegistry.java index a34016f5ef3..07b1bfceba6 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceRegistry.java @@ -16,14 +16,12 @@ */ package org.apache.dubbo.registry.client; -import org.apache.dubbo.common.event.Listenable; - /** * The common interface to register and unregister for a service registry * * @since 2.7.2 */ -public interface ServiceRegistry extends Listenable { +public interface ServiceRegistry { /** * A human-readable description of the implementation diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/DefaultServiceInstanceTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/DefaultServiceInstanceTest.java index 28744c1291c..fb31571aed2 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/DefaultServiceInstanceTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/DefaultServiceInstanceTest.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.registry.client; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,9 +30,14 @@ */ public class DefaultServiceInstanceTest { - public static final DefaultServiceInstance INSTANCE = + public static DefaultServiceInstance INSTANCE = new DefaultServiceInstance("A", "127.0.0.1", 8080); + @BeforeEach + public void init() { + INSTANCE = new DefaultServiceInstance("A", "127.0.0.1", 8080); + } + @Test public void testDefaultValues() { assertTrue(INSTANCE.isEnabled()); diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistryTest.java index ea2fe0a8532..4a3f61f0b53 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/EventPublishingServiceRegistryTest.java @@ -16,15 +16,9 @@ */ package org.apache.dubbo.registry.client; -import org.apache.dubbo.common.event.EventListener; -import org.apache.dubbo.registry.client.event.ServiceInstanceEvent; -import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; -import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; - import org.junit.jupiter.api.Test; import static org.apache.dubbo.registry.client.DefaultServiceInstanceTest.INSTANCE; -import static org.apache.dubbo.registry.client.EventPublishingServiceRegistryTest.handleEvent; import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -40,8 +34,6 @@ public class EventPublishingServiceRegistryTest { @Test public void testRegister() { - serviceRegistry.addEventListener(new BeforeEventListener()); - serviceRegistry.addEventListener(new AfterEventListener()); serviceRegistry.register(serviceInstance); } @@ -54,27 +46,6 @@ public void testUpdate() { public void testUnregister() { serviceRegistry.unregister(serviceInstance); } - - static void handleEvent(ServiceInstanceEvent event) { - assertEquals(INSTANCE, event.getServiceInstance()); - assertEquals(serviceRegistry, event.getSource()); - } -} - -class BeforeEventListener implements EventListener { - - @Override - public void onEvent(ServiceInstancePreRegisteredEvent event) { - handleEvent(event); - } -} - -class AfterEventListener implements EventListener { - - @Override - public void onEvent(ServiceInstanceRegisteredEvent event) { - handleEvent(event); - } } class DefaultServiceRegistry extends EventPublishingServiceRegistry { diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java index e76a4777e27..59a526764d0 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/InMemoryServiceDiscovery.java @@ -48,12 +48,7 @@ public List getInstances(String serviceName) throws NullPointer return repository.computeIfAbsent(serviceName, s -> new LinkedList<>()); } - @Override - public void registerListener(ServiceDiscoveryChangeListener listener) { - dispatcher.addEventListener(listener); - } - - protected InMemoryServiceDiscovery addServiceInstance(ServiceInstance serviceInstance) { + public InMemoryServiceDiscovery addServiceInstance(ServiceInstance serviceInstance) { String serviceName = serviceInstance.getServiceName(); List serviceInstances = repository.computeIfAbsent(serviceName, s -> new LinkedList<>()); if (!serviceInstances.contains(serviceInstance)) { @@ -66,4 +61,18 @@ public String toString() { return "InMemoryServiceDiscovery"; } + @Override + public void addEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + + } + + @Override + public void removeEventListener(ServiceDiscoveryChangeListener listener) throws NullPointerException, IllegalArgumentException { + + } + + @Override + public List getAllEventListeners() { + return null; + } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java index 0ae94d653e2..12a3ac48fb5 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryTest.java @@ -16,8 +16,14 @@ */ package org.apache.dubbo.registry.client; +import org.apache.dubbo.common.event.EventDispatcher; +import org.apache.dubbo.common.event.EventListener; import org.apache.dubbo.common.utils.Page; +import org.apache.dubbo.registry.client.event.ServiceInstanceEvent; +import org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent; +import org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.HashSet; @@ -26,6 +32,8 @@ import static java.lang.Integer.MAX_VALUE; import static java.util.Arrays.asList; +import static org.apache.dubbo.registry.client.DefaultServiceInstanceTest.INSTANCE; +import static org.apache.dubbo.registry.client.ServiceDiscoveryTest.handleEvent; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,7 +45,17 @@ */ public class ServiceDiscoveryTest { - private InMemoryServiceDiscovery instance = new InMemoryServiceDiscovery(); + private static InMemoryServiceDiscovery instance; + + private EventDispatcher dispatcher = EventDispatcher.getDefaultExtension(); + + @BeforeEach + public void init() { + instance = new InMemoryServiceDiscovery(); + dispatcher.addEventListener(new BeforeEventListener()); + dispatcher.addEventListener(new AfterEventListener()); + dispatcher.removeAllEventListeners(); + } @Test public void testToString() { @@ -212,4 +230,24 @@ public void testGetInstancesWithHealthy() { } + static void handleEvent(ServiceInstanceEvent event) { + assertEquals(INSTANCE, event.getServiceInstance()); + assertEquals(instance, event.getSource()); + } +} + +class BeforeEventListener implements EventListener { + + @Override + public void onEvent(ServiceInstancePreRegisteredEvent event) { + handleEvent(event); + } +} + +class AfterEventListener implements EventListener { + + @Override + public void onEvent(ServiceInstanceRegisteredEvent event) { + handleEvent(event); + } }