无法使用ChannelFutureListener.CLOSE关闭连接

时间:2017-08-24 07:50:41

标签: netty

我使用Netty-5.0.0.Alpha1.I想要使用 ChannelFutureListener.CLOSE 来关闭连接,但它不起作用。 我将IdleStateHandler添加到管道 .addLast(new IdleStateHandler(45,0,0,TimeUnit.SECONDS))

@Override    
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
   if (evt instanceof IdleStateEvent) {
      log.warn("closeing idle connection:{}", ctx.channel().attr("MAC_ADDRESS").get());
      //write an empty message to the client then close the channel
      ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
      }
   }

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    log.warn("channel closed");
   }

服务器每45秒触发一次IdleStateEvent,但从不调用channelInactive方法。

>2017-08-24 14:00:35,170 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:01:20,170 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:02:05,171 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:02:50,172 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:03:35,172 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:04:20,172 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:05:05,173 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:05:50,173 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:06:35,174 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:07:20,174 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:08:05,175 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:08:50,176 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:09:35,176 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:10:20,176 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7
>2017-08-24 14:11:05,176 WARN c.b.f.d.s.h.EventAndExceptionHandler [nioEventLoopGroup-3-3] closeing idle connection:******7925D7

但它在大多数时候运作良好,这种情况只发生过一次。

0 个答案:

没有答案