Netty(九) Netty会话清除

2024-06-20 08:58
文章标签 会话 netty 清除

本文主要是介绍Netty(九) Netty会话清除,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Netty(九) Netty会话清除

netty学习目录
一、Netty(一) NIO例子
二、Netty(二) netty服务端
三、Netty(三) Netty客户端+服务端
四、Netty(四) 简化版Netty源码
五、Netty(五)Netty5.x服务端
六、Netty(六) Netty Http 服务器例子
七、Netty(七) Netty服务端+客户端代码
八、Netty(八) Netty多客户端连接例子
九、Netty(九) Netty会话清除
十、Netty(十) Netty自定义编码器解码器
十一、Netty(十一) Netty对象传输

package com.zqw.netty5x.heart;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;public class Server {public static void main(String[] args) {ServerBootstrap bootstrap = new ServerBootstrap();EventLoopGroup boss = new NioEventLoopGroup();EventLoopGroup worker = new NioEventLoopGroup();try{bootstrap.group(boss, worker);bootstrap.channel(NioServerSocketChannel.class);bootstrap.childHandler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel ch) throws Exception {ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new IdleStateHandler(5,5,10));ch.pipeline().addLast(new ServerHandler());}});bootstrap.option(ChannelOption.SO_BACKLOG,1024);bootstrap.childOption(ChannelOption.TCP_NODELAY, true);bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);ChannelFuture future = bootstrap.bind(7777);System.out.println("服务启动!");future.channel().closeFuture().sync();}catch (Exception e){e.printStackTrace();}finally {boss.shutdownGracefully();worker.shutdownGracefully();}}
}
package com.zqw.netty5x.heart;import io.netty.channel.*;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.EventExecutorGroup;public class ServerHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {System.out.println(msg);ctx.writeAndFlush("回话");}@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {if(evt instanceof IdleStateEvent){IdleStateEvent event = (IdleStateEvent) evt;if(event.state() == IdleState.ALL_IDLE){ChannelFuture future = ctx.writeAndFlush("我要把你清除掉了");future.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture future) throws Exception {future.channel().close();}});return;}}super.userEventTriggered(ctx, evt);}
}
package com.zqw.netty5x.heart;import com.zqw.netty5x.ClientHandler1;
import com.zqw.netty5x.MutiClient1;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;public class MultiClient {private Bootstrap bootstrap = new Bootstrap();private List<Channel> channels = new ArrayList<>();private AtomicInteger integer = new AtomicInteger();public void init(int count){EventLoopGroup worker = new NioEventLoopGroup();bootstrap.group(worker);bootstrap.channel(NioSocketChannel.class);bootstrap.handler(new ChannelInitializer<io.netty.channel.Channel>() {@Overrideprotected void initChannel(io.netty.channel.Channel ch) throws Exception {ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new ClientHandler1());}});for (int i = 0; i < count ; i++) {ChannelFuture future = bootstrap.connect("192.168.1.4",7777);channels.add(future.channel());}}public Channel next(){return getChannel(0);}private Channel getChannel(int count) {Channel channel = channels.get(Math.abs(integer.getAndIncrement() % channels.size()));if(!channel.isActive()){if(count >= channels.size()){throw new RuntimeException("没有可用的channel");}reconnect(channel);return getChannel(++count);}return channel;}private void reconnect(Channel channel) {synchronized (channel){System.out.println("重连。。。。。");int index = channels.indexOf(channel);channel = bootstrap.connect("192.168.1.4",7777).channel();channels.set(index, channel);}}public static void main(String[] args) {MultiClient mutiClient = new MultiClient();mutiClient.init(10);Scanner scanner = new Scanner(System.in);while(true){System.out.println("请输入:");String str = scanner.next();try{Channel channel = mutiClient.next();channel.writeAndFlush(str);}catch (Exception e){e.printStackTrace();}}}
}
package com.zqw.netty5x;import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerInvoker;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;public class ClientHandler1 extends SimpleChannelInboundHandler<String> {@Overrideprotected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {System.out.println(msg);}
}

这篇关于Netty(九) Netty会话清除的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077682

相关文章

如何让应用在清除内存时保持运行

最近在写聊天软件。一个聊天软件需要做到在清除内存时仍能保持其应有的状态。      首先,我尝试在应用的Service中的onDestroy()进行重启应用,经过测试,发现被强制清除内存的应用不会调用Service的onDestroy,只会调用activity的onDestroy(),于是我决定在触发activity的onDestroy( )处发送广播给应用的静态广播接收器,然后让广播

AIGC大模型智能抠图(清除背景):Sanster/IOPaint,python(2)

AIGC大模型智能抠图(清除背景):Sanster/IOPaint,python(2)   在文章(1)的基础上,尝试用大模型扣除图中的某些主要景物。 1、首先,安装插件: pip install rembg   2、第1步安装成功,启动webui,注意,这里要启用清除背景/抠图的插件 --enable-remove-bg : iopaint start --model=lama

【Netty】netty中都是用了哪些设计模式

对于工程师来说,掌握并理解运用设计模式,是非常重要的,但是除了学习基本的概念之外,需要结合优秀的中间件、框架源码学习其中的优秀软件设计,这样才能以不变应万变。 单例模式 单例模式解决的对象的唯一性,一般来说就是构造方法私有化、然后提供一个静态的方法获取实例。 在netty中,select用于处理CONTINUE、SELECT、BUSY_WAIT 三种策略,通过DefaultSelectStra

Java语言的Netty框架+云快充协议1.5+充电桩系统+新能源汽车充电桩系统源码

介绍 云快充协议+云快充1.5协议+云快充1.6+云快充协议开源代码+云快充底层协议+云快充桩直连+桩直连协议+充电桩协议+云快充源码 软件架构 1、提供云快充底层桩直连协议,版本为云快充1.5,对于没有对接过充电桩系统的开发者尤为合适; 2、包含:启动充电、结束充电、充电中实时数据获取、报文解析、Netty通讯框架、包解析工具、调试器模拟器软件等; 源码合作 提供完整云快充协议源代码

CSS学习12--清除浮动的本质及处理办法

清除浮动 前言一、清除浮动的本质二、清除浮动的方法 前言 为什么要清除浮动? 浮动不占用原文档流的位置,可能会对后面的元素排版产生影响。因此需要在该元素中清除浮动,清除浮动后造成的影响。 一、清除浮动的本质 为了解决父级元素因为子级元素引起内部高度为0的问题。 <html><head><style>* {padding: 0;margin: 0;}.box1 {width:

文件内容的清除

想到两种方法 -1.用空格覆盖所有内容(有问题,内容全变成空格)    int  fd=open(filename,O_RDWR)    struct stat stBuf;    stat(filename,&stBuf);    len=stBuf.st_size;    char *szBuf=(char*)malloc(len);    bzero(s

leetcode:3174 清除数字

3174 清除数字 题目链接https://leetcode.cn/problems/clear-digits/ 题目描述 给你一个字符串 s 。 你的任务是重复以下操作删除 所有 数字字符: 删除 第一个数字字符 以及它左边 最近 的 非数字 字符。 请你返回删除所有数字字符以后剩下的字符串。 示例 1: 输入:s = "abc"输出:"abc" 解释:字符串

SQL循环清除表数据

SQL循环执行清除表数据语句 最近项目经常需要清库测试 但是一个个 truncate 很慢 浪费时间 所以写了个 sql批量清除表数据 这样方便下次使用 灵活性也很高 语句不仅可以 用来清除数据 也可以 批量update delete等 逻辑: 根据 字符拆分 字符串 获取每次执行语句的 表名 根据 split 获取字符串内有多少个表 也就是循环的次数 每次循环 先获取本次 执行语

火狐浏览器重置密码后收藏的标签密码等数据被清除

火狐浏览器重置密码后收藏的标签密码等数据被清除 最早接触火狐是因为当时开发前端页面,firebug是当时最好用的前端调试工具。 用了很多年,最近因为一次重置密码,把我10几年的收藏数据全都清空了。还无法找回… 现在大部分web应用都要求使用chrome,比如在线文档、在线的office等,可是我还一直坚持使用火狐浏览器。 只是因为当初的先入为主,一直还坚持使用火狐浏览器,这次的遭遇让我丢失10年

Netty源码解析9-ChannelHandler实例之MessageToByteEncoder

MessageToByteEncoder框架可见用户使用POJO对象编码为字节数据存储到ByteBuf。用户只需定义自己的编码方法encode()即可。 首先看类签名: public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter 可知该类只处理出站事件,切确的说是write事件