什么是netty?以下是官方文档的简单介绍:
The Netty project  is an effort to provide
an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance and high scalability protocol servers and clients. In other words, Netty is
a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP/IP
socket server.

以下写一个简单的实例:
1.client细节分析

ChannelFactory是创建一个通道(和一次详细的通信实体关联如网络套接字)的主要接口。比方NioServerSocketChannelFactory 会创建一个Channel,有基于NIO的服务套接字作为底层的通信实体。一旦一个新的通道创建。那么相应的ChannelPipeline就会開始处理相关的ChannelEvents。
 

NioClientSocketChannelFactory会创建一个client的基于NIO的SocketChannel。利用非堵塞IO模型来高效处理这些并发的连接。当中有两种类型的线程, boss thread 和 worker thread,每一个NioClientSocketChannelFactory
有一个boss thread。它主要是有请求要发出时试图进行一次连接。连接成功后,将这个连接的通道交付给一个worker thread。接下来这个worker thread 为一个或多个通道运行非堵塞的读写服务。


ClientBootstrap仅仅是一个辅助函数,不会分配或者管理不论什么资源,管理资源是由构造器中指定的ChannelFactory完毕的。所以从同一个ChannelFactory衍生出多个ClientBootstrap是能够的。从而为不同的Channel应用不同的设置。connect()方法会依据指定的SocketAddress试图建立连接,假设本地地址没有设置,就会自己主动分配,等价于:
ClientBootstrap b = ....;
b.connect(remoteAddress, b.getOption("localAddress"));


静态方法 Channels.pipeline(ChannelHandler... handlers)用參数所指定的ChannelHandler 来创建一个新的ChannelPipeline,当然它们是有顺序的,我们也能够自己一个一个的加入。

 public static ChannelPipeline pipeline(ChannelHandler...
handlers) {
        if (handlers == null) {
            throw new NullPointerException( "handlers");
        }

        ChannelPipeline newPipeline = pipeline ();
        for (int i = 0; i < handlers. length;
i ++) {
            ChannelHandler h = handlers[i];
            if (h == null) {
                break;
            }
            newPipeline.addLast(ConversionUtil. toString(i), h);
        }
        return newPipeline;
    }

2. server端细节分析

服务器端构建的基本流程和client类似,仅仅是这里的ChannelFactory。Bootstrap 都要满足作为server的特性。

NioServerSocketChannelFactory创建server端的,基于NIO的ServerSocketChannel。仍然是非堵塞模式。

每一个绑定的ServerSocketChannel 有自身的boos thread,比方说打开监听了两个port 80,443。那么就会有两个boss thread,各自负责各自port的连接请求,直到那个port解绑定,然后将接受的连接请求交给worker
thread去处理。


这里是面向连接传输的ClientBootstrap 和  ServerBootstrap 。假设想用UDP的话就选 ConnectionlessBootstrap。

3. ChannelHandler的常见使用方法就会依据详细的事件类型做出详细的处理。牵扯到读写管道。并且有上下流的情况。


一个简单的netty样例:
TimeClientl.java
import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; public class TimeClient {
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]); ChannelFactory factory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new TimeClientHandler2());
}
});
bootstrap.connect(new InetSocketAddress(host, port)); //
} }

TimeClientHandler.java
import java.util.Date;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; public class TimeClientHandler extends SimpleChannelHandler{
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
ChannelBuffer buffer = (ChannelBuffer)e.getMessage();
long currentTimeMills = buffer.readInt() * 1000L;
System.out.println(new Date(currentTimeMills));
e.getChannel().close();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
Channel c = e.getChannel();
c.close();
}
}


TimeServer.java
import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.ChannelGroupFuture;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; public class TimeServer {
public static ChannelGroup allChannels = new DefaultChannelGroup("time-server"); public static void main(String[] args) {
ChannelFactory factory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new TimeServerHandler2(),
new TimeEncoder());
}
}); bootstrap.setOption("reuseAddr", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true); Channel channel = bootstrap.bind(new InetSocketAddress(8080)); allChannels.add(channel);
//waitForShutdownCommand(); this is a imaginary logic:for instance
//when there is accepted connection we close this server ;
if(allChannels.size() >=2){
ChannelGroupFuture f = allChannels.close();
f.awaitUninterruptibly();
factory.releaseExternalResources();
}
}
}


TimeServerHandler.java
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler; public class TimeServerHandler extends SimpleChannelHandler{
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
Channel ch = e.getChannel();
ChannelBuffer time = ChannelBuffers.buffer(4); //sizeof int
time.writeInt((int)(System.currentTimeMillis()/1000L + 2208988800L)); ChannelFuture cf = ch.write(time);
cf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
Channel ch = future.getChannel();
ch.close();
}
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
Channel c = e.getChannel();
c.close();
}
}

Netty入门实例及分析的更多相关文章

  1. 网络编程Netty入门:ByteBuf分析

    目录 Netty中的ByteBuf优势 NIO使用的ByteBuffer有哪些缺点 ByteBuf的优势和做了哪些增强 ByteBuf操作示例 ByteBuf操作 简单的Demo示例 堆内和堆外内存 ...

  2. netty入门实例

    TimeServer.java package netty.timeserver.server; import io.netty.bootstrap.ServerBootstrap; import i ...

  3. 网络编程Netty入门:EventLoopGroup分析

    目录 Netty线程模型 代码示例 NioEventLoopGroup初始化过程 NioEventLoopGroup启动过程 channel的初始化过程 Netty线程模型 Netty实现了React ...

  4. GEF入门实例_总结_04_Eclipse插件启动流程分析

    一.前言 本文承接上一节:GEF入门实例_总结_03_显示菜单和工具栏 注意到app目录下的6个类文件. 这6个文件对RCP应用程序而言非常重要,可能我们现在对这几个文件的理解还是云里雾里,这一节我们 ...

  5. (入门篇 NettyNIO开发指南)第三章-Netty入门应用

    作为Netty的第一个应用程序,我们依然以第2章的时间服务器为例进行开发,通过Netty版本的时间服务报的开发,让初学者尽快学到如何搭建Netty开发环境和!运行Netty应用程序. 如果你已经熟悉N ...

  6. Netty入门(三):EventLoop

    前言 Netty系列索引: 1.Netty入门(一):ByteBuf 2.Netty入门(二):Channel IO相关: 1.Java基础(一):I/O多路复用模型及Linux中的应用 上文提到,早 ...

  7. Netty入门(二):Channel

    前言 Netty系列索引: 1.Netty入门(一):ByteBuf 2.Netty入门(二):Channel 在Netty框架中,Channel是其中之一的核心概念,是Netty网络通信的主体,由它 ...

  8. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  9. jquery实战第一讲---概述及其入门实例

    就在5月28号周四下午五点的时候,接到xxx姐姐的电话,您是xxx吗?准备一下,周五上午八点半去远洋面试,一路风尘仆仆,颠颠簸簸,由于小编晕车,带着晕晕乎乎的脑子,小编就稀里糊涂的去面试了,温馨提醒, ...

随机推荐

  1. ASP .Net Core系统部署到SUSE Linux Enterprise Server 12 SP3 64 具体方案

    .Net Core 部署到 SUSE Linux Enterprise Server 12 SP3 64 位中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2. ...

  2. LeetCode(16):最接近的三数之和

    Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

  3. java判断部署项目使用的服务器类型

    有两种方式 1.项目引入portal-kernel.jar.项目运行时使用 根据返回boolean值判断类型! 2.自己写java类 package webService.ZFGX.service; ...

  4. AndroidManifest.xml中android:configChanges的简介

    程序在运行时,一些设备的配置可能会改变,如:横竖屏的切换.键盘的可用性等,这样的事情一发生,Activity会重新启动,其中的过程是:在销毁之前会先 called onSaveInstanceStat ...

  5. hdu 2519 求组合数

    求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...

  6. CDM中添加Hive服务时Gateway是什么?

    参考这里http://grokbase.com/t/cloudera/scm-users/12aayq5cyh/what-is-gateway-in-cloudera-manager 实际上Gatew ...

  7. 030 RDD Join中宽依赖与窄依赖的判断

    1.规律 如果JoinAPI之前被调用的RDD API是宽依赖(存在shuffle), 而且两个join的RDD的分区数量一致,join结果的rdd分区数量也一样,这个时候join api是窄依赖 除 ...

  8. 详解VirtualBox虚拟机网络环境解析和搭建-NAT、桥接、Host-Only、Internal、端口映射

    本文以VirtualBox为例 如果出现主机无法ping通虚拟机的情况,请首先确认虚拟机防火墙已关闭. 一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3. ...

  9. 002.Postfix空客户端配置

    一 空客户端介绍 空客户端必须满足(符合)以下条件: 本地发送的任何邮件都会转发到现有出站邮件中继以进行发送 本地postfix服务不接受任何电子邮件的本地发送 用户可以在空客户端运行邮件客户端来读取 ...

  10. Windows上Nginx的安装教程详解

    一 背景 为了方便本地的开发和验证,于是整理了这一篇Windows上安装Nginx的博文,建议一般学习还是使用Linux,一般正规公司都是在Linux上安装Nginx服务! 本篇内容相对比较简单,如果 ...