|
8 | 8 | import io.netty.channel.nio.NioEventLoopGroup;
|
9 | 9 | import io.netty.channel.socket.SocketChannel;
|
10 | 10 | import io.netty.channel.socket.nio.NioSocketChannel;
|
| 11 | +import io.netty.handler.codec.serialization.ObjectEncoder; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.context.ApplicationContext; |
| 14 | +import org.springframework.stereotype.Component; |
11 | 15 |
|
12 | 16 | /**
|
13 | 17 | * Created by ocean on 11/23/15.
|
14 | 18 | */
|
| 19 | +@Component |
15 | 20 | public class SocketClient {
|
16 |
| - public static void main(String[] args) throws Exception { |
17 |
| - String host = "127.0.0.1"; |
18 |
| - int port = 8080; |
19 |
| - EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| 21 | + private final String host = "127.0.0.1"; |
| 22 | + private final int port = 8080; |
| 23 | + private Bootstrap b; |
| 24 | + private NioEventLoopGroup workerGroup; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + ApplicationContext context; |
| 28 | + |
| 29 | + public void run() throws Exception { |
| 30 | + initialize(); |
20 | 31 |
|
21 | 32 | try {
|
22 |
| - Bootstrap b = new Bootstrap(); // (1) |
23 |
| - b.group(workerGroup); // (2) |
24 |
| - b.channel(NioSocketChannel.class); // (3) |
25 |
| - b.option(ChannelOption.SO_KEEPALIVE, true); // (4) |
26 |
| - b.handler(new ChannelInitializer<SocketChannel>() { |
27 |
| - @Override |
28 |
| - public void initChannel(SocketChannel ch) throws Exception { |
29 |
| - ch.pipeline().addLast(new SocketDecoder(), new SocketClientHandler()); |
30 |
| - } |
31 |
| - }); |
32 |
| - |
33 |
| - // Start the client. |
34 |
| - ChannelFuture f = b.connect(host, port).sync(); // (5) |
35 |
| - |
36 |
| - // Wait until the connection is closed. |
37 |
| - f.channel().closeFuture().sync(); |
| 33 | + configureAndStartServer(); |
38 | 34 | } finally {
|
39 |
| - workerGroup.shutdownGracefully(); |
| 35 | + shutdownClient(workerGroup); |
40 | 36 | }
|
41 | 37 | }
|
| 38 | + |
| 39 | + private void configureAndStartServer() throws InterruptedException { |
| 40 | + configureServer(); |
| 41 | + startServer(); |
| 42 | + } |
| 43 | + |
| 44 | + private void startServer() throws InterruptedException { |
| 45 | + // Start the client. |
| 46 | + ChannelFuture f = b.connect(host, port).sync(); // (5) |
| 47 | + |
| 48 | + // Wait until the connection is closed. |
| 49 | + f.channel().closeFuture().sync(); |
| 50 | + } |
| 51 | + |
| 52 | + private void configureServer() { |
| 53 | + b.group(workerGroup); // (2) |
| 54 | + b.channel(NioSocketChannel.class); // (3) |
| 55 | + b.option(ChannelOption.SO_KEEPALIVE, true); // (4) |
| 56 | + b.handler(new ChannelInitializer<SocketChannel>() { |
| 57 | + @Override |
| 58 | + public void initChannel(SocketChannel ch) throws Exception { |
| 59 | + ch.pipeline().addLast( |
| 60 | + new ObjectEncoder(), |
| 61 | + context.getBean(SocketClientHandler.class) |
| 62 | + ); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + private void initialize() { |
| 68 | + workerGroup = new NioEventLoopGroup(); |
| 69 | + b = new Bootstrap(); // (1) |
| 70 | + } |
| 71 | + |
| 72 | + private void shutdownClient(EventLoopGroup workerGroup) { |
| 73 | + workerGroup.shutdownGracefully(); |
| 74 | + } |
42 | 75 | }
|
0 commit comments