Skip to content

Commit 4d9ba1a

Browse files
ralf0131chickenlj
authored andcommitted
Merge pull request #2126, ensure compatibility for elegant shutdown under servlet container.
Fixes #1998
1 parent 3eae624 commit 4d9ba1a

File tree

9 files changed

+193
-42
lines changed

9 files changed

+193
-42
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,21 @@
112112
<artifactId>javax.el</artifactId>
113113
<scope>test</scope>
114114
</dependency>
115-
116115
<dependency>
117116
<groupId>org.springframework</groupId>
118117
<artifactId>spring-tx</artifactId>
119118
<scope>test</scope>
120119
</dependency>
121-
122120
<dependency>
123121
<groupId>org.springframework</groupId>
124122
<artifactId>spring-test</artifactId>
125123
<scope>test</scope>
126124
</dependency>
125+
<dependency>
126+
<groupId>org.apache.tomcat.embed</groupId>
127+
<artifactId>tomcat-embed-core</artifactId>
128+
<scope>test</scope>
129+
</dependency>
127130

128131
</dependencies>
129132
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.initializer;
18+
19+
import org.springframework.context.ApplicationContextInitializer;
20+
import org.springframework.context.ConfigurableApplicationContext;
21+
22+
/**
23+
* Automatically register {@link DubboApplicationListener} to Spring context
24+
* A {@link org.springframework.web.context.ContextLoaderListener} class is defined in
25+
* src/main/resources/META-INF/web-fragment.xml
26+
* In the web-fragment.xml, {@link DubboApplicationContextInitializer} is defined in context params.
27+
* This file will be discovered if running under a servlet 3.0+ container.
28+
* Even if user specifies {@link org.springframework.web.context.ContextLoaderListener} in web.xml,
29+
* it will be merged to web.xml.
30+
* If user specifies <metadata-complete="true" /> in web.xml, this will no take effect,
31+
* unless user configures {@link DubboApplicationContextInitializer} explicitly in web.xml.
32+
*/
33+
public class DubboApplicationContextInitializer implements ApplicationContextInitializer {
34+
35+
@Override
36+
public void initialize(ConfigurableApplicationContext applicationContext) {
37+
applicationContext.addApplicationListener(new DubboApplicationListener());
38+
}
39+
}

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboWebApplicationInitializer.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
4+
5+
<name>dubbo-fragment</name>
6+
7+
<ordering>
8+
<before>
9+
<others/>
10+
</before>
11+
</ordering>
12+
13+
<context-param>
14+
<param-name>contextInitializerClasses</param-name>
15+
<param-value>org.apache.dubbo.config.spring.initializer.DubboApplicationContextInitializer</param-value>
16+
</context-param>
17+
18+
<listener>
19+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20+
</listener>
21+
22+
</web-fragment>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.initializer;
18+
19+
import org.apache.catalina.core.StandardContext;
20+
import org.apache.catalina.startup.ContextConfig;
21+
import org.apache.catalina.startup.Tomcat;
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
import org.springframework.web.context.ContextLoaderListener;
25+
26+
27+
public class DubboApplicationContextInitializerTest {
28+
29+
@Test
30+
public void testSpringContextLoaderListenerInWebXml() throws Exception {
31+
Tomcat tomcat = new Tomcat();
32+
tomcat.setBaseDir("src/test/resources");
33+
tomcat.setPort(12345);
34+
StandardContext context = new StandardContext();
35+
context.setName("test");
36+
context.setDocBase("test");
37+
context.setPath("/test");
38+
context.addLifecycleListener(new ContextConfig());
39+
tomcat.getHost().addChild(context);
40+
tomcat.start();
41+
// there should be 1 application listener
42+
Assert.assertEquals(1, context.getApplicationLifecycleListeners().length);
43+
// the first one should be Spring's built in ContextLoaderListener.
44+
Assert.assertTrue(context.getApplicationLifecycleListeners()[0] instanceof ContextLoaderListener);
45+
tomcat.stop();
46+
tomcat.destroy();
47+
}
48+
49+
@Test
50+
public void testNoListenerInWebXml() throws Exception {
51+
Tomcat tomcat = new Tomcat();
52+
tomcat.setBaseDir("src/test/resources");
53+
tomcat.setPort(12345);
54+
StandardContext context = new StandardContext();
55+
context.setName("test2");
56+
context.setDocBase("test2");
57+
context.setPath("/test2");
58+
context.addLifecycleListener(new ContextConfig());
59+
tomcat.getHost().addChild(context);
60+
tomcat.start();
61+
// there should be 1 application listener
62+
Assert.assertEquals(1, context.getApplicationLifecycleListeners().length);
63+
// the first one should be Spring's built in ContextLoaderListener.
64+
Assert.assertTrue(context.getApplicationLifecycleListeners()[0] instanceof ContextLoaderListener);
65+
tomcat.stop();
66+
tomcat.destroy();
67+
}
68+
69+
@Test
70+
public void testMetadataComplete() throws Exception {
71+
Tomcat tomcat = new Tomcat();
72+
tomcat.setBaseDir("src/test/resources");
73+
tomcat.setPort(12345);
74+
StandardContext context = new StandardContext();
75+
context.setName("test3");
76+
context.setDocBase("test3");
77+
context.setPath("/test3");
78+
context.addLifecycleListener(new ContextConfig());
79+
tomcat.getHost().addChild(context);
80+
tomcat.start();
81+
// there should be no application listeners
82+
Assert.assertEquals(0, context.getApplicationLifecycleListeners().length);
83+
tomcat.stop();
84+
tomcat.destroy();
85+
}
86+
87+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
</beans>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.1">
3+
<display-name>dubbo-demo</display-name>
4+
5+
<context-param>
6+
<param-name>contextConfigLocation</param-name>
7+
<param-value>classpath:applicationContext.xml</param-value>
8+
</context-param>
9+
10+
<listener>
11+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12+
</listener>
13+
14+
</web-app>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.1">
3+
<display-name>dubbo-demo</display-name>
4+
5+
<context-param>
6+
<param-name>contextConfigLocation</param-name>
7+
<param-value>classpath:applicationContext.xml</param-value>
8+
</context-param>
9+
10+
</web-app>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.1" metadata-complete="true">
3+
<display-name>dubbo-demo</display-name>
4+
5+
<context-param>
6+
<param-name>contextConfigLocation</param-name>
7+
<param-value>classpath:applicationContext.xml</param-value>
8+
</context-param>
9+
10+
</web-app>

0 commit comments

Comments
 (0)