java - Netty: Make use of a pooled buffer for writing -
i'm working on poc using netty , till far goes nice , managed quite functionality , running.
i have question reusing byte-buffer writing. in next illustration can see manually created bytebuffer-response, created every request , isn't needed. create utilize of 'buf'. i'm running bit in trial , error mode , have checked out examples. although case looks standard, have not been able figure out right way of making of pooled buffer.
public class operationhandler extends channelinboundhandleradapter { private bytebuf buf; @override public void handleradded(channelhandlercontext ctx) { buf = ctx.alloc().buffer(1024); // system.out.println("channel handler added"); } @override public void handlerremoved(channelhandlercontext ctx) { buf.release(); buf = null; } @override public void channelread(channelhandlercontext ctx, object msg) { seek { bytebuffer response = bytebuffer.allocate(1024); byte[] operation = (byte[]) msg; invoker.invoke(operation, response); response.flip(); ctx.write(unpooled.wrappedbuffer(response)); } { referencecountutil.release(msg); } } @override public void channelreadcomplete(channelhandlercontext ctx) { ctx.flush(); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) { // close connection when exception raised. cause.printstacktrace(); ctx.close(); } }
java netty
No comments:
Post a Comment