This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 83d020c [3.0] Support servlet takeover mode (#8123)
83d020c is described below
commit 83d020cdc0052689538130a89d647f1f0aa8a433
Author: Gong Dewei <[email protected]>
AuthorDate: Thu Jun 24 13:21:52 2021 +0800
[3.0] Support servlet takeover mode (#8123)
* Support servlet takeover mode
* Fix NPE
---
.../context/DubboBootstrapApplicationListener.java | 22 +++++---
.../DubboBootstrapServletContextListener.java | 58 ++++++++++++++++++++++
2 files changed, 74 insertions(+), 6 deletions(-)
diff --git
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
index 6a2ae27..f40e663 100644
---
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
+++
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
@@ -46,17 +46,23 @@ public class DubboBootstrapApplicationListener extends
OnceApplicationContextEve
private final DubboBootstrap dubboBootstrap;
public DubboBootstrapApplicationListener() {
- this.dubboBootstrap = DubboBootstrap.getInstance();
- this.dubboBootstrap.setTakeoverMode(BootstrapTakeoverMode.SPRING);
+ this.dubboBootstrap = initBootstrap();
}
public DubboBootstrapApplicationListener(ApplicationContext
applicationContext) {
super(applicationContext);
- this.dubboBootstrap = DubboBootstrap.getInstance();
- this.dubboBootstrap.setTakeoverMode(BootstrapTakeoverMode.SPRING);
+ this.dubboBootstrap = initBootstrap();
DubboBootstrapStartStopListenerSpringAdapter.applicationContext =
applicationContext;
}
+ private DubboBootstrap initBootstrap() {
+ DubboBootstrap dubboBootstrap = DubboBootstrap.getInstance();
+ if (dubboBootstrap.getTakeoverMode() != BootstrapTakeoverMode.MANUAL) {
+ dubboBootstrap.setTakeoverMode(BootstrapTakeoverMode.SPRING);
+ }
+ return dubboBootstrap;
+ }
+
@Override
public void onApplicationContextEvent(ApplicationContextEvent event) {
if (DubboBootstrapStartStopListenerSpringAdapter.applicationContext ==
null) {
@@ -70,11 +76,15 @@ public class DubboBootstrapApplicationListener extends
OnceApplicationContextEve
}
private void onContextRefreshedEvent(ContextRefreshedEvent event) {
- dubboBootstrap.start();
+ if (dubboBootstrap.getTakeoverMode() == BootstrapTakeoverMode.SPRING) {
+ dubboBootstrap.start();
+ }
}
private void onContextClosedEvent(ContextClosedEvent event) {
- DubboShutdownHook.getDubboShutdownHook().run();
+ if (dubboBootstrap.getTakeoverMode() == BootstrapTakeoverMode.SPRING) {
+ DubboShutdownHook.getDubboShutdownHook().run();
+ }
}
@Override
diff --git
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapServletContextListener.java
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapServletContextListener.java
new file mode 100644
index 0000000..c270b8b
--- /dev/null
+++
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapServletContextListener.java
@@ -0,0 +1,58 @@
+/*
+ * 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.spring.context;
+
+import org.apache.dubbo.config.DubboShutdownHook;
+import org.apache.dubbo.config.bootstrap.BootstrapTakeoverMode;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
+
+/**
+ * DubboBootstrap lifecycle controller for Servlet container.
+ *
+ * @see DubboBootstrapApplicationListener
+ */
+@WebListener
+public class DubboBootstrapServletContextListener implements
ServletContextListener {
+
+ public DubboBootstrapServletContextListener() {
+ // Set takeover mode when servlet container is starting
+ if (DubboBootstrap.getInstance().getTakeoverMode() ==
BootstrapTakeoverMode.AUTO) {
+
DubboBootstrap.getInstance().setTakeoverMode(BootstrapTakeoverMode.SERVLET);
+ }
+ }
+
+ @Override
+ public void contextInitialized(ServletContextEvent servletContextEvent) {
+ // If takeover mode is not changed on servlet container is
initialized, it means dubbo does not running in Spring context.
+ // Otherwise, the takeover mode will be changed if dubbo is loaded by
Spring.
+ if (DubboBootstrap.getInstance().getTakeoverMode() ==
BootstrapTakeoverMode.SERVLET) {
+ DubboBootstrap.getInstance().start();
+ }
+ }
+
+ @Override
+ public void contextDestroyed(ServletContextEvent servletContextEvent) {
+ if (DubboBootstrap.getInstance().getTakeoverMode() ==
BootstrapTakeoverMode.SERVLET) {
+ DubboShutdownHook.getDubboShutdownHook().run();
+ }
+ }
+
+}