- Improve the stability of "UriBuilder.getPublicIP()", by using a "HttpURLConnection" to increase the connection and read timeouts and avoid timeout-exceptions.

- Update Spring.
This commit is contained in:
Lampros Smyrnaios 2023-01-03 18:39:50 +02:00
parent 4528d1f9be
commit 9904ea5743
2 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,5 @@
plugins { plugins {
id 'org.springframework.boot' version '2.7.6' id 'org.springframework.boot' version '2.7.7'
id 'io.spring.dependency-management' version '1.1.0' id 'io.spring.dependency-management' version '1.1.0'
id 'java' id 'java'
} }

View File

@ -8,8 +8,8 @@ import org.springframework.core.env.Environment;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -55,18 +55,22 @@ public class UriBuilder {
private static String getPublicIP() private static String getPublicIP()
{ {
String publicIpAddress = ""; String publicIpAddress = "";
URL url_name; HttpURLConnection conn = null;
try { try {
url_name = new URL("https://api.ipify.org/"); conn = (HttpURLConnection) new URL("https://api.ipify.org/").openConnection();
} catch (MalformedURLException mue) { conn.setConnectTimeout(60_000);
logger.warn(mue.getMessage()); conn.setReadTimeout(60_000);
return null; conn.setRequestMethod("GET");
} conn.connect();
try (BufferedReader bf = new BufferedReader(new InputStreamReader(url_name.openStream()))) { try ( BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
publicIpAddress = bf.readLine().trim(); publicIpAddress = bf.readLine().trim();
}
} catch (Exception e) { } catch (Exception e) {
logger.warn("Cannot get the publicIP address for this machine!", e); logger.warn("Cannot get the publicIP address for this machine!", e);
return null; return null;
} finally {
if ( conn != null )
conn.disconnect();
} }
return publicIpAddress; return publicIpAddress;
} }