Invoking Rest API in @preDestory method and getting unknownHostExeception issue in Spring boot application

2/28/2022

I'm calling API using RestTeplate.postForEntity(); in the @preDestroy annotated method. Deploying it as AWS EKS and using kubectl command scaling down the pod and the @preDestroy method is getting called but RestTeplate.postForEntity() call is failing due to unknownhostExeception

Error log: <br> Test API call is failed org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://test.com/v2/testprocess/test": test.com; nested exception is java.net.UnknownHostException: test.com| at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)| at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:689)| at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:452)| at com.test.TestClient.testAPI(c.java:226)| at com.test.TestClient.testAPI(TestClient.java:52)|Caused by: java.net.UnknownHostException: test.com| at java.net.InetAddress.getAllByName0(Unknown Source)| at java.net.InetAddress.getAllByName(Unknown Source)| at java.net.InetAddress.getAllByName(Unknown Source)| at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)| at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)| at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)| at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)| at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)| at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)| at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)| at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)| at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)| at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)| at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)| at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)| at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)| at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)| at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:733)| ... 4 more

<br> @mozello - thanks for responding, In my app I call the API in two aspect, 1. During the App startup for this i'm using @postConstuctor annotated method and API is call is happening perfectly fine. which means the k8's able to resolve the "test.com" right. But getting this unknownHostException issue only during the @preDestroy method execution, pls suggest.
-- Sivakumar Neelam Veera
amazon-eks
java
kubernetes
spring
spring-boot

0 Answers