Tuesday, April 12, 2011

Experience with GZIP compression of GWT JavaScript files

As your GWT module code is growing, the compiled JavaScript file is growing also. One of the common practises is Code Splitting, which splits GWT compiled code into smaller javascript files. Another option is to compress JavaScript using gzip compression. This post deals with my experience about gzip compression using Apache Tomcat 6.
The size of my GWT modules grow up to few megabytes. Not a large size at all, but could be a damn problem with downloading on slower network links. So download time could take few minutes. That's no good.
One of the Tomcat feature is gzip response encoding. By the Tomcat 6 reference documentation this could be done by adding compression parameter into server.xml. Pay attention on note paragraph; failing which you could be surprised, why files above 49 kB will not be compressed. There is another important attribute, compressableMimeType,which I also used and add text/javascript MIME type. And that's enought about configuration. All you have to do is restart Tomcat and clear browser cache.
And what was the result? Gzip compression reduced JavaScript file size from 2,515 KB to 236 kB! Besides code splitting, gzip compression is another way to reduce size of GWT compiled files. Problem could be server CPU load due to on the fly compression of files during many user requests.

Sunday, January 2, 2011

How to setup Java + Tomcat + MySQL on Amazon EC2

As you may know, by the 1st November of 2010, Amazon launched Free Tier for new users to getting help started with some of their services. The best things in life are for free, I used to speak, so I decided to try it. I am working on smaller Java web application, which is based on Spring 3 and I need some (free) service for hosting my application on a public web. My app needs relational database for persisting data, and access to filesystem for file storage, where file size counts up to hundreds of megabytes. In the beginning I was curious, if 613 MB of memory will be enough for my web app. I found that it was sufficient, and also performs very well.
This article guides you to setup of your own infrastucture. I am still using OS Windows, but few things you need to do, e.g. SSH connection to your EC2 image are simplier to do in Linux. So let's go!

Sign in to Amazon WS and launch image

First of all, you need to create your personal account on Amazon WS, if you don't have any. I signed in to S3 and EC2 services to achieve my application's requirements. After successful registration, I followed a great AWS Management Console, where you can manage your EC2 instances, S3 bucks and other Amazon services. I launched Amazon Linux 32-bit image on Micro Instance. Note, that there are also many preconfigured Community images with complete Java, Tomcat and MySQL stack. Images based on OS with its own GUI (e.g. Ubuntu Desktop, or Windows) allows you to connect thru remote desktop, which is also interesting possibility. I decided to use simpliest image to do the things my way. Amazon Linux AMI comes with preinstalled Open JDK 6. Also note, you have to select ESB based image, if you want to use free tier. I used Putty for SSH connection to my image. Documentation is available in Geeting Started Chapter of EC2 Documentation.

Install Java

Amazon Linux AMI comes with preinstalled Open JDK 6. I changed JAVA_HOME variable for Tomcat, which now points to JDK instead of preconfigured JRE.

Install Tomcat

Amazon Linux AMI is similar to CentOS, so I used this article for installing Tomcat 6 (or other version).
If you want connect to your Tomcat application from public web, don't forget to allow port 8080 (or other depends on your configuration) in Security Group Section of AWS Management console.
I also installed Tomcat native library with following command:

sudo yum install tomcat-native.i686

Install MySQL

Well, this was the funniest part of setup :) I followed these four simple commands for whole MySQL setup.

Conclusion

As you can see, setup your own image with Java stack on Amazon EC2 is not complicated. Next step could be create a script for automatic deploy of WAR application on demand. First, you would upload your application to some server with absolute and reachable URI. The other option is availability of your application on web as a result of automated build. In this case, script could contains commands for download a war archive from public web thru curl tool and copying it to a tomcat webapps folder. If you are using Eclipse IDE, there is AWS Toolkit available, which also looks promising.