Our application runs on Spring Boot with embedded Netty server (enabled http2). And it exposes services via Rest APIs. On Rest API call, we have a scenario where we wanted to ignore the http2 request without sending any response.
Our Environment: Reactor version(s) used: reactor-netty-http : 1.0.16 JVM version (java -version): 17.0.1 springBootVersion= 2.7.4
Do we have any support from Netty Server to close a http2 stream(request) without response?
Here is my code:
GreetingController.java
public class GreetingController {
private final GreetingService greetingService;
public GreetingController(GreetingService greetingService) {
this.greetingService = greetingService;
}
@GetMapping("/{name}")
private Mono<String> greet(@PathVariable String name) {
return greetingService.greet(name);
}
}
DiscardOutBoundHandler.java
public class DiscardOutBoundHandler extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("Outbound Handler name: " + ctx.channel().pipeline().names());
promise.channel().close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
System.out.print("exceptionCaught call");
cause.printStackTrace();
ctx.close();
}
}
NettyWebServerFactoryPortCustomizer.java
public class NettyWebServerFactoryPortCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
@Override
public void customize(NettyReactiveWebServerFactory serverFactory) {
Http2 h2 = new Http2();
serverFactory.setHttp2(h2);
serverFactory.addServerCustomizers(new PortCustomizer(8443));
serverFactory.addServerCustomizers(
httpServer -> httpServer.doOnChannelInit((connectionObserver, channel, remoteAddress) -> {
ChannelPipeline channelPipeline = channel.pipeline();
channelPipeline.addLast("encoder", new DiscardOutBoundHandler());
}));
}
private static class PortCustomizer implements NettyServerCustomizer {
private final int port;
private PortCustomizer(int port) {
this.port = port;
}
@Override
public HttpServer apply(HttpServer httpServer) {
return httpServer.port(port);
}
}
}
Comment From: philwebb
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.