1、netty3是nio的封装版本。在使用上面比nio的直接使用更好。nio简单使用都是单线程的方式(比如:一个服务员服务很多客户),但是netty3的方式不一样的是,引入线程池的方式来实现服务的通信(比如:不同的服务员服务不同的客户群体一样)。netty3将入口和实现分成两个线程池。入口:boss,实现:work。实现过程就是当一个客户端进入过后,boss线程池分配一个线程来接待客户,而通过boss来分配具体的服务人work来服务这位客户。

  2、netty3的使用是在nio的基础上加入线程池的概念进行实现的,后面我会单独讲一个netty3的源码实现过程。

  3、这里写了一点netty3的实现过程。

  1)服务端:server和serverHandler

import org.jboss.netty.bootstrap.ServerBootstrap;
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.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Server { public static void main(String[] args) { //声明服务类
ServerBootstrap serverBootstrap = new ServerBootstrap(); //设定线程池
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService work = Executors.newCachedThreadPool(); //设置工厂
serverBootstrap.setFactory(new NioServerSocketChannelFactory(boss,work)); //设置管道流
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline channelPipeline = Channels.pipeline();
//添加处理方式
channelPipeline.addLast("decode",new StringDecoder());
channelPipeline.addLast("encode",new StringEncoder());
channelPipeline.addLast("server",new ServerHandler());
return channelPipeline;
}
}); //设置端口
serverBootstrap.bind(new InetSocketAddress(9000));
}
}

  备注:具体的数据处理交给ServerHandler

import org.jboss.netty.channel.*;

public class ServerHandler extends SimpleChannelHandler {

    @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("client:"+e.getMessage());
ctx.getChannel().write(e.getMessage());
super.messageReceived(ctx, e);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
} @Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelConnected(ctx, e);
} @Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
} @Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
}
}

  2)客户端:client和clientHandler

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Client { public static void main(String[] args) { //声明客户端
ClientBootstrap clientBootstrap = new ClientBootstrap(); //设置线程池
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService work = Executors.newCachedThreadPool(); //设置线程池工厂
clientBootstrap.setFactory(new NioClientSocketChannelFactory(boss,work)); //设置管道工厂
clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline channelPipeline = Channels.pipeline();
channelPipeline.addLast("decode",new StringDecoder());
channelPipeline.addLast("encode",new StringEncoder());
channelPipeline.addLast("client",new ClientHandler());
return channelPipeline;
}
}); //连接服务器
ChannelFuture channelFuture = clientBootstrap.connect(new InetSocketAddress("localhost", 9000));
//获取通道
Channel channel = channelFuture.getChannel();
//写入数据
Scanner scanner = new Scanner(System.in);
while (true) {
channel.write(scanner.next());
}
}
}

  备注:处理的方式基本上和服务端一样

import org.jboss.netty.channel.*;

public class ClientHandler extends SimpleChannelHandler {

    @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("server:"+e.getMessage());
super.messageReceived(ctx, e);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
} @Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelConnected(ctx, e);
} @Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
} @Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
}
}

  4、这里讲的是netty3的应用过程,应用线程池来实现具体的工作。这样在效率和时间上面都会得到很大的改善

nio之netty3的应用的更多相关文章

  1. Netty5 + WebSocket 练习

    1. 了解WebSocket知识 略2. websocket实现系统简单反馈时间 WebSocketServerHandler.java package com.jieli.nettytest.web ...

  2. Netty3 源代码分析 - NIO server绑定过程分析

    Netty3 源代码分析 - NIO server绑定过程分析      一个框架封装的越好,越利于我们高速的coding.可是却掩盖了非常多的细节和原理.可是源代码可以揭示一切. 服务器端代码在指定 ...

  3. 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战

    概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...

  4. 【原创】NIO框架入门(三):iOS与MINA2、Netty4的跨平台UDP双向通信实战

    前言 本文将演示一个iOS客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo.服务端将分别用MINA2和Netty4进行实现,而通信时服务端你只需选其一就行了.同 ...

  5. 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示

    前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...

  6. 【原创】NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示

    申明:本文由作者基于日常实践整理,希望对初次接触MINA.Netty的人有所启发.如需与作者交流,见文签名,互相学习. 学习交流 更多学习资料:点此进入 推荐 移动端即时通讯交流: 215891622 ...

  7. 几种Java NIO框架的比较(zz)

    问题:生活中工作中,会有人问我javaNIO框架里面 Netty Mina  xSocket Grizzly 等等哪个比较好? 在这里写一下自己的感受,也算是总结一下吧 在我的印象中.不管是什么NIO ...

  8. NIO设置SO_LINGER引发的异常

    欢迎关注Github:https://github.com/teaey/ ### 背景 银时跟我讲,想从 Netty3迁移到Netty4 . 问其原因是由于 Netty3在容器里会报错,错误堆栈: j ...

  9. 7.3 netty3基本使用

    由于dubbo默认使用的是netty3进行通信的,这里简单的列出一个netty3通信的例子. 一 server端 1 Server package com.hulk.netty.server; imp ...

随机推荐

  1. phpcms利用表单向导创建留言板(可以回复)

    这篇博客写的很详细,可跳转到如下链接: http://blog.aiwebcom.com/%E7%BD%91%E7%AB%99%E5%BB%BA%E8%AE%BE/phpcms/456.html 注: ...

  2. MS12-020蓝屏攻击

    MS12-020远程桌面协议RDP拒绝访问漏洞 条件:受害者必须开放RDP协议 开放了3389端口 或者端口改了,知道对方RDP开放的对应端口. 过程:MSF利用 MSF显示为seems down说明 ...

  3. hadoop-2.2.0 HA配置

    采用的是4台真实机器: namenode:qzhong  node27 datanode:qzhong node27 node100 node101 操作系统环境:qzhong(Ubuntu-14.0 ...

  4. NodeJs——入门

    关于NPM: npm 是 nodejs 的包管理和分发工具.它可以让 javascript 开发者能够更加轻松的共享代码和共用代码片段,并且通过 npm 管理你分享的代码也很方便快捷和简单. 一 No ...

  5. 若是将Map作为Key,存入Redis,该如何操作?

    1.先封装HashMap Map<String,Object> map=new HashMap<String,Object>(); map.put("name&quo ...

  6. jQuery mouseove和mouseout事件不断触发

    关于锋利的jQuery第三章结尾提示图片效果(鼠标放在图片上会出现一个大图跟随鼠标移动)实现时mouseove和mouseout事件不断触发的问题 html <ul class="bo ...

  7. zabbix基本监控各指标简解

    监控项目及使用模板 监控http和https: Template App HTTP Service     Template App HTTPS Service 监控cpu,内存,网络等: Templ ...

  8. markdown常用命令(持续整理更新...)

    编写使用的工具 VS Code 拥有丰富插件支持的代码编辑器,当然也支持markdown MdEditor一款在线编辑markdown网站 1.标题 示例: # 一级标题 ## 二级标题 ### 三级 ...

  9. 基于CLGeocoder - 地理编码

    iOS中CoreLocatio框架中的CLGeocoder为我们提供了地理编码方法: 首先需要导入框架 #import <CoreLocation/CoreLocation.h> 地理编码 ...

  10. React 父子组件和非父子组件传值

      零.this.props     可以接收到 外界的传值 和 此组件标签内部自定义的方法       例:         <one vals={message} sendVal={this ...