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 0b8cb6d commit e828a40
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.bytecode.Wrapper;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.event.Event;
import org.apache.dubbo.common.event.EventDispatcher;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand All @@ -30,6 +32,8 @@
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.metadata.integration.MetadataReportService;
Expand Down Expand Up @@ -76,6 +80,8 @@ public class ServiceConfig<T> extends AbstractServiceConfig {

private static final long serialVersionUID = 3033787999037024738L;

private static final EventDispatcher eventDispathcer = EventDispatcher.getDefaultExtension();

/**
* The {@link Protocol} implementation with adaptive functionality,it will be different in different scenarios.
* A particular {@link Protocol} implementation is determined by the protocol attribute in the {@link URL}.
Expand Down Expand Up @@ -387,6 +393,13 @@ protected synchronized void doExport() {
path = interfaceName;
}
doExportUrls();

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

private void dispatch(Event event) {
eventDispathcer.dispatch(event);
}

private void checkRef() {
Expand Down Expand Up @@ -419,6 +432,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
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.common.event.Event;
import org.apache.dubbo.config.ServiceConfig;

/**
* {@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.common.event.Event;
import org.apache.dubbo.config.ServiceConfig;

/**
* {@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 @@ -19,9 +19,13 @@

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.event.EventDispatcher;
import org.apache.dubbo.common.event.EventListener;
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;
Expand All @@ -41,6 +45,7 @@

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

import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_BEAN;
import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_DEFAULT;
Expand All @@ -64,6 +69,8 @@ public class ServiceConfigTest {
private ServiceConfig<DemoServiceImpl> service2 = new ServiceConfig<DemoServiceImpl>();
private ServiceConfig<DemoServiceImpl> delayService = new ServiceConfig<DemoServiceImpl>();

private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension();

@BeforeEach
public void setUp() throws Exception {
MockProtocol2.delegate = protocolDelegate;
Expand Down Expand Up @@ -115,6 +122,9 @@ public void setUp() throws Exception {
delayService.setDelay(100);

ConfigManager.getInstance().clear();

// remove all event listeners
eventDispatcher.removeAllEventListeners();
}

@AfterEach
Expand All @@ -124,8 +134,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 @@ -167,8 +189,32 @@ public void testDelayExport() throws Exception {
public void testUnexport() throws Exception {
System.setProperty(Constants.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 e828a40

Please sign in to comment.