I want to run only one instance of my web-application which is deployed in tomcat 5.5 how to implement it.

for example if the system has 2 tomcat server each having the web application name xxx i don't want the two application run in parallel only one should run at a time.

suppose if the user access the index.jsp inside tomcat 1 and after that when he try to access the index.jsp from tomcat 2 it should n't happen

migrated from stackoverflow.com May 12 '11 at 19:03

This question came from our site for professional and enthusiast programmers.

  • what exactly do you mean by single instance? Do you want to create a singleton inside your web application or do you just want to create a web application and deploy it once? – csupnig May 12 '11 at 12:59
  • restrict the application from copying to the other tomcat server and accessing it – Lalchand May 12 '11 at 13:02

Here is one workaround way of doing it (assuming both Tomcat instances are on same host):

  • Deploy webapp from its own path using docBase parameter rather than copying it physically under individual webapp directory of both Tomcats
  • In your servlet's contextInitialized method start a ServerSocket on particular port eg: 5555
  • If you cannot start ServerSocket and it throws exception then catch that exception and re-throw a RuntimeException from the same contextInitialized method.

This way once you start first instance of the application it will start listening on that port 5555. Now when you start 2nd instance it obviously cannot acquire port 5555 since it is already in use and will result in RuntimeException, which in turn will cause Tomcat to ** not to deploy** 2nd instance of same webapp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy