当前位置:网站首页>Mina's long and short connections
Mina's long and short connections
2022-08-05 06:21:00 【Sajia Roshan Demon King】
1. Long connection
The two communicating parties maintain a connection state for a long time without disconnecting.
For example, when you log in to QQ, you will connect to the Tencent server. Once the connection is established, it will not be disconnected unless an exception occurs. This method is a long connection.
It consumes IO resources for long connections.
2. Short connection
The two communicating parties do not maintain a long-term connection state. After a request-response ends, the connection is automatically disconnected.
For example: http protocol, the client initiates an http request, and the server processes the http request. When the server finishes processing, it disconnects the client data after returning it, and needs to re-initiate the next connection request.
3. Short connection chestnut
Or refer to the previous section"Introduction to Mina""'s chestnut, modify the processor MyServerHandler.java on the Mina server side
package com.hl.magic.mina.mina1;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.core.session.IoSession;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Date;/*** Custom Mina server session adapter** @Author HL* @Date 2021/10/10 21:06*/public class MyServerHandler extends IoHandlerAdapter {private static final Logger LOGGER = LoggerFactory.getLogger(MyServerHandler.class);/*** create session** @param session session object*/@Overridepublic void sessionCreated(IoSession session) throws Exception {LOGGER.debug("sessionCreated");}/*** Open session** @param session session object*/@Overridepublic void sessionOpened(IoSession session) throws Exception {LOGGER.debug("sessionOpened");}/*** close the session** @param session session object*/@Overridepublic void sessionClosed(IoSession session) throws Exception {LOGGER.debug("sessionClosed");}/*** Session idle** @param session session object* @param status session status*/@Overridepublic void sessionIdle(IoSession session, IdleStatus status) {LOGGER.debug("IDLE: [{}]", session.getIdleCount(status));}/*** When the connection is abnormal** @param session session object*/@Overridepublic void exceptionCaught(IoSession session, Throwable cause) {LOGGER.debug("exceptionCaught");}/*** receive messages** @param session session object* @param message message body*/@Overridepublic void messageReceived(IoSession session, Object message) {String msg = (String) message;LOGGER.debug("Data received by server: [{}]", msg);//Long connection mode monitoring, if you want to change to short connection, comment the connection disconnection mode below, and disconnect in the messageSent method// if (msg.trim().equalsIgnoreCase("quit")) {// session.closeNow();// return;// }Date date = new Date();session.write("message received, time: " + date);}/*** Occurrence message** @param session session object* @param message message body*/@Overridepublic void messageSent(IoSession session, Object message) {LOGGER.debug("messageSent...");// Short connection method, automatically disconnect after sending the messagesession.closeNow();}}Run Mina server program MyServer.java class

Run CMD, enter telnet 127.0.0.1 54321 in the window to establish a connection with the server

Then send the data "ww", you can see that the server will automatically disconnect after receiving the data and giving a response.

边栏推荐
- 运维工程师,快来薅羊毛
- I/O performance and reliability
- 正则表达式小实例--验证邮箱地址
- 逻辑卷创建
- The highlight moment of operation and maintenance starts with intelligence
- Small example of regular expression--remove spaces in the middle and on both sides of the string
- Hugo搭建个人博客
- Logical volume creation
- Network wiring and digital-to-system conversion
- Regular expression small example - get number character and repeated the most
猜你喜欢

IP数据包格式(ICMP协议与ARP协议)

解决这三大问题,运维效率将超90%的医院

网络不通?服务丢包?看这篇就够了

I/O性能与可靠性

干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站

spark operator - map vs mapPartitions operator

LinkSLA坚持用户第一,打造可持续的运维服务方案

教您简单几步实现工业树莓派正确安装RS232转USB驱动

spark source code - task submission process - 1-sparkSubmit

Technology Sharing Miscellaneous Technologies
随机推荐
正则表达式小实例--去掉字符串中间和两边的空格
Programmers should understand I/O this way
What impact does CIPU have on the cloud computing industry?
spark source code - task submission process - 2-YarnClusterApplication
Getting Started Document 09 Standalone watch
深度 Zabbix 使用指南——来自惨绿少年
[Day5] Soft and hard links File storage, deletion, directory management commands
Take you in-depth understanding of cookies
VRRP概述及实验
network issue?Service packet loss?This is enough
RAID磁盘阵列
跨域的十种解决方案详解(总结)
逻辑卷创建
Wireshark packet capture and common filtering methods
[Paper Intensive Reading] The relationship between Precision-Recall and ROC curves
spark operator-parallelize operator
多线程之传递参数
Technology Sharing Miscellaneous Technologies
Cloud computing - osi seven layers and TCP\IP protocol
错误类型:反射。ReflectionException:无法设置属性“xxx”的“类”xxx”与价值“xxx”