Java的学习是从Hello word开始的,Netty也从这里开始吧。这里的例子比较简单,后面会慢慢的对Netty的一些复杂应用、Netty的原理进行一些解析。本文主要是列举Netty初步使用的一个最基本的例子,具体的说明在代码中都有了,所以也不再重复。
1、ClientThread.java
- package HelloWord;
- import static org.jboss.netty.channel.Channels.pipeline;
- import java.net.InetSocketAddress;
- import java.util.concurrent.Executors;
- import org.jboss.netty.bootstrap.ClientBootstrap;
- import org.jboss.netty.channel.ChannelFuture;
- import org.jboss.netty.channel.ChannelPipeline;
- import org.jboss.netty.channel.ChannelPipelineFactory;
- import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
- import org.jboss.netty.handler.codec.string.StringDecoder;
- import org.jboss.netty.handler.codec.string.StringEncoder;
- /**
- * 启动一个client线程,用来间歇性的发送消息
- * @author Ransom
- */
- public class ClientThread implements Runnable
- {
- private ChannelFuture future;
- public ChannelFuture getFuture()
- {
- return future;
- }
- public void setFuture(ChannelFuture future)
- {
- this.future = future;
- }
- @Override
- public void run()
- {
- /*
- * 实例化一个客户端Bootstrap实例,
- * NioClientSocketChannelFactory是Netty默认提供的。
- * 两个参数,一个是boss的线程池,一个是worker执行的线程池。
- * 两个线程池都使用了java.util.concurrent.Executors中的线程池来创建。
- */
- ClientBootstrap bootstrap = new ClientBootstrap(
- new NioClientSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool()));
- /*
- * 设置piplineFactory,
- * 顾名思义,就是产生默认的pipline。
- * pipline的实例是DefaultChannelPipeline
- * 提供了链式的事件通讯机制
- */
- bootstrap.setPipelineFactory(new ChannelPipelineFactory(){
- /*
- * (non-Javadoc)
- * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
- */
- @Override
- public ChannelPipeline getPipeline() throws Exception
- {
- /*
- * 在DefaultChannelPipeline的过滤器 链中实现了
- * encode 、decode、handler
- * 其中encode实现自ChannelDownstreamHandler接口
- * decode、Handler实现自ChannelUpstreamHandler接口
- * 也就说明了在client发送消息的时候,默认按照顺序会先调用decode
- * 在client接收到响应的时候,会按照顺序调用encode和Handler。
- * 后面会有文章专门将ChannelDownstreamHandler和ChannelUpstreamHandler的调用顺序。
- */
- ChannelPipeline pipleline = pipeline();
- pipleline.addLast("encode", new StringEncoder());
- pipleline.addLast("decode", new StringDecoder());
- pipleline.addLast("handler", new Handler());
- return pipleline;
- }
- });
- /*
- * 与127.0.0.1建立长连接。
- */
- future = bootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));
- }
- /**
- * 发送消息至server
- */
- public void sendMsg()
- {
- if(future==null) return;
- String s = "Hello Word!";
- future.getChannel().write(s);
- }
- }
2、Handler.java
- package HelloWord;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.channel.ExceptionEvent;
- import org.jboss.netty.channel.MessageEvent;
- import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
- /**
- * client和server接收消息共用的handler
- * 由于两个都是继承自SimpleChannelUpstreamHandler,所以就写在一起了。
- * @author Ransom
- *
- */
- public class Handler extends SimpleChannelUpstreamHandler
- {
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
- throws Exception
- {
- System.out.println("recive message,message content:"+e.getMessage());
- }
- public void exceptionCaught(
- ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
- System.err.println("Client has a error,Error cause:"+e.getCause());
- e.getChannel().close();
- }
- }
3、Server.java
- package HelloWord;
- import static org.jboss.netty.channel.Channels.pipeline;
- import java.net.InetSocketAddress;
- import java.util.concurrent.Executors;
- import org.jboss.netty.bootstrap.ServerBootstrap;
- import org.jboss.netty.channel.ChannelPipeline;
- import org.jboss.netty.channel.ChannelPipelineFactory;
- import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
- import org.jboss.netty.handler.codec.string.StringDecoder;
- import org.jboss.netty.handler.codec.string.StringEncoder;
- /**
- * 在本地8080端口启动netty服务
- * @author Ransom
- *
- */
- public class Server
- {
- public static void main(String[] args)
- {
- /*
- * server的注释和client类似,在这里就不重复了
- * 但是需要注意的是server初始化的是ServerBootstrap的实例
- * client初始化的是ClientBootstrap,两个是不一样的。
- * 里面的channelfactory也是NioServerSocketChannelFactory。
- */
- ServerBootstrap bootstrap = new ServerBootstrap(
- new NioServerSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool()));
- bootstrap.setPipelineFactory(new ChannelPipelineFactory()
- {
- @Override
- public ChannelPipeline getPipeline() throws Exception
- {
- ChannelPipeline pipleline = pipeline();
- pipleline.addLast("encode", new StringEncoder());
- pipleline.addLast("decode", new StringDecoder());
- pipleline.addLast("handler", new Handler());
- return pipleline;
- }
- });
- bootstrap.bind(new InetSocketAddress(8080));
- }
- }
4、HelloWord.java
- package HelloWord;
- /**
- * Netty 初步之hello word的client入口
- * @author Ransom
- *
- */
- public class HelloWord
- {
- public static void main(String[] args)
- {
- ClientThread r = new ClientThread();
- Thread t = new Thread(r);
- t.setName("client thread");
- t.start();
- while(true)
- {
- try
- {
- Thread.sleep(3000);
- } catch (InterruptedException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- r.sendMsg();
- }
- }
- }