Netty复习:(2)IdleStateHandler的用法

一、handler定义:

package handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class MyChatServerHandler3 extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println(evt);
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("received: " + msg);
    }
}

二、定义Initializer

package initializer;

import handler.MyChatServerHandler;
import handler.MyChatServerHandler3;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.concurrent.TimeUnit;

public class MyChatServerInitializer3 extends ChannelInitializer<SocketChannel>{


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        System.out.println("in initChannel......");
        //ch.pipeline().addLast(new DelimiterBasedFrameDecoder(4096,Delimiters.lineDelimiter()));
        ch.pipeline().addLast(new IdleStateHandler(3, 5, 6));
        ch.pipeline().addLast(new StringDecoder());
        ch.pipeline().addLast(new StringEncoder());

        ch.pipeline().addLast(new MyChatServerHandler3());

    }





}

三、定义server启动类,在pipeline中添加IdleStateHandler

package server;

import handler.MyChatServerHandler;
import initializer.MyChatServerInitializer;
import initializer.MyChatServerInitializer;
import initializer.MyChatServerInitializer3;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class MyChatServer3 {

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup =new NioEventLoopGroup();
        EventLoopGroup workerGroup =new NioEventLoopGroup();

        try
        {

            ServerBootstrap serverBootstrap=new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup);
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
            serverBootstrap.childHandler(new MyChatServerInitializer3());
            ChannelFuture channelFuture=serverBootstrap.bind(9999).sync();
            channelFuture.channel().closeFuture().sync();




        }
        finally
        {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }


    }

}

当客户端连接之后,如果一段时间不发送数据,则服务器端会触发IdleStateEvent并被处理
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/109865.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

第N个斐波那契数列

第N个斐波那契数列 力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 class Solution { public:int tribonacci(int n) {int a[4]{0,1,1,2};if(n<4) return a[n];int kn-3;for(int i0; i<k;i){int tmpa[3];a[3]a[1]a[2]a[3];//不是【0】开始&…

从零开始搭建Prometheus+grafana服务器组件监控系统

服务器及相关组件监控 本文档主要记录了常用企业级服务器及各种组件的监控手段和监控部署方案&#xff0c;使企业可以实时感知服务器组件的健康状态&#xff0c;并在服务器或组件出现异常时及时做出反应。 本方案采用的Prometheusgrafana的方式实现对服务器及各种组件的监控&am…

CentOS 搭建本地 yum 源方式 安装 httpd 服务

CentOS 搭建本地 yum 源方式 安装 httpd 服务 修改 yum 源 挂载光驱 mkdir -p /mnt/cdrom mount /dev/cdrom /mnt/cdromvi /etc/fstab追加以下内容&#xff1a; /dev/cdrom /mnt/cdrom iso9660 defaults 0 0手动修改CentOS-Base.repo 备份 yum 源配置文件 mv /etc/yum.re…

06 MIT线性代数-线性无关,基和维数Independence, basis, and dimension

1. 线性无关 Independence Suppose A is m by n with m<n (more unknowns than equations) Then there are nonzero solutions to Ax0 Reason: there will be free variables! A中具有至少一个自由变量&#xff0c;那么Ax0一定具有非零解。A的列向量可以线性组合得到零向…

ubuntu PX4 vscode stlink debug设置

硬件 stlink holybro debug板 pixhawk4 安装openocd 官方文档&#xff0c;但是第一步安装建议从源码安装&#xff0c;bug少很多 github链接 编译安装&#xff0c;参考 ./bootstrap (when building from the git repository)./configure [options]makesudo make install安装后…

CentOS 7 安装和配置java环境

1 安装包准备 安装包可以通过下面地址进行版本选择安装&#xff1a; https://www.oracle.com/java/technologies/downloads/#java8 2 正式开始安装 本次分享的安装方法直接通过编辑/etc/profile文件实现java的安装 2.1 新建安装包存放目录 mkdir /java cd /java/ 2.2 解压安…

【音视频|PCM】PCM格式详解

&#x1f601;博客主页&#x1f601;&#xff1a;&#x1f680;https://blog.csdn.net/wkd_007&#x1f680; &#x1f911;博客内容&#x1f911;&#xff1a;&#x1f36d;嵌入式开发、Linux、C语言、C、数据结构、音视频&#x1f36d; &#x1f923;本文内容&#x1f923;&a…

javascript数据类型

目录 原始数据类型 引用数据类型 类型检测 类型转换 总结 原始数据类型 JavaScript 中有六种原始数据类型&#xff0c;它们是&#xff1a; Undefined&#xff08;未定义&#xff09;: 表示一个未被赋值的变量。Null&#xff08;空值&#xff09;: 表示一个空对象指针。B…

SpringBoot 公司推广系统 公司广告系统

SpringBoot 公司推广系统 公司广告系统 系统功能 首页功能: 广告展示 方案列表 站内搜索 资讯 查看详细咨询 登录注册 收藏 咨询方案 在线客服实时聊天 后台管理功能: 系统管理分为: 用户管理 角色管理 客户管理 首页轮播管理 公告管理 方案管理: 方案管理 资讯管理: 类型管…

打包个人项目成python算法包

*免责声明&#xff1a; 1\此方法仅提供参考 2\搬了其他博主的操作方法,以贴上路径. 3* 场景一: 使用conda pack进行打包个人项目 场景二: … 场景一: 使用conda pack进行打包个人项目 1.1 导出包列表 activate jiancepip list --formatfreeze >requirements.txt 1.…

Transformers实战(二)快速入门文本相似度、检索式对话机器人

Transformers实战&#xff08;二&#xff09;快速入门文本相似度、检索式对话机器人 1、文本相似度 1.1 文本相似度简介 文本匹配是一个较为宽泛的概念&#xff0c;基本上只要涉及到两段文本之间关系的&#xff0c;都可以被看作是一种文本匹配的任务&#xff0c; 只是在具体…

【JavaEE】HTTP协议

HTTP协议 HTTP是什么?HTTP 协议格式HTTP 请求格式HTTP响应格式协议格式总结 HTTP 请求 (Request)认识 URLURL 基本格式 关于 URL encode认识 "方法" (method)1. GET 方法2. POST 方法 认识请求 "报头" (header) HTTP 响应详解认识 "状态码" (st…

Elasticsearch:使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation (二)

这是继上一篇文章 “Elasticsearch&#xff1a;使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation &#xff08;一&#xff09;” 的续篇。在这篇文章中&#xff0c;我主要来讲述 ElasticVectorSearch 的使用。 我们的设置和之前的那篇文章是一样的&#xff…

idea中Run/Debug Python项目报错 Argument for @NotNull parameter ‘module‘ of ...

idea中Run/Debug Python项目报错 Argument for NotNull parameter module of ... idea中运行Python项目main.py时报错&#xff1a; Error running main: Argument for NotNull parameter module of com/intellij/openapi/roots/ModuleRootManager.getInstance must not be nu…

Flash Attention 的优点以及Softmax 归一化系数解释

文章&#xff1a;FLASHATTENTION: Fast and Memory-Efficient Exact Attention with IO-Awareness 原始Attention 计算使用gpu存储标准流程 涉及两个gpu存储器&#xff1a; 1&#xff09;SRAM&#xff08;static Random Access Memory&#xff09;:静态随机存取存储器 2&…

管理类联考——数学——汇总篇——知识点突破——代数——整式分式——记忆

文章目录 考点记忆/考点汇总——按大纲 整体目录大纲法记忆宫殿法绘图记忆法 局部数字编码法归类记忆法重点记忆法歌决记忆法谐音记忆法理解记忆法比较记忆法转图像记忆法可视化法 本篇思路&#xff1a;根据各方的资料&#xff0c;比如名师的资料&#xff0c;按大纲或者其他方式…

什么是全排列?(算法实现)

全排列是什么&#xff1f; 全排列是指将一组元素按照一定顺序进行排列的所有可能结果。以一组数字为例&#xff0c;比如[1, 2, 3]的全排列结果为&#xff1a;[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]。 全排列有许多不同的计算方法&#xff0c;其中…

【电路笔记】-交流电感和感抗

交流电感和感抗 文章目录 交流电感和感抗1、概述1.1 电感1.2 电感器 2、频率特性2.1 电抗(Reactance)2.2 相移2.3 感应现象 3、RL滤波器4、总结 在之前有 交流电阻的文章中&#xff0c;我们已经看到电阻器在正常频率下的直流或交流状态下的行为是相同的。 然而&#xff0c;其他…

CN考研真题知识点二轮归纳(1)

本轮开始更新真题中涉及过的知识点&#xff0c;总共不到20年的真题&#xff0c;大致会出5-10期&#xff0c;尽可能详细的讲解并罗列不重复的知识点~ 目录 1.三类IP地址网络号的取值范围 2.Socket的内容 3.邮件系统中向服务器获取邮件所用到的协议 4.RIP 5.DNS 6.CSMA/CD…

Linux云服务器限制ip进行ssh远程连接

对Linux云服务器限制IP进行SSH远程连接的原因主要有以下几点&#xff1a; 增加安全性&#xff1a;SSH是一种加密的网络传输协议&#xff0c;可以保护数据的机密性和完整性。通过限制SSH连接的IP地址&#xff0c;可以防止未经授权的访问和数据泄露。只有拥有访问权限的IP地址才…
最新文章