netty串連redis

來源:互聯網
上載者:User

http://wangnow.com/article/22-redis-protocol-specification-by-php 連線協定請看

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));}}

ubuntu下運行,輸出為:

channel connected!
result :+OK
$7
myvalue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.