0
# Reactive Web Server
1
2
Reactive web server implementation using Jetty for WebFlux applications. Provides non-blocking, reactive HTTP server capabilities optimized for high-concurrency applications with asynchronous I/O.
3
4
## Capabilities
5
6
### JettyReactiveWebServerFactory
7
8
Creates and configures Jetty reactive web server instances for Spring WebFlux applications.
9
10
```java { .api }
11
/**
12
* Factory for creating reactive Jetty web servers for WebFlux applications
13
*/
14
public class JettyReactiveWebServerFactory
15
extends AbstractReactiveWebServerFactory
16
implements ConfigurableJettyWebServerFactory {
17
18
/** Create factory with default configuration */
19
public JettyReactiveWebServerFactory();
20
21
/** Create factory with specified port */
22
public JettyReactiveWebServerFactory(int port);
23
24
/** Create reactive web server with HTTP handler */
25
public WebServer getWebServer(HttpHandler httpHandler);
26
27
/** Add server customizers for additional configuration */
28
public void addServerCustomizers(JettyServerCustomizer... customizers);
29
30
/** Get collection of configured server customizers */
31
public Collection<JettyServerCustomizer> getServerCustomizers();
32
33
/** Set number of acceptor threads */
34
public void setAcceptors(int acceptors);
35
36
/** Set number of selector threads */
37
public void setSelectors(int selectors);
38
39
/** Configure forward headers handling */
40
public void setUseForwardHeaders(boolean useForwardHeaders);
41
42
/** Set resource factory for HTTP client resources */
43
public void setResourceFactory(JettyResourceFactory resourceFactory);
44
45
/** Get the configured resource factory */
46
public JettyResourceFactory getResourceFactory();
47
48
/** Set maximum number of connections */
49
public void setMaxConnections(int maxConnections);
50
51
/** Set thread pool configuration */
52
public void setThreadPool(ThreadPool threadPool);
53
54
/** Get the configured thread pool */
55
public ThreadPool getThreadPool();
56
57
/** Set collection of server customizers */
58
public void setServerCustomizers(Collection<? extends JettyServerCustomizer> customizers);
59
}
60
```
61
62
**Usage Examples:**
63
64
```java
65
@Configuration
66
public class JettyReactiveConfig {
67
68
@Bean
69
public JettyReactiveWebServerFactory jettyReactiveWebServerFactory() {
70
JettyReactiveWebServerFactory factory = new JettyReactiveWebServerFactory(8080);
71
72
// Configure for high concurrency
73
factory.setAcceptors(1);
74
factory.setSelectors(8);
75
76
// Add custom server configuration
77
factory.addServerCustomizers(server -> {
78
// Configure HTTP/2 support
79
HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(
80
new HttpConfiguration());
81
82
ServerConnector connector = new ServerConnector(server, h2);
83
connector.setPort(8080);
84
server.addConnector(connector);
85
});
86
87
return factory;
88
}
89
}
90
```
91
92
### Resource Management
93
94
Jetty resource factory for managing HTTP client and server resources efficiently.
95
96
```java { .api }
97
/**
98
* Factory for managing Jetty HTTP client and server resources
99
*/
100
public class JettyResourceFactory implements DisposableBean, InitializingBean {
101
102
/** Create resource factory with default configuration */
103
public JettyResourceFactory();
104
105
/** Get the HTTP client instance */
106
public HttpClient getHttpClient();
107
108
/** Get the executor for async operations */
109
public Executor getExecutor();
110
111
/** Get the scheduler for timed operations */
112
public Scheduler getScheduler();
113
114
/** Get the byte buffer pool */
115
public ByteBufferPool getByteBufferPool();
116
117
/** Set custom thread pool */
118
public void setThreadPool(ThreadPool threadPool);
119
120
/** Set custom executor */
121
public void setExecutor(Executor executor);
122
123
/** Set custom scheduler */
124
public void setScheduler(Scheduler scheduler);
125
126
/** Set custom byte buffer pool */
127
public void setByteBufferPool(ByteBufferPool byteBufferPool);
128
129
/** Lifecycle method - initialize resources */
130
public void afterPropertiesSet() throws Exception;
131
132
/** Lifecycle method - destroy resources */
133
public void destroy() throws Exception;
134
}
135
```
136
137
### HTTP/2 Support
138
139
HTTP/2 protocol support is configured through server customizers and Jetty's connection factories.
140
141
**HTTP/2 Configuration Example:**
142
143
```java
144
@Configuration
145
public class Http2Config {
146
147
@Bean
148
public JettyReactiveWebServerFactory jettyFactory() {
149
JettyReactiveWebServerFactory factory = new JettyReactiveWebServerFactory();
150
151
factory.addServerCustomizers(server -> {
152
HttpConfiguration httpConfig = new HttpConfiguration();
153
httpConfig.setSendServerVersion(false);
154
httpConfig.setSendXPoweredBy(false);
155
156
// HTTP/1.1 configuration
157
HttpConnectionFactory http1 = new HttpConnectionFactory(httpConfig);
158
159
// HTTP/2 configuration
160
HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(httpConfig);
161
http2.setMaxConcurrentStreams(100);
162
http2.setInitialStreamRecvWindow(65536);
163
164
// ALPN configuration for protocol negotiation
165
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
166
alpn.setDefaultProtocol("http/1.1");
167
168
ServerConnector connector = new ServerConnector(server, alpn, http2, http1);
169
connector.setPort(8080);
170
server.addConnector(connector);
171
});
172
173
return factory;
174
}
175
}
176
```
177
178
### Reactive Handler Integration
179
180
Integration with Spring WebFlux HttpHandler for reactive request processing.
181
182
```java { .api }
183
/**
184
* Jetty adapter for Spring WebFlux HttpHandler
185
*/
186
public class JettyHttpHandlerAdapter {
187
188
/** Create adapter for HTTP handler */
189
public JettyHttpHandlerAdapter(HttpHandler httpHandler);
190
191
/** Handle reactive HTTP requests */
192
public void handle(Request request, Response response, Callback callback);
193
194
/** Configure request and response buffer sizes */
195
public void setRequestBufferSize(int requestBufferSize);
196
public void setResponseBufferSize(int responseBufferSize);
197
}
198
```
199
200
### Server Lifecycle Management
201
202
Lifecycle management for reactive Jetty servers.
203
204
```java { .api }
205
/**
206
* Lifecycle management for reactive Jetty web servers
207
*/
208
public class JettyWebServer implements WebServer {
209
210
/** Start the reactive server */
211
public void start() throws WebServerException;
212
213
/** Stop the server gracefully */
214
public void stop() throws WebServerException;
215
216
/** Get the server port */
217
public int getPort();
218
219
/** Get the underlying Jetty server */
220
public Server getServer();
221
222
/** Graceful shutdown with callback */
223
public void shutDownGracefully(GracefulShutdownCallback callback);
224
}
225
```
226
227
**Reactive Application Example:**
228
229
```java
230
@SpringBootApplication
231
public class ReactiveJettyApplication {
232
233
public static void main(String[] args) {
234
SpringApplication.run(ReactiveJettyApplication.class, args);
235
}
236
237
@Bean
238
public RouterFunction<ServerResponse> routes() {
239
return RouterFunctions.route()
240
.GET("/hello", request ->
241
ServerResponse.ok()
242
.contentType(MediaType.TEXT_PLAIN)
243
.body(Mono.just("Hello from Jetty!"), String.class))
244
.build();
245
}
246
}
247
```