标签:ges str end 一个 res value pac tee hand
由于dubbo默认使用的是netty3进行通信的,这里简单的列出一个netty3通信的例子。
一 server端
1 Server
1 package com.hulk.netty.server; 2 3 import org.jboss.netty.bootstrap.ServerBootstrap; 4 import org.jboss.netty.channel.*; 5 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; 6 import org.jboss.netty.handler.codec.string.StringDecoder; 7 import org.jboss.netty.handler.codec.string.StringEncoder; 8 9 import java.net.InetSocketAddress; 10 import java.util.concurrent.Executors; 11 12 public class Server { 13 public void start(){ 14 ChannelFactory factory = new NioServerSocketChannelFactory( 15 Executors.newCachedThreadPool(),//boss线程池 16 Executors.newCachedThreadPool(),//worker线程池 17 8//worker线程数 18 ); 19 ServerBootstrap bootstrap = new ServerBootstrap(factory); 20 /** 21 * 对于每一个连接channel, server都会调用PipelineFactory为该连接创建一个ChannelPipline 22 */ 23 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { 24 public ChannelPipeline getPipeline() throws Exception { 25 ChannelPipeline pipeline = Channels.pipeline(); 26 pipeline.addLast("decoder", new StringDecoder()); 27 pipeline.addLast("encoder", new StringEncoder()); 28 pipeline.addLast("handler", new ServerLogicHandler()); 29 return pipeline; 30 } 31 }); 32 33 Channel channel = bootstrap.bind(new InetSocketAddress("127.0.0.1", 8080)); 34 System.out.println("server start success!"); 35 } 36 37 public static void main(String[] args) throws InterruptedException { 38 Server server = new Server(); 39 server.start(); 40 Thread.sleep(Integer.MAX_VALUE); 41 } 42 }
步骤:
2 事件处理器ServerLogicHandler
1 package com.hulk.netty.server; 2 3 import org.jboss.netty.channel.Channel; 4 import org.jboss.netty.channel.ChannelHandlerContext; 5 import org.jboss.netty.channel.ChannelStateEvent; 6 import org.jboss.netty.channel.ExceptionEvent; 7 import org.jboss.netty.channel.MessageEvent; 8 import org.jboss.netty.channel.SimpleChannelHandler; 9 10 public class ServerLogicHandler extends SimpleChannelHandler { 11 @Override 12 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { 13 System.out.println("连接成功, channel: " + e.getChannel().toString()); 14 } 15 16 @Override 17 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { 18 String msg = (String) e.getMessage(); 19 System.out.println("接收到了client的消息, msg: " + msg); 20 21 Channel channel = e.getChannel(); 22 String str = "hi, client"; 23 24 channel.write(str);//写消息发给client端 25 System.out.println("服务端发送数据: " + str + "完成"); 26 } 27 28 @Override 29 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { 30 e.getCause().printStackTrace(); 31 e.getChannel().close(); 32 } 33 }
说明:
二 client端
1 Client
1 package com.hulk.netty.client; 2 3 import org.jboss.netty.bootstrap.ClientBootstrap; 4 import org.jboss.netty.channel.ChannelFactory; 5 import org.jboss.netty.channel.ChannelPipeline; 6 import org.jboss.netty.channel.ChannelPipelineFactory; 7 import org.jboss.netty.channel.Channels; 8 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; 9 import org.jboss.netty.handler.codec.string.StringDecoder; 10 import org.jboss.netty.handler.codec.string.StringEncoder; 11 12 import java.net.InetSocketAddress; 13 import java.util.concurrent.Executors; 14 15 public class Client { 16 public static void main(String[] args) { 17 ChannelFactory factory = new NioClientSocketChannelFactory( 18 Executors.newCachedThreadPool(), 19 Executors.newCachedThreadPool(), 20 8 21 ); 22 ClientBootstrap bootstrap = new ClientBootstrap(factory); 23 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { 24 public ChannelPipeline getPipeline() throws Exception { 25 ChannelPipeline pipeline = Channels.pipeline(); 26 pipeline.addLast("decoder", new StringDecoder()); 27 pipeline.addLast("encoder", new StringEncoder()); 28 pipeline.addLast("handler", new ClientLogicHandler()); 29 return pipeline; 30 } 31 }); 32 33 bootstrap.connect(new InetSocketAddress("127.0.0.1", 8080)); 34 System.out.println("client start success!"); 35 } 36 }
步骤:(与Server几乎相同)
2 ClientLogicHandler
1 package com.hulk.netty.client; 2 3 import org.jboss.netty.channel.ChannelHandlerContext; 4 import org.jboss.netty.channel.ChannelStateEvent; 5 import org.jboss.netty.channel.ExceptionEvent; 6 import org.jboss.netty.channel.MessageEvent; 7 import org.jboss.netty.channel.SimpleChannelHandler; 8 import org.jboss.netty.channel.WriteCompletionEvent; 9 10 public class ClientLogicHandler extends SimpleChannelHandler { 11 @Override 12 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { 13 System.out.println("客户端连接成功!"); 14 String str = "hi server!"; 15 e.getChannel().write(str);//异步 16 } 17 18 @Override 19 public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception { 20 System.out.println("客户端写消息完成"); 21 } 22 23 @Override 24 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { 25 String msg = (String) e.getMessage(); 26 System.out.println("客户端接收到消息, msg: " + msg); 27 } 28 29 @Override 30 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { 31 e.getCause().printStackTrace(); 32 e.getChannel().close(); 33 } 34 }
说明:
这就是一个简单的netty3通信的例子,关于netty,后续会读源码。
标签:ges str end 一个 res value pac tee hand
原文地址:http://www.cnblogs.com/java-zhao/p/7625557.html