Skip to content

Instantly share code, notes, and snippets.

@normanmaurer
Last active August 29, 2015 14:07
Show Gist options
  • Save normanmaurer/f0b6086a6002891a43a9 to your computer and use it in GitHub Desktop.
Save normanmaurer/f0b6086a6002891a43a9 to your computer and use it in GitHub Desktop.
package io.netty.example.http.snoop;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
public class HttpResponseDelayHandler extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, final ChannelPromise promise) {
ByteBuf buf = (ByteBuf) msg;
int totalSize = buf.readableBytes();
int slice1 = totalSize/2;
int slice2 = totalSize - slice1;
ByteBuf out = buf.readSlice(slice1).retain();
final ByteBuf out2 = buf.readSlice(slice2).retain();
ctx.write(out);
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
ctx.writeAndFlush(out2, promise);
}
}, 3, TimeUnit.SECONDS);
buf.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment