Netty connects to redis

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.