Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running my java code using Tomcat,currently it shows bunch of errors whenever the target endpoint is not reachable/unavailable/cant get any response. I tried to add catch exception in my code but it seems not working.

Java
reportUrl = "http://Unavailable";
                HttpEntity<DrEndpointRequest> entity = new HttpEntity<>(der, headers);
                try {
                    ResponseEntity<String> response = restTemplate.exchange(reportUrl, HttpMethod.POST, entity, String.class);
                    System.out.println("Response-- " + response.getBody() + " " + response.getStatusCode());
                    if (response.getStatusCode() == HttpStatus.OK) {
                        trans.setDnStatus("Sent");
                    } else {
                        trans.setDnStatus("Failed");
                    }
                } catch (HttpStatusCodeException ex) {
                    System.out.println(ex.getStatusText() + "   " + ex.getResponseBodyAsString());
                    trans.setDnStatus("Failed");
                }



below is the error shown in tomcat :

2019-04-30 02:33:55.016 ERROR 4432 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://Unavailable": Unavailable; nested exception is java.net.UnknownHostException: Unavailable at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at com.dr.scheduler.ScheduledTask.scheduleTaskWithFixedRate(ScheduledTask.java:97) ~[classes/:1.0] at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_201] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_201] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_201] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_201] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201] Caused by: java.net.UnknownHostException: Unavailable at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_201] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_201] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_201] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_201] at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_201] at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.(HttpClient.java:242) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984) ~[na:1.8.0_201] at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:734) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 15 common frames omitted


What I have tried:

i added
catch (HttpStatusCodeException ex) {
                    System.out.println(ex.getStatusText() + "   " + ex.getResponseBodyAsString());
                    trans.setDnStatus("Failed");
                }

but not working
Posted
Updated 29-Apr-19 20:16pm
v2
Comments
Richard MacCutchan 30-Apr-19 3:39am    
It can only catch the exception you tell it to look for. In your case the system has thrown a different one.

1 solution

Try intercepting a ResourceAccessException or a UnknownHostException instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900