Saturday, January 10, 2009

Jetty embedded WEB server offers GZIP compression out of the box

Jetty is an Open Source WEB server written Java that you can embed in your application. You can find out more about it here.

It's very useful as an a way to serve a browser based control panel for an appliance, desktop system tray or windows service application that generally runs unattended.
image
Browser based control panels are particularly useful if the application is running in a dark room with no direct access.

My ADSL modem has a good looking, intuitive menu based control panel and if I had a manual it's long lost.
image

"One key benefit of such a solution (written in Java) is that static HTML, XML, JavaScript and CSS files can all be included within the application jar file where they can't be tampered with by the casual user."

An AJAX based solution gives users a nice ride and quick feedback but the the EXT/JS framework that I use has some pretty big js, css, and image files to slow things up.

If you've read my posts about GZIP compression (Safari Browser provides Web Inspection Tool, Use Fiddler to explore HTTP GZIP compression and Fiddler helps you analyse HTTP traffic) you know that download bandwidth can be improved dramatically. This can improve download speed.

  1. Before Using this code 
    imagethe download cost 675KB & 1.46 seconds
  2. After using the the GzipStream instead
    image the download cost 203KB & 1.37 seconds

A saving of 472KB but only 90ms, probably because it's running on localhost where the operating system is passing data via pipes and streams.
The speed improvement on LAN or WWW would probably be significant.

Jetty makes the decisions

Of course we could GZIP the files and include them in the JAR file but the variations in browsers make decision making about which file to serve pretty difficult.

Doing it on the fly makes sense, particularly if your service up large dynamic data files or images.

Here's the code for you to cut:

byte[] b = readResource(uri);
OutputStream os = response.getOutputStream();
// GzipStream os = new GzipStream(request, response, b.length, 8192, 100);
os.write(b);
os.close();
if (uri.endsWith(".js")) {
   response.setContentType("text/javascript");
} else {
   String s = mt.getMimeByExtension(uri).toString();  
   response.setContentType(s);
}
response.setStatus(status);

Another improvement would be to set the last modified header.

This code does not work:
image because f.lastModified() returns 0.
Maybe because it's a resource.

When we go live, we can use the date on the jar file, but that's no good during debugging.

I'll post the solution when I find it.

No comments: