环境:springboot2.5.12
application.yml配置
server:port: 8081tomcat:maxThreads: 10maxConnections: 10acceptCount: 1connectionTimeout: 3000
测试1:
在controller中休眠10s>connectionTimeout
public class TestController {public Object index() {try {System.out.println(Thread.currentThread().getName()) ;TimeUnit.SECONDS.sleep(10) ;} catch (InterruptedException e) {e.printStackTrace();}return "success" ;}}
发现程序能正常地响应。
结论:connectionTimeout参数与具体的请求响应时间是没有关系的。
测试2:
通过HttpURLConnection发送请求
public class HttpURLConnectionDemo {public static void main(String[] args) throws Exception {HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8081/test/index").openConnection() ;con.setDoInput(true) ;con.setDoOutput(true) ;long start = System.currentTimeMillis() ;InputStream is = con.getInputStream() ;Scanner scan = new Scanner(is) ;while(scan.hasNext()) {System.out.println("接收到内容:" + scan.next() + "\n耗时:" + (System.currentTimeMillis() - start)) ;}scan.close() ;con.disconnect() ;con = null ;}}
结果:

结论:connectionTimeout参数与什么样的客户端做连接请求没关系。
测试3:
通过Socket建立连接
public class TomcatConnectionTimeoutDemo {public static void main(String[] args) throws Exception {Socket socket = new Socket("127.0.0.1", 8081) ;long start = System.currentTimeMillis() ;InputStream is = socket.getInputStream() ;is.read() ;System.out.println(System.currentTimeMillis() - start ) ;}}
运行结果:

差不多3s后程序结束了,也就是连接断开了。接着测试:
public class TomcatConnectionTimeoutDemo {public static void main(String[] args) throws Exception {Socket socket = new Socket("127.0.0.1", 8081) ;long start = System.currentTimeMillis() ;final OutputStream os = socket.getOutputStream() ;new Thread(() -> {Scanner scan = new Scanner(System.in) ;while(scan.hasNext()) {String content = scan.next() ;System.out.println("准备发送:" + content) ;try {os.write(content.getBytes()) ;os.flush() ;} catch (IOException e) {e.printStackTrace() ;}}}).start() ;InputStream is = socket.getInputStream() ;is.read() ;System.out.println(System.currentTimeMillis() - start ) ;}}
结果1(什么也不做):

结果2(控制台不停的输入内容):

程序一开始运行,控制台不停地输入内容,发现程序一直正常,当停留3秒后在输入内容,发现程序又断开了。
结论:connectionTimeout参数是说当客户端与服务器连接以后,如果客户端不输入任何内容,那么超过了connectionTimeout设置的时间后连接会被断开。
求关注+转发



