Skip to content

Commit

Permalink
merge #2725 (#2734)
Browse files Browse the repository at this point in the history
* merge #2725

* fix UT failure

* remove useless test

* fix unit test failures
  • Loading branch information
beiwei30 committed Nov 5, 2018
1 parent 0b0e8b5 commit f2d73f7
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 500 deletions.
10 changes: 1 addition & 9 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,6 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-bootstrap</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hessian-lite</artifactId>
Expand Down Expand Up @@ -424,7 +417,6 @@
<include>com.alibaba:dubbo-serialization-fst</include>
<include>com.alibaba:dubbo-serialization-kryo</include>
<include>com.alibaba:dubbo-serialization-jdk</include>
<include>com.alibaba:dubbo-bootstrap</include>
</includes>
</artifactSet>
<transformers>
Expand Down Expand Up @@ -588,4 +580,4 @@
</build>
</profile>
</profiles>
</project>
</project>
45 changes: 0 additions & 45 deletions dubbo-bootstrap/pom.xml

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,4 @@ public void destroy() {
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name).destroy();
}
}

/**
* Just for compatibility.
* It should be deleted in the next major version, say 2.7.x.
*/
@Deprecated
public static void destroyAll() {
DubboShutdownHook.getDubboShutdownHook().destroyAll();
}
}
}
7 changes: 1 addition & 6 deletions dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
<artifactId>dubbo-config-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-bootstrap</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
Expand Down Expand Up @@ -167,4 +162,4 @@
-->
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.alibaba.dubbo.config.annotation.Reference;
Expand Down Expand Up @@ -112,17 +111,16 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

@Override
public void destroy() {

// This will only be called for singleton scope bean, and expected to be called by spring shutdown hook when BeanFactory/ApplicationContext destroys.
// We will guarantee dubbo related resources being released with dubbo shutdown hook.

// for (ServiceConfig<?> serviceConfig : serviceConfigs) {
// try {
// serviceConfig.unexport();
// } catch (Throwable e) {
// logger.error(e.getMessage(), e);
// }
// }
// no need to destroy here
// see org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener
/*
for (ServiceConfig<?> serviceConfig : serviceConfigs) {
try {
serviceConfig.unexport();
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
for (ReferenceConfig<?> referenceConfig : referenceConfigs.values()) {
try {
Expand All @@ -131,6 +129,7 @@ public void destroy() {
logger.error(e.getMessage(), e);
}
}
*/
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractApplicationContext;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener;

/**
* ServiceFactoryBean
*
Expand All @@ -56,8 +56,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean

private static final long serialVersionUID = 213195494150089726L;

private static transient ApplicationContext SPRING_CONTEXT;

private final transient Service service;

private transient ApplicationContext applicationContext;
Expand All @@ -78,34 +76,11 @@ public ServiceBean(Service service) {
this.service = service;
}

public static ApplicationContext getSpringContext() {
return SPRING_CONTEXT;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
SpringExtensionFactory.addApplicationContext(applicationContext);
if (applicationContext != null) {
SPRING_CONTEXT = applicationContext;
try {
Method method = applicationContext.getClass().getMethod("addApplicationListener", new Class<?>[]{ApplicationListener.class}); // backward compatibility to spring 2.0.1
method.invoke(applicationContext, new Object[]{this});
supportedApplicationListener = true;
} catch (Throwable t) {
if (applicationContext instanceof AbstractApplicationContext) {
try {
Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", new Class<?>[]{ApplicationListener.class}); // backward compatibility to spring 2.0.1
if (!method.isAccessible()) {
method.setAccessible(true);
}
method.invoke(applicationContext, new Object[]{this});
supportedApplicationListener = true;
} catch (Throwable t2) {
}
}
}
}
supportedApplicationListener = addApplicationListener(applicationContext, this);
}

@Override
Expand Down Expand Up @@ -303,9 +278,8 @@ private void publishExportEvent() {

@Override
public void destroy() throws Exception {
// This will only be called for singleton scope bean, and expected to be called by spring shutdown hook when BeanFactory/ApplicationContext destroys.
// We will guarantee dubbo related resources being released with dubbo shutdown hook.
//unexport();
// no need to call unexport() here, see
// org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener
}

// merged from dubbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ConcurrentHashSet;

import com.alibaba.dubbo.config.DubboShutdownHook;
import com.alibaba.dubbo.config.spring.util.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;

import java.util.Set;

Expand All @@ -35,14 +40,21 @@ public class SpringExtensionFactory implements ExtensionFactory {

private static final Set<ApplicationContext> contexts = new ConcurrentHashSet<ApplicationContext>();

private static final ApplicationListener shutdownHookListener = new ShutdownHookListener();

public static void addApplicationContext(ApplicationContext context) {
contexts.add(context);
BeanFactoryUtils.addApplicationListener(context, shutdownHookListener);
}

public static void removeApplicationContext(ApplicationContext context) {
contexts.remove(context);
}

public static Set<ApplicationContext> getContexts() {
return contexts;
}

// currently for test purpose
public static void clearContexts() {
contexts.clear();
Expand Down Expand Up @@ -83,4 +95,17 @@ public <T> T getExtension(Class<T> type, String name) {
return null;
}

private static class ShutdownHookListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
// we call it anyway since dubbo shutdown hook make sure its destroyAll() is re-entrant.
// pls. note we should not remove dubbo shutdown hook when spring framework is present, this is because
// its shutdown hook may not be installed.
DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
shutdownHook.destroyAll();
}
}
}

}

0 comments on commit f2d73f7

Please sign in to comment.