HelloWebServer.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package hello;
  2. import java.net.InetSocketAddress;
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.Channel;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.ServerChannel;
  8. import io.netty.channel.epoll.Epoll;
  9. import io.netty.channel.epoll.EpollChannelOption;
  10. import io.netty.channel.epoll.EpollEventLoopGroup;
  11. import io.netty.channel.epoll.EpollServerSocketChannel;
  12. import io.netty.channel.kqueue.KQueue;
  13. import io.netty.channel.kqueue.KQueueServerSocketChannel;
  14. import io.netty.channel.nio.NioEventLoopGroup;
  15. import io.netty.channel.socket.nio.NioServerSocketChannel;
  16. import io.netty.incubator.channel.uring.IOUring;
  17. import io.netty.incubator.channel.uring.IOUringChannelOption;
  18. import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
  19. import io.netty.incubator.channel.uring.IOUringServerSocketChannel;
  20. import io.netty.util.ResourceLeakDetector;
  21. import io.netty.util.ResourceLeakDetector.Level;
  22. public class HelloWebServer {
  23. static {
  24. ResourceLeakDetector.setLevel(Level.DISABLED);
  25. }
  26. private final int port;
  27. public HelloWebServer(int port) {
  28. this.port = port;
  29. }
  30. public void run() throws Exception {
  31. // Configure the server.
  32. if (IOUring.isAvailable()) {
  33. doRun(new IOUringEventLoopGroup(Runtime.getRuntime().availableProcessors()), IOUringServerSocketChannel.class, IoMultiplexer.IO_URING);
  34. } else
  35. if (Epoll.isAvailable()) {
  36. doRun(new EpollEventLoopGroup(), EpollServerSocketChannel.class, IoMultiplexer.EPOLL);
  37. } else if (KQueue.isAvailable()) {
  38. doRun(new EpollEventLoopGroup(), KQueueServerSocketChannel.class, IoMultiplexer.KQUEUE);
  39. } else {
  40. doRun(new NioEventLoopGroup(), NioServerSocketChannel.class, IoMultiplexer.JDK);
  41. }
  42. }
  43. private void doRun(EventLoopGroup loupGroup, Class<? extends ServerChannel> serverChannelClass, IoMultiplexer multiplexer) throws InterruptedException {
  44. try {
  45. InetSocketAddress inet = new InetSocketAddress(port);
  46. System.out.printf("Using %s IoMultiplexer%n", multiplexer);
  47. ServerBootstrap b = new ServerBootstrap();
  48. if (multiplexer == IoMultiplexer.EPOLL) {
  49. b.option(EpollChannelOption.SO_REUSEPORT, true);
  50. }
  51. if (multiplexer == IoMultiplexer.IO_URING) {
  52. b.option(IOUringChannelOption.SO_REUSEPORT, true);
  53. }
  54. b.option(ChannelOption.SO_BACKLOG, 8192);
  55. b.option(ChannelOption.SO_REUSEADDR, true);
  56. b.group(loupGroup).channel(serverChannelClass).childHandler(new HelloServerInitializer(loupGroup.next()));
  57. b.childOption(ChannelOption.SO_REUSEADDR, true);
  58. Channel ch = b.bind(inet).sync().channel();
  59. System.out.printf("Httpd started. Listening on: %s%n", inet.toString());
  60. ch.closeFuture().sync();
  61. } finally {
  62. loupGroup.shutdownGracefully().sync();
  63. }
  64. }
  65. public static void main(String[] args) throws Exception {
  66. int port;
  67. if (args.length > 0) {
  68. port = Integer.parseInt(args[0]);
  69. } else {
  70. port = 8080;
  71. }
  72. new HelloWebServer(port).run();
  73. }
  74. }