Skip to content

Commit

Permalink
Polish apache#4093 : To add exported and unexported events for Servic…
Browse files Browse the repository at this point in the history
…eConfig
  • Loading branch information
mercyblitz committed May 20, 2019
1 parent 5c73899 commit b2dbff3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.event.ServiceConfigExportedEvent;
import org.apache.dubbo.config.event.ServiceConfigUnexportedEvent;
import org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker;
import org.apache.dubbo.config.support.Parameter;
import org.apache.dubbo.event.Event;
import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.metadata.integration.MetadataReportService;
import org.apache.dubbo.remoting.Constants;
import org.apache.dubbo.rpc.Exporter;
Expand Down Expand Up @@ -67,31 +71,31 @@
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.ConfigConstants.DUBBO_IP_TO_BIND;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EXPORT_KEY;
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_BIND;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
import static org.apache.dubbo.common.constants.RegistryConstants.EXPORT_KEY;
import static org.apache.dubbo.config.Constants.MULTICAST;
import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX;
import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
import static org.apache.dubbo.config.Constants.SCOPE_NONE;
import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.rpc.Constants.PROXY_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort;

/**
* ServiceConfig
Expand Down Expand Up @@ -144,6 +148,8 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
*/
private final List<Exporter<?>> exporters = new ArrayList<Exporter<?>>();

private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension();

/**
* The interface name of the exported service
*/
Expand Down Expand Up @@ -413,6 +419,9 @@ protected synchronized void doExport() {
path = interfaceName;
}
doExportUrls();

// dispatch a ServiceConfigExportedEvent since 2.7.2
dispatch(new ServiceConfigExportedEvent(this));
}

private void checkRef() {
Expand Down Expand Up @@ -445,6 +454,9 @@ public synchronized void unexport() {
exporters.clear();
}
unexported = true;

// dispatch a ServiceConfigUnExportedEvent since 2.7.2
dispatch(new ServiceConfigUnexportedEvent(this));
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down Expand Up @@ -1051,4 +1063,14 @@ public void setProviders(List<ProviderConfig> providers) {
public String getPrefix() {
return DUBBO + ".service." + interfaceName;
}

/**
* Dispatch an {@link Event event}
*
* @param event an {@link Event event}
* @since 2.7.2
*/
protected void dispatch(Event event) {
eventDispatcher.dispatch(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.config.event;

import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.event.Event;

/**
* {@link ServiceConfig} event post-{@link ServiceConfig#export() export}
*
* @since 2.7.2
*/
public class ServiceConfigExportedEvent extends Event {

public ServiceConfigExportedEvent(ServiceConfig source) {
super(source);
}

public ServiceConfig getServiceConfig() {
return (ServiceConfig) getSource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.config.event;

import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.event.Event;

/**
* {@link ServiceConfig} event post-{@link ServiceConfig#unexport() unexport}
*
* @since 2.7.2
*/
public class ServiceConfigUnexportedEvent extends Event {

public ServiceConfigUnexportedEvent(ServiceConfig source) {
super(source);
}

public ServiceConfig getServiceConfig() {
return (ServiceConfig) getSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import org.apache.dubbo.config.api.DemoService;
import org.apache.dubbo.config.api.Greeting;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.event.ServiceConfigExportedEvent;
import org.apache.dubbo.config.event.ServiceConfigUnexportedEvent;
import org.apache.dubbo.config.mock.MockProtocol2;
import org.apache.dubbo.config.mock.MockRegistryFactory2;
import org.apache.dubbo.config.mock.TestProxyFactory;
import org.apache.dubbo.config.provider.impl.DemoServiceImpl;
import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.event.EventListener;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
Expand All @@ -40,16 +44,17 @@

import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.ConfigConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EXPORT_KEY;
import static org.apache.dubbo.config.Constants.SHUTDOWN_TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.ConfigConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
Expand All @@ -75,6 +80,8 @@ public class ServiceConfigTest {
private ServiceConfig<DemoServiceImpl> service2 = new ServiceConfig<DemoServiceImpl>();
private ServiceConfig<DemoServiceImpl> delayService = new ServiceConfig<DemoServiceImpl>();

private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension();

@BeforeEach
public void setUp() throws Exception {
MockProtocol2.delegate = protocolDelegate;
Expand Down Expand Up @@ -135,8 +142,20 @@ public void tearDown() {

@Test
public void testExport() throws Exception {

AtomicReference reference = new AtomicReference();

eventDispatcher.addEventListener(new EventListener<ServiceConfigExportedEvent>() {
@Override
public void onEvent(ServiceConfigExportedEvent event) {
reference.set(event.getServiceConfig());
}
});

service.export();

assertEquals(service, reference.get());

assertThat(service.getExportedUrls(), hasSize(1));
URL url = service.toUrl();
assertThat(url.getProtocol(), equalTo("mockprotocol2"));
Expand Down Expand Up @@ -178,8 +197,32 @@ public void testDelayExport() throws Exception {
public void testUnexport() throws Exception {
System.setProperty(SHUTDOWN_WAIT_KEY, "0");
try {
AtomicReference reference = new AtomicReference();

eventDispatcher.addEventListener(new EventListener<ServiceConfigExportedEvent>() {
@Override
public void onEvent(ServiceConfigExportedEvent event) {
reference.set(event.getServiceConfig());
}
});

service.export();

assertEquals(service, reference.get());

assertTrue(reference.compareAndSet(service, null));

eventDispatcher.addEventListener(new EventListener<ServiceConfigUnexportedEvent>() {
@Override
public void onEvent(ServiceConfigUnexportedEvent event) {
reference.set(event.getServiceConfig());
}
});

service.unexport();

assertEquals(service, reference.get());

Thread.sleep(1000);
Mockito.verify(exporter, Mockito.atLeastOnce()).unexport();
} finally {
Expand Down

0 comments on commit b2dbff3

Please sign in to comment.