1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.http.helloworld;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelOption;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.MultiThreadIoEventLoopGroup;
23 import io.netty.channel.nio.NioIoHandler;
24 import io.netty.channel.socket.nio.NioServerSocketChannel;
25 import io.netty.example.util.ServerUtil;
26 import io.netty.handler.logging.LogLevel;
27 import io.netty.handler.logging.LoggingHandler;
28 import io.netty.handler.ssl.SslContext;
29
30
31
32
33
34 public final class HttpHelloWorldServer {
35
36 static final boolean SSL = System.getProperty("ssl") != null;
37 static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
38
39 public static void main(String[] args) throws Exception {
40
41 final SslContext sslCtx = ServerUtil.buildSslContext();
42
43
44 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
45 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
46 try {
47 ServerBootstrap b = new ServerBootstrap();
48 b.option(ChannelOption.SO_BACKLOG, 1024);
49 b.group(bossGroup, workerGroup)
50 .channel(NioServerSocketChannel.class)
51 .handler(new LoggingHandler(LogLevel.INFO))
52 .childHandler(new HttpHelloWorldServerInitializer(sslCtx));
53
54 Channel ch = b.bind(PORT).sync().channel();
55
56 System.err.println("Open your web browser and navigate to " +
57 (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
58
59 ch.closeFuture().sync();
60 } finally {
61 bossGroup.shutdownGracefully();
62 workerGroup.shutdownGracefully();
63 }
64 }
65 }