当前位置:网站首页>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.
边栏推荐
- May I ask how to read the binlog of the two tables of hologres through flink sql, and then how to join?
- Billions of IT operations in the market, the product by strength to speak
- 逻辑卷创建
- wc、grep、tar、vi/vim
- 交换机原理
- Spark source code-task submission process-6.1-sparkContext initialization-create spark driver side execution environment SparkEnv
- 媒体查询、rem移动端适配
- One-arm routing and 30% switch
- OpenCV3.0 is compatible with VS2010 and VS2013
- Small example of regular expression--validate email address
猜你喜欢
[issue resolved] - jenkins pipeline checkout timeout
disabledDate 日期选择器 datePicker
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
IP地址及子网的划分
入职前,没想到他们玩的这么花
运维的高光时刻,从智能化开始
5分钟完成mysql离线安装
static routing
Cloud computing - osi seven layers and TCP\IP protocol
The highlight moment of operation and maintenance starts with intelligence
随机推荐
千亿IT运维市场,产品要凭实力说话
NAT实验
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
网络层协议介绍
Into the pre-service, thought they play so flowers
[Day6] File system permission management, file special permissions, hidden attributes
实力卷王LinkSLA,实现运维工程师快乐摸鱼
Getting Started Document 07 Staged Output
User and user group management, file permission management
原生JS带你了解数组方法实现及使用
Hugo builds a personal blog
有哪些事情是你做了运维才知道的?
selenium学习
正则表达式小示例--获取重复最多的字符及其数量
IP地址及子网的划分
Apache configure reverse proxy
Small example of regular expression--validate email address
静态路由
监控系统的内卷,有什么讲究?
Network wiring and digital-to-system conversion