Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two instances of Tomcat on the same OS

 
Chanakya Gupta
Ranch Hand
Posts: 37
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to this forum.

How to configure 2 JVMs, 2 Tomcats on the same OS ?
1. Is it possible ? If the answer is 'Yes', then,
2. How to have 2 different JAVA_HOME and JRE_HOME ?
3. Both Tomcats point to the same JRE_HOME, how can
we make the 2nd Tomcat point to the 2nd JRE ?


My aim is to test the HttpSession object's lifecycle on
2 JVMs as described in the book 'Head First Servlets and JSP',
on page 257, 'The Beer Web App distributed on two JVMs'


My Setup is -
1. Installed one JVM in '/usr/local' and
2nd JVM in '/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0'
JAVA_HOME is '/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0'
JRE_HOME is '/usr/lib/jvm/jre-1.6.0-openjdk'
2. OS is Fedora.
3. Installed two Tomcats, one in /usr/local/tomcat1
and 2nd in /usr/local/tomcat2
4. Created TC1_HOME=/usr/local/tomcat1 and
TC2_HOME=/usr/local/tomcat2

Thankyou in advance.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Chanakya,

yes it is possible to host many Tomcats on the same machine!

In any case you have to take care that the ports used by each Tomcat instance don't overlap. That means, depending on the configuration in the server.xml file, you have to configure different port numbers for the shutdown port, the HTTP and HTTPS connector and the AJP connector (if you use all these connectors).
Another possibility is to have multipe IP addresses and configure each Tomcat to use another IP so ports can't conflict.

To your question regarding the JVMs: Do you really want to use two DIFFERENT JVMs or just two instances of the JVM? If you mean two instances of the JVM you don't have to worry about this because each Tomcat will start in its own JVM anyway.

If you really want to configure different JVMs you can export the JAVA_HOME or JRE_HOME environment variables inside the involved startup scripts. A good place could be the catalina.sh/catalina.bat script of the Tomcat installation itself though you have to take care to maintain this when upgrading Tomcat. Or you could set these environment variables inside the shell or batch script used to startup Tomcat. This depends on the OS used, so in your case you could create one or multiple shell scripts to start Tomcat.

Clearer now??

Btw.: I have recently played around with multiple Tomcats and session replication and migration on my machine. You don't have to worry about the Java environment variables for this as I said. But it's helpful to have some scripts which help you to manage multiple Tomcats or generate the server.xml files with different ports for each Tomcat. Of course it's not that much work with only two Tomcats...

Marco
 
Chanakya Gupta
Ranch Hand
Posts: 37
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Marco !

Running two instances of Tomcat with the same JAVA_HOME and JRE_HOME
is possible. This implies the same JVM.

How to achieve what the book says,
'Beer Web App distributed across two VMs' ?

We need two distinct VMs, on the same OS. Right ?

Thankyou in advance !
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I don't know "the book"

But I know what you mean As I said it IS possible to run multiple Tomcats with multiple JVMs on the same machine. You don't have to do anything to achieve this because each Tomcat starts its own JVM anyway. It's the same as starting any application on your computer twice although you just have to install the application once. So there is one installation of Java (to which JAVA_HOME should point) and two installations of Tomcat (because it's the easiest way to have different Tomcat configuration files). And each started Tomcat automatically runs in its own instance of the JVM. I don't even know if it would theoretically be possible to run two Tomcat instances insided only one JVM...

You would have to take care about setting a different JAVA_HOME only if you wanted to use different VERSIONS of the JVM!

But as easy as it may sound it's not trivial to setup a Tomcat cluster with session replication and/or failover. You have to configure the Tomcats correctly and for failover it's almost necessary to have something like an Apache HTTP server as a "frontend" to your Tomcat backend servers to see an effect from the user perspective. A solid understanding of networking in general could be helpful, too.

As I said, I don't know what this book tells you but I hope my information can guide you to your goal. I you still experience problems just let us know

Marco
 
Chanakya Gupta
Ranch Hand
Posts: 37
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Marco !
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the time of doing the installation of Tomcat.

There is option of selecting the path of a j2se 5.0 JRE installed on your system.

You can select the sameJVM or a different one installed somewhere else.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chanakya is using Fedora. I guess there is no installer or installation routine to configure specific Java environment variables during installation of Tomcat. At least this is true for the Linux distributions I know.

Marco
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i thought he may be using windows7
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're running a Red Hat flavor of Linux, you can have as many JVM versions - both JDK and JRE - as you can cram onto the disk. If you install from the RPM, they're all placed in subdirectories of the /usr/java directory. The stock non-RPM install is just to explode a tarball in a suitably convenient directory. I usually find it convenient to name that directory /usr/java. You can do that for both non Red Hat and Red Hat - the RPM doesn't really do anything more than hard-code the install directory and explode into it anyway.

The "built-in" JVMs are usually more tightly bundled into to OS conventional paths, but they're still not fit for JEE use anyway, so I just ignore them and their never-to-be-sufficiently-accursed "alternatives" system, install Sun JVMs in /usr/java, and select the version I want by setting the JAVA_HOME environment variable.

If you're running multiple Tomcat instances, you have a number of possible configurations. If you want to run the exact same version of TOMCAT, this is where the distinction between CATALINA_HOME and CATALINA_BASE becomes important, because it allows you to select one Tomcat binary and launch its code in 2 distinct JVMs using 2 distinct configuration, webapp, log, and work file sets. Or, you can just make 2 separate copies of the entire Tomcat system, if you'd rather. In the case of a cluster, you normally want symmetry, so you'd point both systems' JAVA_HOME to the same jdk version, setup your CATALINA_HOME/CATALINA_BASE settings as desired, configure any necessary clustering settings, and go.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, you can just make 2 separate copies of the entire Tomcat system, if you'd rather..


To Quote Tim Holloway... that would be "something you do crudely..."

Follow Tim's advice on setting up two Tomcats with distinct configurations by reading the RUNNING.TXT file that comes with your Tomcat. In there you will find info on how to run multiple instances. You will want to start and stop Tomcat using scripts and that is where you can specify multiple CATALINA_BASE values, and if you really have to, you *could* run Tomcat instances on different versions of the JVM, but that would only be for testing purposes.

Like Marco has said, when you have multiple instances of Tomcat, you really have multiple instances of a JVM.
 
Chanakya Gupta
Ranch Hand
Posts: 37
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leo donahue wrote:
Or, you can just make 2 separate copies of the entire Tomcat system, if you'd rather..


To Quote Tim Holloway... that would be "something you do crudely..."

Follow Tim's advice on setting up two Tomcats with distinct configurations by reading the RUNNING.TXT file that comes with your Tomcat. In there you will find info on how to run multiple instances. You will want to start and stop Tomcat using scripts and that is where you can specify multiple CATALINA_BASE values, and if you really have to, you *could* run Tomcat instances on different versions of the JVM, but that would only be for testing purposes.

Like Marco has said, when you have multiple instances of Tomcat, you really have multiple instances of a JVM.



Thankyou Marco, leo, Tim, Shanky ! About the two JVMs, I got it,
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Or, you can just make 2 separate copies of the entire Tomcat system, if you'd rather.


Anyone,

The only disadvantage of this are: 1) disk space on the 2nd installation 2) maintainability, as it is more scaleable to use manageable configuations sets, than huge installations. Is this correct? Are there other disadvantages or dangers on such installations (multiple copies)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic