-
Let's start with an example.
Client:
package top.quartz.util; /** * description: //connect timeout Setting up connection timeout time; * //read timeout,Is the timeout for data transfer. * Both values must be set. If only one is set, either case will block. * Then the current application presents a false dead state, which is a serious problem. Almost a lot of big companies have stepped on the pit. * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/9/23 3:22 PM */ import java.io.*; import java.net.*; public class TestUrl { /** * Compatible with HTTP and FTP protocols */ public static String getDocumentAt(String urlString) { StringBuffer document = new StringBuffer(); try { URL url = new URL(urlString); URLConnection conn = url.openConnection(); //connect timeout is the time-out to establish the connection. //read timeout is the time-out for transferring data. conn.setReadTimeout(10000); conn.setConnectTimeout(10000); System.out.println("Get connection---"); BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { document.append(line + "/n"); } reader.close(); } catch (MalformedURLException e) { System.out.println("Unable to connect to URL: " + urlString); } catch (IOException e) { System.out.println("IOException when connecting to URL: " + urlString); } return document.toString(); } /** * * @param args */ public static void main(String[] args) { //Test setReadTimeout time-out for data transfer //String documentAt = getDocumentAt("http://localhost:8081/avro/server"); //Test the timeout time for setConnect Timeout to establish a connection String documentAt = getDocumentAt("https://www.google.com/search"); System.out.println(documentAt); } }
-
Annotate
conn.setReadTimeout(10000); //And documentAt= getDocumentAt("https://www.google.com/search");
Analog links are normal, and data delivery timeouts have been unresponsive.
-
Annotate
conn.setConnectTimeout(10000); //And documentAt= getDocumentAt("http://localhost:8081/avro/server");
Analog connection timeouts have been unresponsive.
Another client directly accesses the user without using direct access to the service.
Server:
Previously written: https://my.oschina.net/u/3730149/blog/3106915 The main thing is to use the join method.
The startup class is jar package startup, and new Ceshi.serverr() is omitted from main function.
To enable client access http://localhost:8081/avro/server
Note: Once accessed, both the server and the client must be stopped in the test.
Conclusion: This problem is very serious in production; so lay a good foundation.
If there is a problem with the test, opening the corresponding timeout can interrupt the timeout. However, it's better to set up both in production. If there is a production problem because of this problem, we can only use jvm analysis tool, which is very troublesome.