88import com .github .netty .protocol .HttpServletProtocol ;
99import com .github .netty .protocol .servlet .ServletContext ;
1010import com .github .netty .protocol .servlet .ServletRegistration ;
11+ import com .github .netty .protocol .servlet .util .HttpLazyThreadPool ;
1112import com .github .netty .springboot .NettyProperties ;
1213import org .springframework .boot .web .reactive .server .ConfigurableReactiveWebServerFactory ;
1314import org .springframework .boot .web .server .WebServer ;
2324import java .net .InetSocketAddress ;
2425import java .net .UnknownHostException ;
2526import java .util .Collection ;
26- import java .util .Objects ;
2727import java .util .TreeSet ;
2828import java .util .function .Supplier ;
2929
@@ -83,12 +83,13 @@ public WebServer getWebServer(HttpHandler httpHandler) {
8383 //Server port
8484 InetSocketAddress serverAddress = getServerSocketAddress (getAddress (), getPort ());
8585 ServletContext servletContext = getServletContext ();
86- if (servletContext != null ) {
87- ServletRegistration .Dynamic servletRegistration = servletContext .addServlet ("default" , new ServletHttpHandlerAdapter (httpHandler ));
88- servletRegistration .setAsyncSupported (true );
89- servletRegistration .addMapping ("/" );
90- servletContext .setServerAddress (serverAddress );
86+ if (servletContext == null ) {
87+ servletContext = createHttpServletProtocolSpringAdapter ().getServletContext ();
9188 }
89+ ServletRegistration .Dynamic servletRegistration = servletContext .addServlet ("default" , new ServletHttpHandlerAdapter (httpHandler ));
90+ servletRegistration .setAsyncSupported (true );
91+ servletRegistration .addMapping ("/" );
92+ servletContext .setServerAddress (serverAddress );
9293 return new NettyTcpServer (serverAddress , properties , protocolHandlers , serverListeners , channelHandlerSupplier );
9394 } catch (Exception e ) {
9495 throw new IllegalStateException (e .getMessage (), e );
@@ -103,7 +104,10 @@ public WebServer getWebServer(HttpHandler httpHandler) {
103104 */
104105 @ Override
105106 public WebServer getWebServer (ServletContextInitializer ... initializers ) {
106- ServletContext servletContext = Objects .requireNonNull (getServletContext ());
107+ ServletContext servletContext = getServletContext ();
108+ if (servletContext == null ) {
109+ servletContext = createHttpServletProtocolSpringAdapter ().getServletContext ();
110+ }
107111 try {
108112 //Server port
109113 InetSocketAddress serverAddress = getServerSocketAddress (getAddress (), getPort ());
@@ -162,6 +166,14 @@ public ServletContext getServletContext() {
162166 return null ;
163167 }
164168
169+ protected HttpServletProtocol createHttpServletProtocolSpringAdapter () {
170+ HttpLazyThreadPool threadPool = new HttpLazyThreadPool ("NettyX-http" );
171+ HttpServletProtocolSpringAdapter adapter = new HttpServletProtocolSpringAdapter (new NettyProperties (), null , threadPool , threadPool );
172+ protocolHandlers .add (adapter );
173+ serverListeners .add (adapter );
174+ return adapter ;
175+ }
176+
165177 public Collection <ProtocolHandler > getProtocolHandlers () {
166178 return protocolHandlers ;
167179 }
0 commit comments