Reputation: 971
I'm deploying multiple WAR files in my tomcat containing different instances of the same app. each WAR file is containing a Class acting as a Singleton but this Singleton is getting the same values for each WAR I'm deploying...event If I need different value for each (DB parameters, port, etc,...) I read that as Singleton depends on the same ClassLoader as contained in the same JVM, this behavior is normal, but I would need to get a singleton instance independent for each WAR file I'm deploying.
is there any configuration of the tomcat server to ensure I have a different Singleton instance for each WAR instance ?
Upvotes: 1
Views: 2790
Reputation: 159215
Each webapp instance in Tomcat is independent of the others, so if you have multiple webapps deployed, they will have different instances of the singleton class. Of course, they will all be configured the same if you don't tell them otherwise.
If you want to run multiple instances of the same webapp in a single instance of Tomcat, you can do it simply by creating multiple context XML files. You don't even have to copy the war file.
In $TOMCAT_HOME$/conf/Catalina/localhost
, create multiple XML files, e.g.
foo.xml
<Context docBase="C:/path/to/MyWepApp.war">
<Parameter name="greeting" value="Hello from FOO"/>
</Context>
bar.xml
<Context docBase="C:/path/to/MyWepApp.war">
<Parameter name="greeting" value="Hello from BAR"/>
</Context>
If your webapp code calls ServletContext.getInitParameter("greeting")
to get that configuration value, it can now display a different greeting in each deployed instance.
You can now access them using http://localhost:8080/foo
and http://localhost:8080/bar
.
When you update the war file, they will both auto-redeploy, without having to restart Tomcat.
Upvotes: 4