Skip to content

Instantly share code, notes, and snippets.

@daschl
Last active December 24, 2015 07:09
Show Gist options
  • Save daschl/6761750 to your computer and use it in GitHub Desktop.
Save daschl/6761750 to your computer and use it in GitHub Desktop.
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheClientCodec;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheRequestEncoder;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequestHeader;
import io.netty.handler.codec.memcache.binary.util.BinaryMemcacheOpcodes;
import io.netty.util.ReferenceCountUtil;
/**
* Created with IntelliJ IDEA.
* User: michael
* Date: 9/6/13
* Time: 11:06 AM
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String[] args) throws Exception {
Bootstrap bootstrap = new Bootstrap()
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("codec", new BinaryMemcacheClientCodec());
ch.pipeline().addLast("discard", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ReferenceCountUtil.release(msg);
}
});
}
})
.option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator())
.group(new NioEventLoopGroup());
Channel chan1 = bootstrap.connect("127.0.0.1", 11210).sync().channel();
Channel chan2 = bootstrap.connect("127.0.0.1", 11210).sync().channel();
Channel chan3 = bootstrap.connect("127.0.0.1", 11210).sync().channel();
DefaultBinaryMemcacheRequestHeader header = new DefaultBinaryMemcacheRequestHeader();
header.setOpcode(BinaryMemcacheOpcodes.GET);
DefaultBinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(header);
int batchSize = 10;
while(true) {
for(int i = 0; i < batchSize-1; i++) {
String key = "foo-" + (i % 10);
header.setKeyLength((short) key.length());
header.setTotalBodyLength((short) key.length());
request.setKey(key);
chan1.write(request);
chan2.write(request);
chan3.write(request);
}
chan1.writeAndFlush(request).sync();
chan2.writeAndFlush(request).sync();
chan3.writeAndFlush(request).sync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment