我这边第一次连接netty可以,但是关闭netty连接以后,再次使用连接就会报这个错,其实很简单的问题,但是一下子蒙住了,搞了半天,后来发现是全局变量的问题,

我把下面这两行代码放入了全局变量里,重启系统第一次连接没问题,关闭连接第二次开启连接就出现报错 Force-closing a channel whose registration task was not accepted by an event loop 连接不上。

 EventLoopGroup parentGroup = new NioEventLoopGroup();//负责网络的读写EventLoopGroup childGroup = new NioEventLoopGroup();

因为我还得需要通过接口关闭连接,针对上面问题我是这样改的

定义一个安全线程的hashmap用来存储EventLoopGroup 对象,在关闭资源方法里通过map取出来进行关闭,就不会出现这个问题了

@Component
public class NettyServer {private static Logger logger = LoggerFactory.getLogger(NettyServer.class);private ConcurrentHashMap mapEventLoopGroup = new ConcurrentHashMap();private Channel channel;// 开启连接public ChannelFuture bing(InetSocketAddress address) {// 配置服务端NIO线程组-负责接收客户端的连接EventLoopGroup parentGroup = new NioEventLoopGroup();//负责网络的读写EventLoopGroup childGroup = new NioEventLoopGroup();ServerBootstrap b = new ServerBootstrap();ChannelFuture channelFuture = null;System.out.println("进入bind方法");try {// 添加boss和worker组b.group(parentGroup, childGroup)// 非阻塞模式.channel(NioServerSocketChannel.class)// 设置TCP的缓冲区.option(ChannelOption.SO_BACKLOG, 128)// 增加接收缓冲区.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(4096))// childHandler 去绑定具体的事件处理器.childHandler(new ChannelInit());// 绑定端口channelFuture = b.bind(address).syncUninterruptibly();System.out.println("绑定端口");System.out.println(channelFuture);channel = channelFuture.channel();System.out.println(channel);mapEventLoopGroup.put("parent", parentGroup);mapEventLoopGroup.put("child", childGroup);} catch (Exception e) {// 优雅退出,释放线程池资源System.out.println("进入catch方法");e.printStackTrace();parentGroup.shutdownGracefully();childGroup.shutdownGracefully();} finally {if (null != channelFuture && channelFuture.isSuccess()) {logger.info("netty服务启动完成!");} else {logger.error("netty服务启动失败!");}}return channelFuture;}// 关闭连接public boolean destroy() {try {if (null == channel) {return false;} else {channel.close();channel = null;System.out.println("关闭channel");}if (mapEventLoopGroup.containsKey("parent")) {EventLoopGroup parentGroup = (EventLoopGroup) mapEventLoopGroup.get("parent");parentGroup.shutdownGracefully();System.out.println("关闭parent");mapEventLoopGroup.remove("parent");}if (mapEventLoopGroup.containsKey("child")) {EventLoopGroup child = (EventLoopGroup) mapEventLoopGroup.get("child");child.shutdownGracefully();System.out.println("关闭child");mapEventLoopGroup.remove("child");}return true;} catch (Exception e) {e.printStackTrace();return false;}}public Channel getChannel() {return channel;}/** @Author df* @Description 发送消息* @Date 15:28 2021/1/14**/public AjaxResult sendMessage(byte[] byteData, String orderNo) {System.out.println("进入发送message方法(),命令号是:" + orderNo);System.out.println(ServerHandler.ctxMaps.size());try {// 指定不同的连接请求发送命令for (Map<String, ChannelHandlerContext> ctxMap : ServerHandler.ctxMaps) {if (ctxMap.containsKey(orderNo)) {ChannelHandlerContext ctx = ctxMap.get(orderNo);if (ctx == null) {return AjaxResult.error("请确保打开TCP连接操作!");}// netty需要转换ByteBuf才可使用ByteBuf bytebuf = Unpooled.buffer();bytebuf.writeBytes(byteData);ServerHandler.isAccess = true;ctx.writeAndFlush(bytebuf);String ip = IpCabinetBind.findOrderNoByIp(orderNo);String msg = "给Ip为:" + ip + "发送了命令号是" + orderNo + "指令";System.out.println(msg);Session session = WebSocketServer.sessionPools.get("android");if (session != null) {boolean isSucc = WebSocketServer.sendMessage(session, msg);}}}} catch (Exception e) {e.printStackTrace();ServerHandler.isAccess = false;return AjaxResult.error("服务器报错!发送指令失败!");}return null;}
}

netty Force-closing a channel whose registration task was not accepted by an event loop问题相关推荐

  1. Netty是如何把Channel 从Boss线程传到Work线程的?

    在 ServerBootstrapAcceptor#channelRead 方法中调用childGroup 将Channel 注册到work 线程中的. 一. 处理流程 io.netty.channe ...

  2. python3 异步错误 asyncio.Semaphore RuntimeError: Task got Future attached to a different loop

    错误现象 asyncio.Semaphore RuntimeError: Task got Future attached to a different loop asyncio.Semaphore ...

  3. Netty入门——组件(Channel)一

    目录 一.channel的主要作用 二.EventLoop处理io任务代码示例 2.1.服务端代码示例 2.2.客户端代码示例 2.3.服务端和客户端查看控制台输出结果 三.ChannelFuture ...

  4. 【Netty】NIO 通道 ( Channel ) 组件

    文章目录 I . 通道 ( Channel ) 概念简介 II . 通道 ( Channel ) 常用类 III . 常用的 通道 ( Channel ) 之 套接字通道 ( SocketChanne ...

  5. Netty学习总结(4)——图解Netty之Pipeline、channel、Context之间的数据流向

    以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件) 当用户调用channel的connect时,会发起一个outbound类型的事件,该事件将在pipel ...

  6. 深入理解netty(二)Channel

    上一篇文章介绍过channel,channel里面主要是封装了对于客户端连接的建立等的一些api,以及socket等,对于封装的api可以进行一些读写 先留两个问题: 1.服务端的socket在哪里初 ...

  7. netty源码分析系列——Channel

    2019独角兽企业重金招聘Python工程师标准>>> 前言 Channel是netty中作为核心的一个概念,我们从启动器(Bootstrap)中了解到最终启动器的两个关键操作con ...

  8. Netty Channel源码分析

    原文:https://wangwei.one/posts/netty-channel-source-analyse.html 前面,我们大致了解了Netty中的几个核心组件.今天我们就来先来介绍Net ...

  9. netty源码解解析(4.0)-3 Channel的抽象实现

    AbstractChannel和AbstractUnsafe抽象类 io.netty.channel.AbstractChannel 从本章开始,会有大量的篇幅涉及到代码分析.为了能够清晰简洁的地说明 ...

  10. netty 5 alph1源码分析(服务端创建过程)

    研究了netty的服务端创建过程.至于netty的优势,可以参照网络其他文章.<Netty系列之Netty 服务端创建>是 李林锋撰写的netty源码分析的一篇好文,绝对是技术干货.但抛开 ...

最新文章

  1. SpringBoot--HelloWord
  2. 错误:can't create 事务 lock on /var/lib/rpm/.rpm
  3. O’Reilly发布“微服务成熟度状态”报告:微服务是成功的
  4. 一分钟搭建、运行、测试SSM项目
  5. day5-shutil模块
  6. [结构力学] 几何构造分析的技巧
  7. AlgorithmMan,一套免费的算法演示神器(开源动画演示版)
  8. iOS开发总结(A0)- Localization
  9. idea打开vue项目后报错ESLint: Expected space or tab after ‘//‘ in comment.(spaced-comment)
  10. Keras中的两种模型:Sequential和Model
  11. opencv4快速入门pdf_云复工提升工作效率之九 福昕PDF阅读器
  12. 如何给自己的电脑硬盘分区
  13. 聊一聊字节跳动的面试
  14. php 字符编码转换
  15. 为什么.class文件查看不了_恕我直言,这可能是你见过最详细的class文件结构分析
  16. 海康威视网络摄像头开发流程(七)-------- 激活海康威视网络摄像头
  17. FastReport.Net报表工具 vs RDL标准报表定义语言
  18. API获取天气数据方法——中国天气网数据API下载及处理
  19. 【文件格式探究】EP.1 对ePub文件格式的初探
  20. js 将小数转为科学记数法

热门文章

  1. 搜索 阿虚同学_凉宫春日阿虚台词“在虚构的故事当中寻求真实感的人脑袋一定有问题”动画是出自那一集?...
  2. 如何修改PDF文件的标题
  3. PPT中插入的图片如何铺满整页
  4. 【放牛娃】奶盘seo自媒体文章伪原创系统
  5. 产品经理从专能到全能——不再虚无缥缈的用户体验
  6. 0基础可不可以学大数据
  7. 2020-12-11静态路由汇总实验
  8. 采集利器 - Web Scraper教学及示例
  9. AddressBook iOS读取通讯录信息
  10. ibatis的isequal_ibatIS中的isNotNull、isEqual、isEmpty