mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Simplify ReactiveWebServerApplicationContext.ServerManager
Closes gh-21315
This commit is contained in:
parent
a688ac3f64
commit
f6d8c7d23c
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* https://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.springframework.boot.actuate.autoconfigure.web.reactive;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
import org.springframework.boot.web.server.WebServerException;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* Mock {@link ReactiveWebServerFactory}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
class MockReactiveWebServerFactory extends AbstractReactiveWebServerFactory {
|
||||
|
||||
private MockReactiveWebServer webServer;
|
||||
|
||||
@Override
|
||||
public WebServer getWebServer(HttpHandler httpHandler) {
|
||||
this.webServer = spy(new MockReactiveWebServer(httpHandler, getPort()));
|
||||
return this.webServer;
|
||||
}
|
||||
|
||||
MockReactiveWebServer getWebServer() {
|
||||
return this.webServer;
|
||||
}
|
||||
|
||||
static class MockReactiveWebServer implements WebServer {
|
||||
|
||||
private final int port;
|
||||
|
||||
private HttpHandler httpHandler;
|
||||
|
||||
private Map<String, HttpHandler> httpHandlerMap;
|
||||
|
||||
MockReactiveWebServer(HttpHandler httpHandler, int port) {
|
||||
this.httpHandler = httpHandler;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
MockReactiveWebServer(Map<String, HttpHandler> httpHandlerMap, int port) {
|
||||
this.httpHandlerMap = httpHandlerMap;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
HttpHandler getHttpHandler() {
|
||||
return this.httpHandler;
|
||||
}
|
||||
|
||||
Map<String, HttpHandler> getHttpHandlerMap() {
|
||||
return this.httpHandlerMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws WebServerException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws WebServerException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -60,7 +60,7 @@ class ReactiveManagementContextFactoryTests {
|
||||
|
||||
@Bean
|
||||
ReactiveWebServerFactory reactiveWebServerFactory() {
|
||||
return mock(ReactiveWebServerFactory.class);
|
||||
return new MockReactiveWebServerFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -31,6 +31,7 @@ import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@ -90,7 +91,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
String webServerFactoryBeanName = getWebServerFactoryBeanName();
|
||||
ReactiveWebServerFactory webServerFactory = getWebServerFactory(webServerFactoryBeanName);
|
||||
boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit();
|
||||
this.serverManager = ServerManager.get(webServerFactory, lazyInit);
|
||||
this.serverManager = new ServerManager(webServerFactory, lazyInit);
|
||||
}
|
||||
initPropertySources();
|
||||
}
|
||||
@ -124,8 +125,11 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
|
||||
private WebServer startReactiveWebServer() {
|
||||
ServerManager serverManager = this.serverManager;
|
||||
ServerManager.start(serverManager, this::getHttpHandler);
|
||||
return ServerManager.getWebServer(serverManager);
|
||||
if (serverManager != null) {
|
||||
serverManager.start(this::getHttpHandler);
|
||||
return serverManager.server;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -166,11 +170,13 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
|
||||
private void stopAndReleaseReactiveWebServer() {
|
||||
ServerManager serverManager = this.serverManager;
|
||||
try {
|
||||
ServerManager.stop(serverManager);
|
||||
}
|
||||
finally {
|
||||
this.serverManager = null;
|
||||
if (serverManager != null) {
|
||||
try {
|
||||
serverManager.server.stop();
|
||||
}
|
||||
finally {
|
||||
this.serverManager = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,7 +187,8 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
*/
|
||||
@Override
|
||||
public WebServer getWebServer() {
|
||||
return ServerManager.getWebServer(this.serverManager);
|
||||
ServerManager serverManager = this.serverManager;
|
||||
return (serverManager != null) ? serverManager.server : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -225,6 +232,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
private volatile HttpHandler handler;
|
||||
|
||||
private ServerManager(ReactiveWebServerFactory factory, boolean lazyInit) {
|
||||
Assert.notNull(factory, "ReactiveWebServerFactory must not be null");
|
||||
this.handler = this::handleUninitialized;
|
||||
this.server = factory.getWebServer(this);
|
||||
this.lazyInit = lazyInit;
|
||||
@ -243,31 +251,10 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli
|
||||
return this.handler;
|
||||
}
|
||||
|
||||
static ServerManager get(ReactiveWebServerFactory factory, boolean lazyInit) {
|
||||
return new ServerManager(factory, lazyInit);
|
||||
}
|
||||
|
||||
static WebServer getWebServer(ServerManager manager) {
|
||||
return (manager != null) ? manager.server : null;
|
||||
}
|
||||
|
||||
static void start(ServerManager manager, Supplier<HttpHandler> handlerSupplier) {
|
||||
if (manager != null && manager.server != null) {
|
||||
manager.handler = manager.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier))
|
||||
: handlerSupplier.get();
|
||||
manager.server.start();
|
||||
}
|
||||
}
|
||||
|
||||
static void stop(ServerManager manager) {
|
||||
if (manager != null && manager.server != null) {
|
||||
try {
|
||||
manager.server.stop();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
private void start(Supplier<HttpHandler> handlerSupplier) {
|
||||
this.handler = this.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier))
|
||||
: handlerSupplier.get();
|
||||
this.server.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user