1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.uptime;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelInitializer;
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.SocketChannel;
25 import io.netty.channel.socket.nio.NioServerSocketChannel;
26 import io.netty.handler.logging.LogLevel;
27 import io.netty.handler.logging.LoggingHandler;
28
29
30
31
32
33 public final class UptimeServer {
34 private static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
35 private static final UptimeServerHandler handler = new UptimeServerHandler();
36
37 private UptimeServer() {
38 }
39
40 public static void main(String[] args) throws Exception {
41 EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
42 try {
43 ServerBootstrap b = new ServerBootstrap();
44 b.group(group)
45 .channel(NioServerSocketChannel.class)
46 .handler(new LoggingHandler(LogLevel.INFO))
47 .childHandler(new ChannelInitializer<SocketChannel>() {
48 @Override
49 public void initChannel(SocketChannel ch) {
50 ch.pipeline().addLast(handler);
51 }
52 });
53
54
55 ChannelFuture f = b.bind(PORT).sync();
56
57
58
59
60 f.channel().closeFuture().sync();
61 } finally {
62 group.shutdownGracefully();
63 }
64 }
65 }