|
35 | 35 | import reactor.netty.http.server.HttpServerRequest;
|
36 | 36 | import reactor.netty.http.server.HttpServerResponse;
|
37 | 37 | import reactor.netty.http.server.HttpServerRoutes;
|
| 38 | +import reactor.netty.resources.LoopResources; |
38 | 39 |
|
39 | 40 | import org.springframework.boot.web.server.GracefulShutdownCallback;
|
40 | 41 | import org.springframework.boot.web.server.GracefulShutdownResult;
|
41 | 42 | import org.springframework.boot.web.server.PortInUseException;
|
42 | 43 | import org.springframework.boot.web.server.Shutdown;
|
43 | 44 | import org.springframework.boot.web.server.WebServer;
|
44 | 45 | import org.springframework.boot.web.server.WebServerException;
|
| 46 | +import org.springframework.http.client.reactive.ReactorResourceFactory; |
45 | 47 | import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
46 | 48 | import org.springframework.util.Assert;
|
47 | 49 |
|
@@ -74,19 +76,48 @@ public class NettyWebServer implements WebServer {
|
74 | 76 |
|
75 | 77 | private final GracefulShutdown gracefulShutdown;
|
76 | 78 |
|
| 79 | + private final ReactorResourceFactory resourceFactory; |
| 80 | + |
77 | 81 | private List<NettyRouteProvider> routeProviders = Collections.emptyList();
|
78 | 82 |
|
79 | 83 | private volatile DisposableServer disposableServer;
|
80 | 84 |
|
| 85 | + /** |
| 86 | + * Creates a new {@code NettyWebServer} instance. |
| 87 | + * @param httpServer the HTTP server |
| 88 | + * @param handlerAdapter the handler adapter |
| 89 | + * @param lifecycleTimeout the lifecycle timeout, may be {@code null} |
| 90 | + * @param shutdown the shutdown, may be {@code null} |
| 91 | + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of |
| 92 | + * {@link #NettyWebServer(HttpServer, ReactorHttpHandlerAdapter, Duration, Shutdown, ReactorResourceFactory)} |
| 93 | + */ |
| 94 | + @Deprecated(since = "3.2.0", forRemoval = true) |
81 | 95 | public NettyWebServer(HttpServer httpServer, ReactorHttpHandlerAdapter handlerAdapter, Duration lifecycleTimeout,
|
82 | 96 | Shutdown shutdown) {
|
| 97 | + this(httpServer, handlerAdapter, lifecycleTimeout, shutdown, null); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Creates a new {@code NettyWebServer} instance. |
| 102 | + * @param httpServer the HTTP server |
| 103 | + * @param handlerAdapter the handler adapter |
| 104 | + * @param lifecycleTimeout the lifecycle timeout, may be {@code null} |
| 105 | + * @param shutdown the shutdown, may be {@code null} |
| 106 | + * @param resourceFactory the factory for the server's {@link LoopResources loop |
| 107 | + * resources}, may be {@code null} |
| 108 | + * @since 3.2.0 |
| 109 | + * {@link #NettyWebServer(HttpServer, ReactorHttpHandlerAdapter, Duration, Shutdown, ReactorResourceFactory)} |
| 110 | + */ |
| 111 | + public NettyWebServer(HttpServer httpServer, ReactorHttpHandlerAdapter handlerAdapter, Duration lifecycleTimeout, |
| 112 | + Shutdown shutdown, ReactorResourceFactory resourceFactory) { |
83 | 113 | Assert.notNull(httpServer, "HttpServer must not be null");
|
84 | 114 | Assert.notNull(handlerAdapter, "HandlerAdapter must not be null");
|
85 | 115 | this.lifecycleTimeout = lifecycleTimeout;
|
86 | 116 | this.handler = handlerAdapter;
|
87 | 117 | this.httpServer = httpServer.channelGroup(new DefaultChannelGroup(new DefaultEventExecutor()));
|
88 | 118 | this.gracefulShutdown = (shutdown == Shutdown.GRACEFUL) ? new GracefulShutdown(() -> this.disposableServer)
|
89 | 119 | : null;
|
| 120 | + this.resourceFactory = resourceFactory; |
90 | 121 | }
|
91 | 122 |
|
92 | 123 | public void setRouteProviders(List<NettyRouteProvider> routeProviders) {
|
@@ -143,6 +174,11 @@ DisposableServer startHttpServer() {
|
143 | 174 | else {
|
144 | 175 | server = server.route(this::applyRouteProviders);
|
145 | 176 | }
|
| 177 | + if (this.resourceFactory != null) { |
| 178 | + LoopResources resources = this.resourceFactory.getLoopResources(); |
| 179 | + Assert.notNull(resources, "No LoopResources: is ReactorResourceFactory not initialized yet?"); |
| 180 | + server = server.runOn(resources); |
| 181 | + } |
146 | 182 | if (this.lifecycleTimeout != null) {
|
147 | 183 | return server.bindNow(this.lifecycleTimeout);
|
148 | 184 | }
|
|
0 commit comments