English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
I found a problem when synchronizing data recently. I inserted data in the background and then synchronized it with the background of other departments. To put it simply, it is actually to call the interface provided by the other party for HTTP request calls. Then I found the problem. For HTTP requests, there is a possibility of request timeout, failure due to interruption, and IO exceptions are also possible. It's okay to open a webpage normally, but if you can't open it, you will close it, or the page will display information to you. However, for synchronization, this cannot be done. Once the request fails, the data must be synchronized correctly, and I realized the importance of this problem today.
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 //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 use URL connection input, set the DoInput tag to true. //Input and output are for the computer. If considered from the perspective of a programmer, it is often confused. //Input is input, output is output, then not read from output, write to input, right? Actually, the opposite is true. //Input is input into the computer, and the computer can read it, so it is read from input, and output is computer output, written through output. httpConn.setDoOutput(false); //Therefore, if setDoInput(false), you cannot read from URLConnection //Cannot read from URLConnection if doInput=false (call setDoInput(true)) httpConn.setDoInput(true); //The connection establishment timeout and the data read timeout time still remain, httpConn.setConnectTimeout(600000); httpConn.setReadTimeout(600000); httpConn.setRequestMethod("GET"); httpConn.connect(); //Get 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 connection reader.close(); httpConn.disconnect(); } catch(Exception e){ log.error(e); } finally{ if(httpConn!=null) httpConn.disconnect(); }
The code looks simple, and the resources are released where they should be. The logs are also outputted as they should be. In fact, the problem is the exception handling. I thought some things were not synchronized because of the connection timeout problem before. So I specially caught the SocketTimeoutException exception, and after looking at the logs, I found that the problem was on the server side of the synchronization interface, reported502error. In fact, the exception is an IO exception.
In either case, we need to send the request again after the problem occurs, confirm that the other party has synchronized according to the interface return result. If the server has a temporary problem, we can pause for a little while and then request again.
So the temporary method I thought of is, since the real-time requirement of synchronization is not high, the interval time can be longer. Then loop, start another thread, and each time the interval5minutes 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); }
This is the summary of the materials for handling HttpURLConnection timeout and IO exceptions in Java, and more related materials will be supplemented later. Thank you all for your support of this website!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting. Provide relevant evidence, and once verified, the website will immediately delete the infringing content.