How to run multiple applications on a single tomcat

I do this in Ubuntu 12.10 but I think it applies to Windows 7 configurations too. I wanted to run more than one application in a single instance of tomcat. I have to say this is not the best decision I have made.

You can load two apps in one server instance if you put them in different directories and have two different host names.

In my case, my two hostnames are app1.internal.kw.sg and app2.internal.kw.sg, yours will be different. You can edit your hosts here in your /etc/hosts

Both of my apps would be called app1 and app2. I put them into the corresponding directories, as specified in appBase. In my case, I placed them in:

  • /var/lib/tomcat7/app1
  • /var/lib/tomcat7/app2

You can open up your /etc/tomcat7/server.xml file. In Windows 7, it would be where ever your central tomcat configuration files are at. This is my server.xml file:

[code language=”xml”]<?xml version=’1.0′ encoding=’utf-8′?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

</Server>
[/code]

You can refer to it. Unfortunately I cannot provide much support for this.

Disadvantages

Well, this method has a key disadvantage, each time you rebuild your Java classes, you need to restart the server and it would be better to be able to restart one application at a time rather than both applications together due to a longer loading time. Most of the time you will only make changes to one application anyway. It is better to have a configuration with two servers and each of them supporting just one application. That’s my take and I learnt it the hard way.

Too much magic in Java IDEs

I am very bothered by the way a typical Java web site development works. Java website developments, through JSP (Java Server Pages) is largely supported by IDEs such as Eclipse and Netbeans. It is not in any way the easiest to utilize. And at worst these huge IDEs are too kludgy for my liking.

Take the current Java project that I am working on, it’s a website that has sign in and numerous data presentation tools. Everything .java compiles into a .class file and the remaining JSP files are deployed into this WAR file through Apache Maven into tomcat. With an IDE, everything seems seamless. You don’t even need to know how it works. Just by magic. You configure the servers and paths and it just works.

You could of course decide to dump WAR files again and again to the server paths and watch it automatically deploy after you do a manual restart of tomcat. Unfortunately that’s too unproductive, starting a tomcat server takes 2 seconds for me, and starting the application takes 10 seconds in debug mode. That’s unacceptably slow because it all adds up.

So, IDEs make you more productive by deploying the files in the server for you and compiles the .java files into .class and .jar files or something like that. Essentially you only need to restart the tomcat7 server if you updated the controller. Updates to the views do not require compilation. However — there’s always a caveat — you have the .jsp file edited on the server path rather than your workspace.

Now, under the hood of the IDE, each time you save a .jsp file, what you are essentially doing is:

  1. Saving the file in your workspace.
  2. Copying the file from your workspace to the server path.

Your workspace can be managed by revisioning tools such as SVN or git and the remnants don’t get copied over to the server space. It’s pretty well thought except that you really need a proper IDE set up to save you all the repetitive work.

So the reason why I am writing this is because I wanted to use Sublime Text and Sublime Text doesn’t have Java and tomcat integration features at all. At first this seemed surprising, after all Java is so common. Upon discovering what Eclipse and Netbeans actually does, I can’t help but to feel that there is a certain amount of over-engineering.

Forgive me if that’s not the way Java IDEs work. I’m uncovering new things every day still, so please correct my mistakes, I want to understand the platform better too.

Tomcat paths in Ubuntu 12.10

And so I have the displeasure of working with Java — a platform I once swear to avoid for the rest of my life — and I had to have my Ubuntu 12.10 environment set up. This is also verified to be similar in Ubuntu 12.04.

If you install tomcat7 from the official repository using “sudo apt-get install tomcat7”, the following paths and commands are probably helpful to you too.

Your WAR files

WAR files (.war) typically go into:

[code language=”bash”]/var/lib/tomcat7/webapps[/code]

Your log files

Your log files should be here:

[code language=”bash”]/var/lib/tomcat7/logs[/code]

It should be called “catalina.out”. It’s useful to check here for troubleshoot Apache Tomcat. It doesn’t mean that tomcat’s successful start would mean the application to be working. catalina.out has saved me on several occasions.

Your tomcat7 configuration files

Server settings goes here:

[code language=”bash”]/etc/tomcat7/server.xml[/code]

You might find the other properties files useful as well:

[code language=”bash”]/etc/tomcat7[/code]

Your tomcat7 and catalina startup shell files

You might need to edit some .sh files like in my case to change Catalina options. You can find the shell scripts here:

[code language=”bash”]/usr/share/tomcat7/bin[/code]

You can read more on setting up Catalina environment options.

Other notes

If you know of other paths worth including, do post a comment. While this is documented more for personal use, I hope you can find it useful too!

How to set Catalina options in Ubuntu 12.10

This is more of a personal note and a really specific topic. This tutorial assumes you are using Ubuntu 12.10 or Ubuntu 12.04 and installed tomcat7 package. If you haven’t already installed tomcat7, use:

[code language=”bash”]sudo apt-get install tomcat7[/code]

After doing so, create and edit the file setenv.sh. Putting setenv.sh in CATALINA_BASE/bin allows you to keep your customizations separate.

[code language=”bash”]sudo nano /usr/share/tomcat7/bin/setenv.sh[/code]

I set my environment variables (in my case) as in setenv.sh:

[code language=”bash”]
CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"
[/code]

If the file is present, catalina.sh will pick it up and add to the runtime parameters.

To restart tomcat7, use:

[code language=”bash”]sudo service tomcat7 restart[/code]

Verifying that it works

After the server restart, run this to check:

[code language=”bash”]ps aux|grep jar[/code]

This should return:

[code language=”bash”]tomcat7 14140 63.8 11.4 4001076 918296 ? Sl 12:26 1:37 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed -classpath /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/var/lib/tomcat7 -Dcatalina.home=/usr/share/tomcat7 -Djava.io.tmpdir=/tmp/tomcat7-tomcat7-tmp org.apache.catalina.startup.Bootstrap start
kahwee 15564 0.0 0.0 13580 936 pts/1 S+ 12:29 0:00 grep jar[/code]

You should be able spot your line of customization. And that’s the proper way to setting up Catalina options. You can also set JAVA_OPTS through this method too.

In 1996, Larry Page made this post

This is posted in 1996:

From: Lawrence Page

I have a web robot which is a Java app. I need to be able to set the User-Agent field in the HTTP header in order to be a good net citizen (so people know who is accessing their server). Anyone have any ideas?

Right now, Java sends a request that includes something like:

User-Agent: Java/1.0beta2

I’d rather not rewrite all the HTTP stuff myself. I tried just searching in the JDK for the Java/1.0beta2 figuring I could just change the string, but I couldn’t find it. Perhaps it is stored as a unicode string?

An easy method of setting the User-Agent field should probably be added to Java, so people can properly identify their programs.

Thanks, Larry Page

A web robot? Guess what is it?

[via Guyro]

Oracle trying to make money with Java

This is a good move perhaps, one of the new features of Java 1.6 Update 14 is:

Garbage First (G1) Garbage Collector

Garbage First, or G1, is a low pause, server style collector. G1’s primary advantages over the Concurrent Mark-Sweep (CMS) collector include incremental compaction, better predictability and ease of use.

Although G1 is available for use in this release, note that production use of G1 is only permitted where a Java support contract has been purchased. G1 is supported thru Sun’s Java Platform Standard Edition for Business program. Source: Sun

I got really tired of Java updates. I realized it never just updates to the latest, instead it keeps all the old ones which I see no need for.

It’s exam time

I haven’t been blogging for a while as it’s exam period. It’s also been busy busy at 24seven. The new product which name you will never guess right.

Today’s paper is on programming, it’s (so-call) advanced concepts. Honestly the whole course’s been a mess but never mind that. I did 3 questions on Java, 2 question on SML and 1 question on Prolog. The Java questions are quite easy. Most of what I studied came out. I planned to do 3 questions out of the 5 Java questions and it went well.

What caught me in a surprise is Prolog which is quite hard, especially the last question which was basically some professor wanting to read some opinion. If I were a computing journalist, I probably choose that question.

SML isn’t that hard. What I studied came out, what I didn’t study I chose not to do. And I quickly went home and I try to see if the average function I wrote work in SML. And it did:

[code lang=”ocaml”]fun average x =
let
fun sum [] = 0.0
| sum (h::t) = h + sum t;
fun count [] = 0.0
| count (h::t) = 1.0 + count t;
in
sum x / count x
end;[/code]

I wrote this function very neatly on the paper. I never had so much sense of achievement writing an SML function.

An alternate way of writing would be probably:

[code lang=”ocaml”]fun average x = foldl op+ 0.0 x / real (length x);[/code]

You gotta love foldl sometimes.

Tomorrow’s paper is database. I probably won’t do too well for that. Lots of memorizing to do and my memory’s failing me.

IBM rumored to be buying Sun Microsystems at $6.5 billion

Bloomberg reports that IBM is interested in purchasing Sun Microsystems for $6.5 billion. This acquisition makes sense for IBM although I don’t think it’s worth paying that amount for this company. IBM offers numerous Java-based solutions I think. I don’t see much advantage for IBM in this merger.

IBM May Pay $6.5 Billion to Buy Sun Microsystems, WSJ Reports

International Business Machines Corp. is in talks to buy Sun Microsystems Inc., the Wall Street Journal reported, citing people familiar with the matter.

IBM may pay at least $6.5 billion, 100 percent more than Sun’s closing price of $4.97 a share on Tuesday, the report said, citing the people. An agreement may not be reached, the newspaper said.

An acquisition of Sun Microsystems would bolster IBM’s Internet, data storage, government and telecommunications business, the newspaper said, citing the people.

In recent months, Sun Microsystems has contacted a number of technology companies with the aim of being acquired, people familiar with the matter said, according to the newspaper. Hewlett-Packard Co. declined the offer, the newspaper reported, citing a person briefed on the matter. (Source: Bloomberg)

Sun has too much open-source products and failed to really monetize much of their software. Perhaps IBM can do something about it.

Security plays a less than 10% part of Microsoft

This is how important security is for Microsoft:

Security plays a less than 10 percent part of microsoft

See, it is clear that it’s less than 10%! Heroes happen {here} at Microsoft Singapore.

I don’t know why but every time I look at the Heroes happen {here} sticker I keep thinking of Java and I want to put the keyword return like this:

[code lang=”java”]Heroes happen() {
return here
}[/code]

That is to say “here” is of datatype Heroes which is declared in the field.