netty简单的操作memcached

2 篇文章 0 订阅

pom文件

 

 

<dependency>
    	<groupId>io.netty</groupId>
    	<artifactId>netty-example</artifactId>
    	<version>4.1.8.Final</version>
    	<exclusions>
    		<exclusion>
    			<artifactId>netty-tcnative</artifactId>
    			<groupId>io.netty</groupId>
    		</exclusion>
    	</exclusions>
    </dependency>
    <dependency>
    	<groupId>io.netty</groupId>
    	<artifactId>netty-tcnative</artifactId>
    	<version>1.1.33.Fork26</version>
    	<classifier>linux-x86_64-fedora</classifier>
    </dependency>
    <dependency>
    	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-pool2</artifactId>
    	<version>2.4.2</version>
    </dependency>
    <dependency>
    	<groupId>junit</groupId>
    	<artifactId>junit</artifactId>
    	<version>4.4</version>
    </dependency>

 

 


package com.snailteam.netty.memcached;

import java.util.concurrent.atomic.AtomicInteger;

import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheResponse;
import io.netty.util.CharsetUtil;
import junit.framework.Assert;

public class App {
	public static void main(String[] args) throws InterruptedException {
		NioSession session = new NioSession("127.0.0.1", 11211);
		AtomicInteger counter = new AtomicInteger(0);
		while (true) {
			FullBinaryMemcacheResponse res = null;
			String keyString = "user_id:" + counter.getAndIncrement();
			session.send(keyString, keyString);
			res = session.resp();
			session.get(keyString);
			res = session.resp();
			Assert.assertEquals(keyString, res.content().toString(CharsetUtil.UTF_8));
			if (counter.get() > 10000)
				break;
		}
		session.close();
	}
}

 

package com.snailteam.netty.memcached;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
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.BinaryMemcacheObjectAggregator;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheRequest;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
import io.netty.handler.codec.memcache.binary.DefaultFullBinaryMemcacheRequest;
import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheResponse;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.CharsetUtil;

public class NioSession {
	private MemcachedProtocol protocol;
	private Channel channel;
	private Bootstrap boot;

	public NioSession(String host, int port) throws InterruptedException {
		protocol = new MemcachedProtocol();
		boot = new Bootstrap().group(new NioEventLoopGroup()).channel(NioSocketChannel.class)
				.option(ChannelOption.TCP_NODELAY, true).remoteAddress(new InetSocketAddress(host, port))
				.handler(new ChannelInitializer<SocketChannel>() {

					@Override
					protected void initChannel(SocketChannel ch) throws Exception {
						ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG), new BinaryMemcacheClientCodec(),
								new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE), protocol);
					}

				});

		channel = boot.connect().sync().channel();
	}

	void send(String keyString, String value) throws InterruptedException {
		ByteBuf key = Unpooled.wrappedBuffer(keyString.getBytes(CharsetUtil.UTF_8));
		ByteBuf content = Unpooled.wrappedBuffer(value.getBytes(CharsetUtil.UTF_8));
		ByteBuf extras = channel.alloc().buffer(8);
		extras.writeZero(8);
		BinaryMemcacheRequest req = new DefaultFullBinaryMemcacheRequest(key, extras, content);
		req.setOpcode(BinaryMemcacheOpcodes.SET);
		channel.writeAndFlush(req);

	}

	public FullBinaryMemcacheResponse resp() throws InterruptedException {
		return protocol.resp();
	}

	public void get(String keyString) throws InterruptedException {
		ByteBuf key = Unpooled.wrappedBuffer(keyString.getBytes(CharsetUtil.UTF_8));
		BinaryMemcacheRequest req = new DefaultBinaryMemcacheRequest(key);
		req.setOpcode(BinaryMemcacheOpcodes.GET);
		channel.writeAndFlush(req);
	}

	public void close() {
		if (channel != null && channel.isActive()) {
			channel.close();
		}
		if( protocol != null ) {
			protocol.wakeUpAll() ;
			protocol = null ;
		}
		channel = null;
		if (boot != null && boot.config() != null) {
			boot.config().group().shutdownGracefully();
		}
		boot = null;
	}
}

 

package com.snailteam.netty.memcached;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheResponse;

public class MemcachedProtocol extends ChannelInboundHandlerAdapter {

	private BlockingQueue<FullBinaryMemcacheResponse> queue = new LinkedTransferQueue<FullBinaryMemcacheResponse>();

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		queue.add((FullBinaryMemcacheResponse) msg);
	}

	public FullBinaryMemcacheResponse resp() throws InterruptedException {
		return queue.take();
	}
	
	public void wakeUpAll() {
		while ( queue.poll() != null ) {
		}
		this.queue = null ;
	}
	
}

 

参考 https://github.com/fl061157/surf-memcached

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含最新版文档以及全部jar包: jar包如下 netty-buffer-4.1.32.Final-sources.jar netty-buffer-4.1.32.Final.jar netty-build-22-sources.jar netty-build-22.jar netty-codec-4.1.32.Final-sources.jar netty-codec-4.1.32.Final.jar netty-codec-http-4.1.32.Final-sources.jar netty-codec-http-4.1.32.Final.jar netty-codec-http2-4.1.32.Final-sources.jar netty-codec-http2-4.1.32.Final.jar netty-codec-memcache-4.1.32.Final-sources.jar netty-codec-memcache-4.1.32.Final.jar netty-codec-redis-4.1.32.Final-sources.jar netty-codec-redis-4.1.32.Final.jar netty-codec-socks-4.1.32.Final-sources.jar netty-codec-socks-4.1.32.Final.jar netty-codec-stomp-4.1.32.Final-sources.jar netty-codec-stomp-4.1.32.Final.jar netty-common-4.1.32.Final-sources.jar netty-common-4.1.32.Final.jar netty-example-4.1.32.Final-sources.jar netty-example-4.1.32.Final.jar netty-handler-4.1.32.Final-sources.jar netty-handler-4.1.32.Final.jar netty-handler-proxy-4.1.32.Final-sources.jar netty-handler-proxy-4.1.32.Final.jar netty-resolver-4.1.32.Final-sources.jar netty-resolver-4.1.32.Final.jar netty-tcnative-2.0.20.Final-osx-x86_64.jar netty-tcnative-2.0.20.Final-sources.jar netty-transport-4.1.32.Final-sources.jar netty-transport-4.1.32.Final.jar netty-transport-native-epoll-4.1.32.Final-linux-x86_64.jar netty-transport-native-epoll-4.1.32.Final-sources.jar netty-transport-native-kqueue-4.1.32.Final-osx-x86_64.jar netty-transport-native-kqueue-4.1.32.Final-sources.jar netty-transport-native-unix-common-4.1.32.Final-sources.jar netty-transport-native-unix-common-4.1.32.Final.jar netty-transport-rxtx-4.1.32.Final-sources.jar netty-transport-rxtx-4.1.32.Final.jar netty-transport-sctp-4.1.32.Final-sources.jar netty-transport-sctp-4.1.32.Final.jar netty-transport-udt-4.1.32.Final-sources.jar netty-transport-udt-4.1.32.Final.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值