Http://wangnow.com/article/22-redis-protocol-specification-by-php connection protocol see
package com.huang.test;import java.net.InetSocketAddress;import java.nio.charset.Charset;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ClientBootstrap;import org.jboss.netty.buffer.ChannelBuffer;import org.jboss.netty.buffer.ChannelBuffers;import org.jboss.netty.channel.Channel;import org.jboss.netty.channel.ChannelFactory;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.ExceptionEvent;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;public class ClientHandler extends SimpleChannelHandler{@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)throws Exception {// TODO Auto-generated method stube.getCause().printStackTrace();e.getChannel().close();}@Overridepublic void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)throws Exception {// TODO Auto-generated method stubChannel channel = ctx.getChannel();System.out.println("channel connected!");String cmd = "*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n";ChannelBuffer buffer = ChannelBuffers.buffer(cmd.length());buffer.writeBytes(cmd.getBytes());channel.write(buffer);String get = "*2\r\n$3\r\nget\r\n$5\r\nmykey\r\n";buffer = ChannelBuffers.buffer(get.length());buffer.writeBytes(get.getBytes());channel.write(buffer);}@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {// TODO Auto-generated method stubChannelBuffer buffer = (ChannelBuffer) e.getMessage();String res = buffer.toString(Charset.defaultCharset());System.out.println("result :" + res);}public static void main(String[] args){ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());ClientBootstrap boot = new ClientBootstrap(factory);boot.setPipelineFactory(new ChannelPipelineFactory(){@Overridepublic ChannelPipeline getPipeline() throws Exception {// TODO Auto-generated method stubreturn Channels.pipeline(new ClientHandler());}}); boot.connect(new InetSocketAddress("localhost", 6379));}}
Run in Ubuntu and the output is:
Channel connected!
Result: + OK
$7
Myvalue