ShaggyInjun
ShaggyInjun

Reputation: 2963

Managing rest web app hostname urls in tomcat

This question might have been asked before as it is quite a common exercise, but I am not sure what to search with.

I have a rest client app hosted on one instance of Tomcat and a rest server hosted on a different tomcat instance on another hostname. This works well, but currently I have the hostname hardcoded in the java classes. How can I parameter-ize the host name inside the rest client so that it allows for a future change without restarting the tomcat on which the rest client is hosted ?

Upvotes: 0

Views: 437

Answers (3)

Amit
Amit

Reputation: 288

You can always pass the host name in arguments during client process startup and then read those arguments using System.getProperty("hostName").

Upvotes: 0

Koitoer
Koitoer

Reputation: 19533

A quite common technique to pass environment variables and even hostname is define a System variable that you could read using java.lang.System.getenv() or java.lang.System.getProperty() if you are using Maven once your application startup read this variable in a static class of constants so the value will be set at the beginning and remain until the end of the application there other techniques that involve, web.xml but to keep it simple this will work. Just add the var to the enviroment and read it.

Upvotes: 1

Jason
Jason

Reputation: 11832

There are a number of solutions you could employ including (but not limited to):

  1. look up the server hostname in a properties or xml file on the client server (each time you want to make a REST call)
  2. provide a mechanism in the client app to configure the server hostname and store it in memory
  3. look up the server hostname in a directory such as LDAP or AD or in a database (each time you want to make a REST call)

You could employ a caching mechanism if you don't want to look up the value every time - perhaps once it is read it only expires after a number of seconds/minutes, or after a number of calls.

Upvotes: 1

Related Questions