Recently, I found a problem when syncing data. After inserting data in the background, I synchronized it with other departments. To put it simply, it is actually to call the interface provided by the other party and make HTTP request calls. Then I found a problem later. If an HTTP request is requested, the request timeout, the interrupt failed, and the IO exception is actually possible. If it is normal to open a web page, it is fine, but when it cannot be opened, you will close it, or the page will display information to you. However, synchronization cannot be done. Once the request fails, the data must be synchronized correctly. Today, I realize the importance of this problem.
String httpUrl = "https://www.baidu.com/s?ie=UTF-8&tn=90594569_hao_pg&wd=1"; URL url = null; HttpURLConnection httpConn = null; String result = ""; try { String address = httpUrl; url = new URL(address); httpConn = (HttpURLConnection) url.openConnection(); //A URL connection can be used for input and/or output. Set the //DoInput flag to true if you intend to use the URL connection for input, //false if not. The default is true. //URL connection can be used for input or output. If you want to connect input with URL, set the DoInput tag to true. //Input and output are aimed at computers, and if you consider it from a programmer's perspective, you will often be confused. //Input input and output output, then isn't it read from output or write from input? In fact, on the contrary, //Input input is input into the computer and the computer can read it, so it is from input read, and output is the computer output, which is through output write. httpConn.setDoOutput(false); //So if setDoInput(false), you cannot read when you want to read from URLConnection //Cannot read from URLConnection if doInput=false (call setDoInput(true)) httpConn.setDoInput(true); //The connection establishment timeout time and the data timeout time are also read, httpConn.setConnectTimeout(6000000); httpConn.setReadTimeout(6000000); httpConn.setRequestMethod("GET"); httpConn.connect(); //Get the status code int code = httpConn.getResponseCode(); System.out.println(code); //Read http request response BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); String line; while ((line = reader.readLine()) != null) { result = result + line+"/n"; } System.out.println(result); //Close IO and connect reader.close(); httpConn.disconnect(); } catch(Exception e){ log.error(e); } finally{ if(httpConn!=null) httpConn.disconnect(); }The code seems to be written in little, and the places where resources should be released are also released. If you should call the log output, you will also call it. In fact, the problem is the handling of exceptions. I thought some things were not synchronized before because of the connection timeout problem. So I specially caught the SocketTimeoutException exception. After looking at the log later, I found that it was a problem with the server on the synchronization interface and reported a 502 error. In fact, the exception is an IO exception.
No matter in that case, after this problem occurs, we must send the request again and confirm that the other party has been synchronized according to the result returned by the interface. If there is a temporary problem with the server, we can pause it for a short period of time and then request it again.
So the method that comes to mind is that since the real-time requirement of synchronization is not high, the interval can be longer. Then loop and start the thread again, interval between 5 minutes each time until the result is normal.
catch(Exception e){ for (int i = 0; i < 6; i++) { Thread t = new Thread(){public void run(){get();}}; t.start(); if(result.equals("ok")){ break; } try { Thread.sleep(300000); } catch (InterruptedException e2) { log.error(e2); } } log.error(e); }The above is a compilation of the information on Java HttpURLConnection timeout and IO exception handling. We will continue to add relevant information in the future. Thank you for your support for this site!