当前位置:网站首页>Servlet3 0 + event driven for high performance long polling
Servlet3 0 + event driven for high performance long polling
2022-04-23 05:06:00 【hi wei】
servlet3.0 Asynchronous principle of
servlet I won't introduce the basics , Here is the introduction servlet3.0 An important new feature of : asynchronous .
servlet3.0 Schematic diagram :
- tomcat After receiving the request from the client, the request will be AsyncContext To the business thread , such tomcat The worker thread can release the connection to handle other requests .
- The business thread pool received AsyncContext after , You can handle the request business , After completing the business logic , according to AsyncContext obtain response, Return the response result .
- AsyncListener Will listen AsyncContext act , We can make corresponding business processing according to specific behavior .
servlet3.0 take tomcat Separate worker threads from business threads , such tomcat Worker threads can handle more connection requests . Business threads mainly deal with business logic . In this mode , The number of business threads can be better allocated , Also according to different business , Set a different number of threads , More flexible .
Be careful :tomcat Of NIO and servlet3.0 Asynchrony doesn't matter .tomcat NIO Pattern , Is for http Connection processing uses , The goal is to handle more connections with fewer threads .servlet3.0 Is in tomcat The processing logic of the working thread realizes the asynchronous processing function .
Use servlet3.0 Implement long polling
- What is long polling :
Long polling means that the client will always send requests to the server , It is applicable to the use of pushing data from the server to the client . Long polling should meet the following points :
- After the client initiates the request , When there is no data on the service side , Will not immediately return a null value , It is hold Live connection , Wait for data generation and return immediately .
- The request has a timeout on the server , Not all the time hold live . When the timeout after , The server will return timeout information , After receiving the return, the client will initiate the request again .
- After each request , The client will initiate the request again .
- Short polling 、 Long polling and long connection comparison :
- Short polling : The client sends to the server regularly Ajax request , After receiving the request, the server immediately returns the response information and closes the connection .
advantage : Back end programming is easier , Suitable for small applications ..
shortcoming : Most of the requests are useless , Waste bandwidth and server resources .
example : - Long polling : The client sends to the server Ajax request , The server receives the request hold Live connection , The response information is not returned and the connection is closed until there is a new message , After the client processes the response information, it sends a new request to the server .
advantage : Not frequent requests without messages .
shortcoming : The server hold Connections consume resources . - A long connection : The client and the server establish a long connection socket
advantage : High reliability , Real time high .
shortcoming : The implementation is complex , To maintain the heartbeat , The server maintains the connection and consumes resources .
Long polling implementation
Schematic diagram :
- After the request came in, , Generate an event , Add the corresponding event set . The request is set 30s Timeout time , And add monitor .tomcat Worker thread release .
- When the server data is ready , Trigger the corresponding event , Get subscription events from the container for execution . return when done response.
- request timeout ,listener Trigger , Return timeout information .
Let's see the specific implementation :
- Event definition , Here is just a simple event :
package com.hiwe.demo.event;
import javax.servlet.AsyncContext;
public class HttpEvent {
/** * It can be a business data primary key , Use the request name here as a simple demo */
private String requestName;
private AsyncContext asyncContext;
public HttpEvent(String requestName,AsyncContext asyncContext){
this.requestName = requestName;
this.asyncContext = asyncContext;
}
public String getRequestName() {
return requestName;
}
public AsyncContext getAsyncContext() {
return asyncContext;
}
}
Event manager :
package com.hiwe.demo.event;
import javax.servlet.AsyncContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
public class EventManager {
private final static Map<String,HttpEvent> subHttpEvents = new HashMap<>();
/** * New event subscription * @param event */
public static void addHttpEvent(HttpEvent event){
subHttpEvents.put(event.getRequestName(),event);
}
/** * Triggering event * @param requestName */
public static void onEvent(String requestName){
HttpEvent httpEvent = subHttpEvents.get(requestName);
if(httpEvent==null){
return;
}
AsyncContext asyncContext = httpEvent.getAsyncContext();
try {
PrintWriter writer = asyncContext.getResponse().getWriter();
writer.print(requestName+" request success!");
writer.flush();
asyncContext.complete();
subHttpEvents.remove(requestName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Asynchronous request listener :
package com.hiwe.demo.listener;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.PrintWriter;
@WebListener
public class AppAsyncListener implements AsyncListener {
@Override
public void onComplete(AsyncEvent asyncEvent) throws IOException {
System.out.println("AppAsyncListener onComplete");
// we can do resource cleanup activity here
}
@Override
public void onError(AsyncEvent asyncEvent) throws IOException {
System.out.println("AppAsyncListener onError");
//we can return error response to client
}
@Override
public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
System.out.println("AppAsyncListener onStartAsync");
//we can log the event here
}
/** * Timeout trigger * @param asyncEvent * @throws IOException */
@Override
public void onTimeout(AsyncEvent asyncEvent) throws IOException {
AsyncContext asyncContext = asyncEvent.getAsyncContext();
ServletResponse response = asyncEvent.getAsyncContext().getResponse();
PrintWriter out = response.getWriter();
// return code code , So that the front end can identify , And rebuild the request
out.write(201+" longPolling timeout");
out.flush();
asyncContext.complete();
}
}
- Long polling interface :
package com.hiwe.demo.controller;
import com.hiwe.demo.listener.AppAsyncListener;
import com.hiwe.demo.event.EventManager;
import com.hiwe.demo.event.HttpEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/app")
public class AsyncController {
/** * Long polling interface * @param requestName * @param request * @param response */
@GetMapping("/asyncGet")
public void getDemo(@RequestParam(value = "requestName") String requestName, HttpServletRequest request, HttpServletResponse response){
// Turn on Asynchronous Support
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
AsyncContext asyncContext = request.startAsync();
// Add listener
asyncContext.addListener(new AppAsyncListener());
// Set timeout
asyncContext.setTimeout(30000);
// Add to the event collection
HttpEvent httpEvent = new HttpEvent(requestName, asyncContext);
EventManager.addHttpEvent(httpEvent);
}
/** * Trigger events use * @param requestName */
@GetMapping("/trigger")
public void triggerDemo(@RequestParam(value = "requestName") String requestName){
EventManager.onEvent(requestName);
}
}
The above simple long polling is realized , We can test :
After starting the application, access :http://localhost:8080/app/asyncGet?requestName=123
The server is not ready because the data is not ready , So will hold Request for accommodation . When waiting 30s The timeout information will be returned after :
We are 30s Internal trigger event:http://localhost:8080/app/trigger?requestName=123
return :
The whole long polling implementation above is completed , If there is an error , Welcome to correct !
版权声明
本文为[hi wei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220550379278.html
边栏推荐
- Kubectl command automatic replenishment
- COM in wine (2) -- basic code analysis
- JS determines whether the numeric string contains characters
- 深度学习笔记 —— 物体检测和数据集 + 锚框
- [database] MySQL single table query
- Transaction isolation level of MySQL transactions
- In aggregated query without group by, expression 1 of select list contains nonaggregated column
- MySQL slow query
- redis和mysql区别
- C list field sorting contains numbers and characters
猜你喜欢
Discussion on flow restriction
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
The applet calls the function of scanning QR code and jumps to the path specified by QR code
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
redis数据类型有哪些
使用zerotier让异地设备组局域网
深度学习笔记 —— 微调
【数据库】表的查看、修改和删除
[2021] Spatio-Temporal Graph Contrastive Learning
随机推荐
深度学习笔记 —— 语义分割和数据集
Chapter I overall project management of information system project manager summary
信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
#define 定义常量和宏,指针和结构体
Unity C# 网络学习(四)
What are the redis data types
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
Innovation training (VI) routing
Innovation training (10)
Leetcode -- heuristic search
Innovation training (II) task division
API slow interface analysis
Perfect test of coil in wireless charging system with LCR meter
Implementation of switching windows and capturing data in selenium mode
The WebService interface writes and publishes calls to the WebService interface (I)
redis和mysql区别
Uglifyjs compress JS
Deep learning notes - fine tuning
Cross border e-commerce | Facebook and instagram: which social media is more suitable for you?
Detailed explanation of the differences between TCP and UDP