一、DEMO

官方并没有使用Hello World来作为一个例子,而是采用RFC的DISCARD,这个协议定义了就是接收到请求后什么也不干。

第一步编写DiscardServerHandler类:

package io.netty.example.discard;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
//ChannelInboundHandlerAdapter实现自ChannelInboundHandler
//ChannelInboundHandler提供了不同的事件处理方法你可以重写
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
/*
* @说明:该方法用于接收从客户端接收的信息
* @时间:2017-4-2下午12:25:05
* @see io.netty.channel.ChannelInboundHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//Discard the received data silently
//ByteBuf是一个引用计数对象实现ReferenceCounted,他就是在有对象引用的时候计数+1,无的时候计数-1,当为0对象释放内存
ByteBuf in=(ByteBuf)msg;
try {
while(in.isReadable()){
System.out.println((char)in.readByte());
System.out.flush();
}
} finally {
ReferenceCountUtil.release(msg);
}
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}

  

第二步编写DiscardServer:

package io.netty.example.discard;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class DiscardServer {
private int port;
public DiscardServer(int port){
this.port = port;
} public void run() throws Exception{
//Group:群组,Loop:循环,Event:事件,这几个东西联在一起,相比大家也大概明白它的用途了。
//Netty内部都是通过线程在处理各种数据,EventLoopGroup就是用来管理调度他们的,注册Channel,管理他们的生命周期。
//NioEventLoopGroup是一个处理I/O操作的多线程事件循环
//bossGroup作为boss,接收传入连接
//因为bossGroup仅接收客户端连接,不做复杂的逻辑处理,为了尽可能减少资源的占用,取值越小越好
EventLoopGroup bossGroup=new NioEventLoopGroup(1);
//workerGroup作为worker,处理boss接收的连接的流量和将接收的连接注册进入这个worker
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
//ServerBootstrap负责建立服务端
//你可以直接使用Channel去建立服务端,但是大多数情况下你无需做这种乏味的事情
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
//指定使用NioServerSocketChannel产生一个Channel用来接收连接
.channel(NioServerSocketChannel.class)
//ChannelInitializer用于配置一个新的Channel
//用于向你的Channel当中添加ChannelInboundHandler的实现
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
//ChannelPipeline用于存放管理ChannelHandel
//ChannelHandler用于处理请求响应的业务逻辑相关代码
ch.pipeline().addLast(new DiscardServerHandler());
};
})
//对Channel进行一些配置
//注意以下是socket的标准参数
//BACKLOG用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度。如果未设置或所设置的值小于1,Java将使用默认值50。
//Option是为了NioServerSocketChannel设置的,用来接收传入连接的
.option(ChannelOption.SO_BACKLOG, 128)
//是否启用心跳保活机制。在双方TCP套接字建立连接后(即都进入ESTABLISHED状态)并且在两个小时左右上层没有任何数据传输的情况下,这套机制才会被激活。
//childOption是用来给父级ServerChannel之下的Channels设置参数的
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
ChannelFuture f=b.bind(port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
//sync()会同步等待连接操作结果,用户线程将在此wait(),直到连接操作完成之后,线程被notify(),用户代码继续执行
//closeFuture()当Channel关闭时返回一个ChannelFuture,用于链路检测
f.channel().closeFuture().sync();
}finally{
//资源优雅释放
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
} public static void main(String[] args) {
int port=8088;
try {
new DiscardServer(port).run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

 

二、ECHO协议的DEMO

ECHO协议,定义了客户端请求啥就返回啥

第一步编写EchoServerHandler:

package io.netty.example.echo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; public class EchoServerHandler extends ChannelInboundHandlerAdapter {
/*
* @说明:该方法用于接收从客户端接收的信息
* @时间:2017-4-8下午12:08:51
* @see io.netty.channel.ChannelInboundHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//ChannelHandlerContext提供各种不同的操作用于触发不同的I/O时间和操作
//调用write方法来逐字返回接收到的信息
//这里我们不需要在DISCARD例子当中那样调用释放,因为Netty会在写的时候自动释放
//只调用write是不会释放的,它会缓存,直到调用flush
ctx.write(msg);
ctx.flush();
//你可以直接使用writeAndFlush(msg)
//ctx.writeAndFlush(msg);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}

第二步编写EchoServer:

package io.netty.example.echo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class EchoServer {
private int port;
public EchoServer(int port){
this.port = port;
} public void run() throws Exception{
//NioEventLoopGroup是一个处理I/O操作的多线程事件循环
//bossGroup作为boss,接收传入连接
//bossGroup只负责接收客户端的连接,不做复杂操作,为了减少资源占用,取值越小越好
//Group:群组,Loop:循环,Event:事件,这几个东西联在一起,相比大家也大概明白它的用途了。
//Netty内部都是通过线程在处理各种数据,EventLoopGroup就是用来管理调度他们的,注册Channel,管理他们的生命周期。
EventLoopGroup bossGroup=new NioEventLoopGroup(1);
//workerGroup作为worker,处理boss接收的连接的流量和将接收的连接注册进入这个worker
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
//ServerBootstrap负责建立服务端
//你可以直接使用Channel去建立服务端,但是大多数情况下你无需做这种乏味的事情
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
//指定使用NioServerSocketChannel产生一个Channel用来接收连接
.channel(NioServerSocketChannel.class)
//ChannelInitializer用于配置一个新的Channel
//用于向你的Channel当中添加ChannelInboundHandler的实现
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
};
})
//对Channel进行一些配置
//注意以下是socket的标准参数
//BACKLOG用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度。如果未设置或所设置的值小于1,Java将使用默认值50。
//Option是为了NioServerSocketChannel设置的,用来接收传入连接的
.option(ChannelOption.SO_BACKLOG, 128)
//是否启用心跳保活机制。在双方TCP套接字建立连接后(即都进入ESTABLISHED状态)并且在两个小时左右上层没有任何数据传输的情况下,这套机制才会被激活。
//childOption是用来给父级ServerChannel之下的Channels设置参数的
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
ChannelFuture f=b.bind(port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
//sync()会同步等待连接操作结果,用户线程将在此wait(),直到连接操作完成之后,线程被notify(),用户代码继续执行
//closeFuture()当Channel关闭时返回一个ChannelFuture,用于链路检测
f.channel().closeFuture().sync();
}finally{
//资源优雅释放
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
} public static void main(String[] args) {
int port=8088;
try {
new EchoServer(port).run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

  

三、依赖:

<!-- Netty开始 -->
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
</dependency>
<!-- Netty结束 -->

 

来源:https://blog.csdn.net/wocjy/article/details/78661464

Netty官方示例的更多相关文章

  1. DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...

  2. DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  3. DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  4. html5游戏引擎phaser官方示例学习

    首发:个人博客,更新&纠错&回复 phaser官方示例学习进行中,把官方示例调整为简明的目录结构,学习过程中加了点中文注释,代码在这里. 目前把官方的完整游戏示例看了一大半, brea ...

  5. 将百度坐标转换的javascript api官方示例改写成传统的回调函数形式

    改写前: 百度地图中坐标转换的JavaScript API示例官方示例如下: var points = [new BMap.Point(116.3786889372559,39.90762965106 ...

  6. ngRx 官方示例分析 - 3. reducers

    上一篇:ngRx 官方示例分析 - 2. Action 管理 这里我们讨论 reducer. 如果你注意的话,会看到在不同的 Action 定义文件中,导出的 Action 类型名称都是 Action ...

  7. ngRx 官方示例分析 - 2. Action 管理

    我们从 Action 名称开始. 解决 Action 名称冲突问题 在 ngRx 中,不同的 Action 需要一个 Action Type 进行区分,一般来说,这个 Action Type 是一个字 ...

  8. ngRx 官方示例分析 - 1. 介绍

    ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book  API  来查询图书,并保存自己的精选书籍列表. 菜单有两 ...

  9. Ionic 2 官方示例程序 Super Starter

    原文发表于我的技术博客 本文分享了 Ionic 2 官方示例程序 Super Starter 的简要介绍与安装运行的方法,最好的学习示例代码,项目共包含了 14 个通用的页面设计,如:引导页.主页面详 ...

随机推荐

  1. mysql 数据库备 及移动当天数据到历史表里 window下可用

    1 数据移动到历史表中,同时删除当天数据 test-move-record.bat c: cd C:\FQ_ManageServer\mysqlback mysql.exe -h 10.71.1.23 ...

  2. pro-select-limit-if

    drop procedure if exists p9; CREATE PROCEDURE p9 () BEGIN DECLARE a INT; DECLARE b INT; DECLARE c IN ...

  3. 区块链 block chain 去信任

    去中心化:不以参与交易的任何一方为中心 去信任:假定参与交易的任何一方都是不可信任的 区块链受到关注的原因 去中心化.去信任化.智能合约等,正好满足未来互联网持续发展所要求的信息的盖度自动化和高度程序 ...

  4. MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%) MylSAM平均每秒Key Buffer读命中率(%) MylSAM平均每秒Key Buffer写命中率(%)

    MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%)MylSAM平均每秒Key Buffer读命中率(%)MylSAM平均每秒Key Buff ...

  5. 阻塞IO、非阻塞IO、同步IO、异步IO等

    https://www.cnblogs.com/zingp/p/6863170.html 阅读目录 1 基础知识回顾 2 I/O模式 3 事件驱动编程模型 4 select/poll/epoll的区别 ...

  6. Struts2 框架的值栈

    1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...

  7. SQL Server中行列转换 Pivot UnPivot

    PIVOT用于将列值旋转为列名(即行转列),在SQLServer 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列)FOR 列 in (-) )AS P 完 ...

  8. Java中对Clone的理解

    面试中经常遇到Clone的相关知识,今天总算是把Clone理解的比较透彻了!Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Cl ...

  9. mysql 内置功能 函数 date_format函数

    创建数据库db12 create database db12 charset=utf8; use db12; 准备表和记录 CREATE TABLE blog ( id INT PRIMARY KEY ...

  10. JVM虚拟机—JVM的垃圾回收机制(转载)

    1.前言 理解JVM的垃圾回收机制(简称GC)有什么好处呢?作为一名软件开发者,满足自己的好奇心将是一个很好的理由,不过更重要的是,理解GC工作机制可以帮助你写出更好的Java程序. 在学习GC前,你 ...