Search code examples
javaspringtomcatproxyresttemplate

Intermittent 503 issue with JVM proxy parameters on tomcat


I am facing an intermittent issue on tomcat while making a rest call. Setup:

ApplicationA running on standalone tomcat on portX

ApplicationB running on another standalone tomcat on portY both the tomcat installations are on the same machine. Java version is JRE6

Following parameters are added to the JVM_OPTS in bash profile -Dhttp.proxyHost=[host] -Dhttp.proxyPort=[port] -Dhttp.nonProxyHosts=localhost

Application A makes a rest call to application B using Spring RestTemplate and gets a 503 error (Service unavailable) response.

The URL being used is http://localhost:portY/ApplicationB/restapipath

I cause is that the requests to localhost are going through the proxy defined in JVM_OPTS, but as per my understanding all localhost urls should be called without using any proxy.

This issue is happening intermittently, but once it starts it continues till I restart the ApplicationA tomcat server.

Also, my understanding is that any calls made through Spring RestTemplate will honour the JAVA_OPTS proxy settings without any additional handling in the code.

Any pointers, help is appreciated.


Solution

  • After hours of frustration I found the solution. The problem was that my understanding about JVM proxy parameters was incorrect.

    Parameters in JVM_OPTS in bash profile -Dhttp.proxyHost=[host] -Dhttp.proxyPort=[port] are actually not used automatically when using Spring RestTemplate. These parameters have to be fetched using System.getProperty('http.proxyHost') and then explicitly set in the DefaultHttpClient

    Now the issue in my code was that there was a single httpClient bean defined in the applicationContext.xml which was being used by both the rest calls. The non-proxy rest call worked fine until the with-proxy rest call updated httpClient object with the proxy details.

    Solution was to user separate httpClient objects for both rest calls.