Skip to content

Commit f2d73f7

Browse files
authored
merge #2725 (#2734)
* merge #2725 * fix UT failure * remove useless test * fix unit test failures
1 parent 0b0e8b5 commit f2d73f7

File tree

21 files changed

+142
-500
lines changed

21 files changed

+142
-500
lines changed

all/pom.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,6 @@
319319
<scope>compile</scope>
320320
<optional>true</optional>
321321
</dependency>
322-
<dependency>
323-
<groupId>com.alibaba</groupId>
324-
<artifactId>dubbo-bootstrap</artifactId>
325-
<version>${project.version}</version>
326-
<scope>compile</scope>
327-
<optional>true</optional>
328-
</dependency>
329322
<dependency>
330323
<groupId>com.alibaba</groupId>
331324
<artifactId>hessian-lite</artifactId>
@@ -424,7 +417,6 @@
424417
<include>com.alibaba:dubbo-serialization-fst</include>
425418
<include>com.alibaba:dubbo-serialization-kryo</include>
426419
<include>com.alibaba:dubbo-serialization-jdk</include>
427-
<include>com.alibaba:dubbo-bootstrap</include>
428420
</includes>
429421
</artifactSet>
430422
<transformers>
@@ -588,4 +580,4 @@
588580
</build>
589581
</profile>
590582
</profiles>
591-
</project>
583+
</project>

dubbo-bootstrap/pom.xml

Lines changed: 0 additions & 45 deletions
This file was deleted.

dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java

Lines changed: 0 additions & 111 deletions
This file was deleted.

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ProtocolConfig.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,4 @@ public void destroy() {
459459
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name).destroy();
460460
}
461461
}
462-
463-
/**
464-
* Just for compatibility.
465-
* It should be deleted in the next major version, say 2.7.x.
466-
*/
467-
@Deprecated
468-
public static void destroyAll() {
469-
DubboShutdownHook.getDubboShutdownHook().destroyAll();
470-
}
471-
}
462+
}

dubbo-config/dubbo-config-spring/pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
<artifactId>dubbo-config-api</artifactId>
3636
<version>${project.parent.version}</version>
3737
</dependency>
38-
<dependency>
39-
<groupId>com.alibaba</groupId>
40-
<artifactId>dubbo-bootstrap</artifactId>
41-
<version>${project.parent.version}</version>
42-
</dependency>
4338
<dependency>
4439
<groupId>org.springframework</groupId>
4540
<artifactId>spring-beans</artifactId>
@@ -167,4 +162,4 @@
167162
-->
168163
</plugins>
169164
</build>
170-
</project>
165+
</project>

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.alibaba.dubbo.config.MonitorConfig;
2929
import com.alibaba.dubbo.config.ProtocolConfig;
3030
import com.alibaba.dubbo.config.ProviderConfig;
31-
import com.alibaba.dubbo.config.ReferenceConfig;
3231
import com.alibaba.dubbo.config.RegistryConfig;
3332
import com.alibaba.dubbo.config.ServiceConfig;
3433
import com.alibaba.dubbo.config.annotation.Reference;
@@ -112,17 +111,16 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
112111

113112
@Override
114113
public void destroy() {
115-
116-
// This will only be called for singleton scope bean, and expected to be called by spring shutdown hook when BeanFactory/ApplicationContext destroys.
117-
// We will guarantee dubbo related resources being released with dubbo shutdown hook.
118-
119-
// for (ServiceConfig<?> serviceConfig : serviceConfigs) {
120-
// try {
121-
// serviceConfig.unexport();
122-
// } catch (Throwable e) {
123-
// logger.error(e.getMessage(), e);
124-
// }
125-
// }
114+
// no need to destroy here
115+
// see org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener
116+
/*
117+
for (ServiceConfig<?> serviceConfig : serviceConfigs) {
118+
try {
119+
serviceConfig.unexport();
120+
} catch (Throwable e) {
121+
logger.error(e.getMessage(), e);
122+
}
123+
}
126124
127125
for (ReferenceConfig<?> referenceConfig : referenceConfigs.values()) {
128126
try {
@@ -131,6 +129,7 @@ public void destroy() {
131129
logger.error(e.getMessage(), e);
132130
}
133131
}
132+
*/
134133
}
135134

136135
@Override

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ServiceBean.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import org.springframework.context.ApplicationEventPublisherAware;
3939
import org.springframework.context.ApplicationListener;
4040
import org.springframework.context.event.ContextRefreshedEvent;
41-
import org.springframework.context.support.AbstractApplicationContext;
4241

43-
import java.lang.reflect.Method;
4442
import java.util.ArrayList;
4543
import java.util.List;
4644
import java.util.Map;
4745

46+
import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener;
47+
4848
/**
4949
* ServiceFactoryBean
5050
*
@@ -56,8 +56,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
5656

5757
private static final long serialVersionUID = 213195494150089726L;
5858

59-
private static transient ApplicationContext SPRING_CONTEXT;
60-
6159
private final transient Service service;
6260

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

81-
public static ApplicationContext getSpringContext() {
82-
return SPRING_CONTEXT;
83-
}
84-
8579
@Override
8680
public void setApplicationContext(ApplicationContext applicationContext) {
8781
this.applicationContext = applicationContext;
8882
SpringExtensionFactory.addApplicationContext(applicationContext);
89-
if (applicationContext != null) {
90-
SPRING_CONTEXT = applicationContext;
91-
try {
92-
Method method = applicationContext.getClass().getMethod("addApplicationListener", new Class<?>[]{ApplicationListener.class}); // backward compatibility to spring 2.0.1
93-
method.invoke(applicationContext, new Object[]{this});
94-
supportedApplicationListener = true;
95-
} catch (Throwable t) {
96-
if (applicationContext instanceof AbstractApplicationContext) {
97-
try {
98-
Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", new Class<?>[]{ApplicationListener.class}); // backward compatibility to spring 2.0.1
99-
if (!method.isAccessible()) {
100-
method.setAccessible(true);
101-
}
102-
method.invoke(applicationContext, new Object[]{this});
103-
supportedApplicationListener = true;
104-
} catch (Throwable t2) {
105-
}
106-
}
107-
}
108-
}
83+
supportedApplicationListener = addApplicationListener(applicationContext, this);
10984
}
11085

11186
@Override
@@ -303,9 +278,8 @@ private void publishExportEvent() {
303278

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

311285
// merged from dubbox

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
import com.alibaba.dubbo.common.logger.LoggerFactory;
2222
import com.alibaba.dubbo.common.utils.ConcurrentHashSet;
2323

24+
import com.alibaba.dubbo.config.DubboShutdownHook;
25+
import com.alibaba.dubbo.config.spring.util.BeanFactoryUtils;
2426
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2527
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2628
import org.springframework.context.ApplicationContext;
29+
import org.springframework.context.ApplicationEvent;
30+
import org.springframework.context.ApplicationListener;
31+
import org.springframework.context.event.ContextClosedEvent;
2732

2833
import java.util.Set;
2934

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

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

43+
private static final ApplicationListener shutdownHookListener = new ShutdownHookListener();
44+
3845
public static void addApplicationContext(ApplicationContext context) {
3946
contexts.add(context);
47+
BeanFactoryUtils.addApplicationListener(context, shutdownHookListener);
4048
}
4149

4250
public static void removeApplicationContext(ApplicationContext context) {
4351
contexts.remove(context);
4452
}
4553

54+
public static Set<ApplicationContext> getContexts() {
55+
return contexts;
56+
}
57+
4658
// currently for test purpose
4759
public static void clearContexts() {
4860
contexts.clear();
@@ -83,4 +95,17 @@ public <T> T getExtension(Class<T> type, String name) {
8395
return null;
8496
}
8597

98+
private static class ShutdownHookListener implements ApplicationListener {
99+
@Override
100+
public void onApplicationEvent(ApplicationEvent event) {
101+
if (event instanceof ContextClosedEvent) {
102+
// we call it anyway since dubbo shutdown hook make sure its destroyAll() is re-entrant.
103+
// pls. note we should not remove dubbo shutdown hook when spring framework is present, this is because
104+
// its shutdown hook may not be installed.
105+
DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
106+
shutdownHook.destroyAll();
107+
}
108+
}
109+
}
110+
86111
}

0 commit comments

Comments
 (0)