- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
一般是通过心跳机制来检测长连接的状态。Netty 是通过 IdleStateHandler 来实现心跳机制,每隔一段时间,就向远端发出一次心跳,用于检测远端是否处于活动状态。
package netty.heartbeat;
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.nio.NioServerSocketChannel;
public class MyNettyServerTest {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// ServerBootstrap:服务端启动时的初始化操作
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 将 bossGroup 和 workerGroup 注册到服务端的 Channel 上,并注册一个服务端的初始化器 NettyServerInitializer
// 该初始化器中的 initChannel() 方法,会在连接被注册后立刻执行
// 最后将端口号绑定到 8888
ChannelFuture channelFuture = serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new MyNettyServerInitializer()).bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package netty.heartbeat;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit;
public class MyNettyServerInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel sc) throws Exception {
ChannelPipeline pipeline = sc.pipeline();
/*
IdleStateHandler:心跳机制处理器,主要用来检测远端是否读写超时,如果超时则将超时事件传入到 userEventTriggered(ctx,evt) 方法的 evt 参数中
IdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds,int allIdleTimeSeconds,TimeUnit unit)的参数含义如下:
readerIdleTimeSeconds:如果指定时间内没有检测到远端的读操作,则会触发形成 IdleState.READER_IDLE (读空闲)状态;
writerIdleTimeSeconds:如果指定时间内没有检测到远端的写操作,则会触发形成 IdleState.WRITER_IDLE (写空闲)状态;
allIdleTimeSeconds:如果指定时间内没有检测到远端的读和写操作,则会触发形成 IdleState.ALL_IDLE (读写空闲)状态;
unit:时间单位(默认:秒)
*/
pipeline.addLast("IdleStateHandler", new IdleStateHandler(3, 5, 7, TimeUnit.SECONDS));
// 自定义处理器
pipeline.addLast("MyNettyServerHandler", new MyNettyServerHandler());
}
}
package netty.heartbeat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
public class MyNettyServerHandler extends SimpleChannelInboundHandler<Object> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
}
// 如果 IdleStateHandler 检测到了超时事件,则会触发 userEventTriggered 方法
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
String eventType = null;
// 获取超时事件:READER_IDLE、WRITER_IDLE或ALL_IDLE
switch (event.state()) {
case READER_IDLE:
eventType = "读空闲";
break;
case WRITER_IDLE:
eventType = "写空闲";
break;
case ALL_IDLE:
eventType = "读写空闲";
break;
}
System.out.println(ctx.channel().remoteAddress() + "超时事件:" + eventType);
ctx.channel().close();
}
}
}
1 启动服务端,用 postman 去连接。
2 此时如果客户端不进行任何操作,3s 后就能看到服务端检测到了“读空闲事件”,运行结果如下。
/0:0:0:0:0:0:0:1:60250超时事件:读空闲
我正在使用血氧仪开展项目。我想对其进行平滑处理,以便可以使用它来计算心跳。我正在从麦克风收集原始数据,我将它们放在新数组中,比方说,sData[]。 信号真的很疯狂,如预期的那样在整个图上跳跃,所以我
如果您错过了 - TLS Heartbeat Extension 实现中的 OpenSSL 漏洞一直在流传。欲了解更多信息,请参阅http://heartbleed.com/ . 可能的缓解步骤之一是
随着关于 heartbleed 错误的讨论不断,很难找到关于 OpenSSL 的被利用的心跳扩展究竟用于什么的信息。 此外,是否可以针对 Apache w/mod_ssl 禁用它,而无需按照建议@ h
我是 Linux 新手,正在学习 LINUX heartbeat。我正在尝试做“yum install heartbeat”,如下所示…… # cat /etc/issueRed Hat Enterp
我在我们的服务器中使用带有kibana的heartbeat,metricbeat和elasticsearch 7.5,以检查服务器状态并检查应用程序的可用性。如果应用程序或服务器已关闭或没有响应,我想
我们有一个用 Java 编写的 RMI 客户端应用程序,它需要定期向服务器应用程序发送“保持 Activity ”消息。我们将其实现为一个单独的心跳线程,它向服务器发送保持 Activity 消息,然
我不确定如何最好地让我们的服务器知道我们的 iPhone 应用程序当前正在运行(即使在后台)。有几种不同的选择,但对于 Apple 批准程序允许的以及最可靠的选项存在一些担忧。如果考虑到 Apple
我似乎找不到关于如何在 Spring 中使用 websockets 向客户端发送心跳的好资源! 我有一个使用此配置运行的基本服务器: @Configuration @EnableWebSocketMe
我正在实现一个用于分布式 cronjob 执行的系统(所谓的 cron 计算集群)。当行动时间到来时,Cronjobs 应该排队进入消息队列(RabbitMQ)。在另一侧(集群的节点/ worker
使用 python kafka api 从主题中读取只有少量消息的消息。 Kafka 不断重复播放队列中的消息。 它从我的主题接收到一条消息(返回每条消息内容),然后抛出.更多日志: kafka.co
我目前有一个异步 TCP/IP 套接字 C# WinForms 应用程序,它是“服务器”应用程序。该应用程序持有客户端的持久套接字。我想实现一个心跳包来检测半开连接。 最初我只想创建一个循环并每隔 x
socket.io documentation提到可以像这样禁用心跳: io.disable('heartbeats'); 不幸的是,虽然这似乎阻止了心跳的发送,但客户端在不发送心跳时仍会断开连接。
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我是一名优秀的程序员,十分优秀!