Skip to content

Commit

Permalink
#NettyDemo 增加资源释放代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JMCuixy committed Jul 9, 2018
1 parent 34a6c46 commit 9cbd951
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/java/org/netty/demo/echo/EchoServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

import java.nio.file.FileSystemNotFoundException;

Expand All @@ -14,7 +15,7 @@
* ChannelHandlerAdapter 提供了一些 channelInboundHandler 的默认实现
*/

@ChannelHandler.Sharable //标识一个Channel-Handler 可以被多个Channel安全的共享
@ChannelHandler.Sharable //表示可以被添加到多个 ChannelPipeline 中,标识一个Channel-Handler 可以被多个Channel安全的共享
public class EchoServerHandler extends ChannelHandlerAdapter {


Expand Down Expand Up @@ -74,4 +75,21 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}


/**
* 当请求通过Channel 将数据写到远程节点时被调用
* @param ctx
* @param msg
* @param promise
* @throws Exception
*/
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

ReferenceCountUtil.release(msg);
//ChannelPromise 是ChannelFuture的一个子类,设置成 true 通知 ChannelFutureListener 消息已经被处理了
//当一个 Promise 被完成之后,其对应的 Future 的值便不能再进行任何修改了
promise.setSuccess();
}

}

0 comments on commit 9cbd951

Please sign in to comment.