DataChannelTcpWithNio.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package org.ts.ddcs.net;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelInitializer;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. import org.ts.ddcs.log.LogHelper;
  11. /**
  12. * TCP网络连接
  13. *
  14. * @author dylan
  15. */
  16. public class DataChannelTcpWithNio {
  17. private String channelKey;
  18. private String bindAgreeement;
  19. private boolean nioChannelRunning = false;
  20. /*
  21. 监听端口
  22. */
  23. private int port = 0;
  24. private EventLoopGroup bossGroup = null;
  25. private EventLoopGroup workerGroup = null;
  26. public DataChannelTcpWithNio(String key, String agreement) {
  27. this.channelKey = key;
  28. this.bindAgreeement = agreement;
  29. }
  30. public DataChannelTcpWithNio colseChannel() {
  31. this.nioChannelRunning = false;
  32. if (null != this.workerGroup) {
  33. this.workerGroup.shutdownGracefully();
  34. this.workerGroup = null;
  35. }
  36. if (null != this.bossGroup) {
  37. this.bossGroup.shutdownGracefully();
  38. this.bossGroup = null;
  39. }
  40. LogHelper.info("******************** 关闭 NIO ********************");
  41. return this;
  42. }
  43. public DataChannelTcpWithNio openChannel(int port) {
  44. this.nioChannelRunning = true;
  45. this.port = port;
  46. LogHelper.info("******************** 启动 NIO ********************");
  47. return this;
  48. }
  49. public void async() {
  50. if (!this.nioChannelRunning) {
  51. LogHelper.error("调用 Async() 发生错误:未打开通道");
  52. return;
  53. }
  54. new AcceptThread().start();
  55. }
  56. private class AcceptThread extends Thread {
  57. @Override
  58. public void run() {
  59. bossGroup = new NioEventLoopGroup();
  60. workerGroup = new NioEventLoopGroup();
  61. try {
  62. ServerBootstrap b = new ServerBootstrap();
  63. b.group(bossGroup, workerGroup)
  64. .channel(NioServerSocketChannel.class)
  65. .childHandler(new ChannelInitializer<SocketChannel>() {
  66. @Override
  67. public void initChannel(SocketChannel ch) throws Exception {
  68. ch.pipeline().addLast(new DataDiscardHandler(channelKey,bindAgreeement));
  69. }
  70. })
  71. .option(ChannelOption.SO_BACKLOG, 128)
  72. .childOption(ChannelOption.SO_KEEPALIVE, true);
  73. ChannelFuture f = b.bind(port).sync();
  74. f.channel().closeFuture().sync();
  75. } catch (InterruptedException e) {
  76. LogHelper.error(e);
  77. } finally {
  78. if (null != workerGroup) {
  79. workerGroup.shutdownGracefully();
  80. }
  81. if (null != bossGroup) {
  82. bossGroup.shutdownGracefully();
  83. }
  84. }
  85. }
  86. }
  87. }