Tuesday, February 7, 2012

Hosting Multiple Domains On A Single Apache HTTPD / Tomcat Server Combination

This is a quick and easy guide on how to deploy multiple domains on a single Tomcat sever fronted by a single Apache HTTPD server. I am assuming you already have installed mod_jk and got the communication between Apache and Tomcat working.

First the Tomcat changes ( I am using Tomcat 7 but it should work the same in Tomcat 6 too). Edit server.xml file in conf directory and scroll down until you see the <Host> section there. Copy the host section and paste it again so you have two host sections and modify the name attributes to contain the two domain names. Also point appBase attributes to two different folders (create them under the Tomcat directory). You will deploy your apps to the ROOT folders under each appbase directory so that they can be accessed as http://www.domain1.com and http://www.domain2.com


workers.properties can be as simple as following two lines. Which connects to the worker w1 to ajp port of Tomcat.

worker.list=w1
worker.w1.port=8009


Now to the Apache 2 configuration. Make sure you have uncommented NameVirtualHost *:80 line in httpd.conf. Now add two <VirtualHost> sections for the two domains as below. Note that I have left out other mod_jk configuration directives.

<VirtualHost *:80>
    ServerName www.domain1.com
    JkMount  /* w1
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domain2.com
    JkMount  /* w1
</VirtualHost>


Don' forget to click the +1 button below if this post was helpful.

JDBC Driver Class Names and URL Formats For Popular Databases

A recurring pain for me is digging up JDBC driver class names and URL formats when ever I need to connect to a database over JDBC. So I thought I will document driver / URL details plus additional info such as Spring DBCP configuration, Tomcat datasource configuration and connection validation SQL information for all popular databases.

I have published this info on the internet and you can view them at http://www.kengsi.com/jdbcwiz.html. Hope this will help few others by having this information in one single place. I will add more databases as time goes on so if you don't see information about a database driver you are interested in, please do let me know.


Don' forget to click the +1 button below if this post was helpful.

Friday, January 27, 2012

Quick and Simple LRU Cache in Java

Many times I have come across the need to cache values where using a fully fledged caching library like EHCache is an overkill. All I want is a bounded Map of key / value pairs with an LRU eviction policy. There are few options mentioned on the web and few common ones are

  • LinkedHashMap from JDK
Example from here. This is not really LRU but removing the oldest entry added to the map when it reaches the capacity. Also LinkedHashMap is not thread safe and as you can see in the example you need to wrap it with Collections.synchronizedMap which makes every get / put method to lock the map. This will not perform that well in a multi-threaded environment.

This uses LRU algorithm to evict objects but again is not thread safe and you need to wrap it in a synchronized map interface as in the LinkedHashMap's case which will incur a performance penalty on muti-threaded access.


This open source implementation is the one I finally settled on. It's easy to use as you can construct the cache in a single line, thread safe and doesn't seem to have a big performance hit due to locking in muti-threaded apps. According to the diagram on the main page, get and put performance seems to be comparable to ConcurrentHashMap from the JDK.


As you can see it's pretty simple to create a cache and you can use the put an get methods from the Map interface to add and retrieve entries from the cache

Don' forget to click the +1 button below if this post was helpful.

Apache HTTPD Redirect Root URL to Sub Context / Directory

Yesterday I had to front an application running on Tomcat with Apache HTTPD server and one of the requirements was to automatically redirect the root URL to the Tomcat's web app context

e.g. http://www.mycompany.com to http://www.mycompany.com/application

Sounded pretty simple and did a Google search, went through few pages that had examples on how to do it but none of them worked. Most involved .htaccess files and using RewiteCond /RewriteRule combinations.

I was getting frustrated with finding a simple solution for the simple problem and after about an hour of searching and trying things I finally found the simple solution that worked. All it needed was the following single line in your VirtualHost section.

RedirectMatch ^/$ /application/

That's it and it will redirect all requests to root URL to the specified sub directory or sub context.

Don' forget to click the +1 button below if this post was helpful.

Thursday, January 26, 2012

Few Easy Steps to Install Sun/Oracle JDK on EC2 Linux

I use the Amazon Linux AMI on all my EC2 instances and they come with OpenJDK IcedTea pre installed on them. I have had few issues with OpenJDK when running some applications and I always install the Sun/Oracle JDK 6 on them. Below are the few easy steps / commands to download and install JDK 6 once you log in to the EC2 instance using an SSH client.

Update:
Oracle doesn't provide direct download URLs for JDKs anymore. I have updated the steps with the trick mentioned here to get the installation file with wget

Run java -version to make sure you have the Sun/Oracle JDK as your default jvm now.



Don' forget to click the +1 button below if this post was helpful.

Monday, January 23, 2012

Servlet Filter to Set Response Headers

Frequent requirement when developing Java web apps is to set different response headers based of on content type / URL. Last project it was to set cache headers on dynamic URLs so they will never be cached and today I had to do the opposite for static content, where cache headers were set on static content so they will be cached for a very long time by the browsers.

It's basically the same thing but just setting different response header values. So I decided to write a general purpose Servlet filter to achieve this in a few lines.



We configure the filter in web.xml and set the required response header / value pair as init params


Here we are setting the Cache-Control response header and setting the expiry date to 1 year from request time. Now we add the appropriate filter mapping to apply the filter to static content we are interested in.


And we are done. In summary all we are doing here is setting cache headers to all static content like .js, .css etc... so they will be cached by the client browsers for a very long time making the pages load faster and reducing the bandwidth usage / load on the server significantly.

Please note you shouldn't add far future cache expiration dates if you plan to make changes to the static content at any time as the modified content might not be downloaded by client browsers. The strategy we take is to make a new copy of the file when we modify and link to the new file. e.g. style-1.1.css when modified will be made site-1.2.css,




Don' forget to click the +1 button below if this post was helpful.