Posts

Enabling GZIP compression on Tomcat

Enabling GZIP compression for your web application is a good way to accelerate your web site. This can reduce the size of data being transferred and, consequently, speed your application. Enabling compression on the web server will make data be transferred in compressed form. The browser will decompress the data on the fly, making the application faster. If your application uses Ajax, what usually increases the volume of data transferred from server to client, compression of data will significantly improve your application performance. Here is how to enable GZIP compression on Tomcat: edit file /conf/server.xml and add to the HTTP Connector configuration something like this: compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml" An example of a HTTP Connector configuration with compression enabled could be: Connector port="8080...

How to install Java in Fedora Core

By default, Fedora Core 6 systems come with an old Java software installed, so you need to install a newer version of Java Runtime Environment to enjoy all the Java applications out there. In this quick guide, I will teach you how to update/install your Java Environment. Let's begin by downloading the latest version of JRE (Java Runtime Environment) from here. Just click on the Download link where it says Java Runtime Environment (JRE) 5.0 Update, then you'll need to accept the license and download the Linux self-extracting file. WARNING: Please remember to always replace the xx from the jre-1_5_0_xx-linux-i586.bin file with the latest version. At the moment of this guide's writing, the latest version was 09, so the file should look like this: jre-1_5_0_09-linux-i586.bin After you have finished downloading the file, you need to move it into the /opt folder. Open a console and type: mv jre-1_5_0_xx-linux-i586.bin /opt Now, you will need to make this file executable so yo...

Opening a PDF file in a Java Application

PDF is a very common used file format for documents so quite often you need to develop applications that work with them. In Java it's fairly easy to open a PDF document using the runtime executer. Below you find a code snippet that does the job. Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file); This peace of code let's Windows deside what application to use to open the PDF file which is quite cool since most of the time you don't know what application users use to open PDF files. :-)

Remotely debugging JBoss

When working with JBoss sometimes it's needed to debug your code while running instead of running your application in a lightweight servlet container like Jetty or Tomcat. This can be done by adding the following line to your run.bat or run.sh: -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 You need to add this to your JAVA_OPTS variable so it will be used when JBoss starts up. When starting up now you can attach a debugger i.e. in Eclipse to the port you specified. :-)

Hibernate LazyInitializationException

People who use Hibernate will surely come across this irritating exception. In my whole career I have encountered this several times. Actually this problem is quite easily to fix. Let's start with when this exception can occur... Most of the time it has to do with a collection that is mapped to some objects that are lazily loaded. Lazy loading is used when you don't need the collection every time you retrieve the specific object. So i.e. you have an object called Person that can have multiple PhoneNumber objects. The objects will look something like this: public class Person { private String name; private Integer age; private Set phoneNumbers; ..... } public class PhoneNumber { private String areaCode; Publish Post private String number; private Person person; ..... } Having mapped the collection as lazy (Hibernate3 defaults to lazy) the call to the collection will cause an LazyInitializationException when you ...

Testing your application

Recently I finished a research about how to test an application. This was needed to have clear guidelines on how you should test your application and what kind of tools could be used for accomplishing that. The result of this research is spread out into the following paragraphs. What is testing? The definition of testing To keep it simple testing is making sure your application does what you expect it should do. This can be determined in different ways: - Unit isolation testing - Unit integration testing - Integration testing - Acceptance / functional testing Unit isolation testing Particularly useful when testing the logic of your application, like calculations etc. There shouldn’t be a lot of these kinds of tests in your code base. In other words, Mock tests. Goal The goal of this kind of test should be making sure the logic you have written doesn’t break and produces the right outcome. Unit integration testing Used for testing your logic in its context. So only use mocks ...

JVM Memory usage

During development of a J2EE application at my current employer I came across OutOfMemoryExceptions in the permGen space. The first thing we thought was that it was a memory leak in one of the applications where it occurred. After further investigation it seemed to be that we were pushing the limits of the JVM. The big problem was that we had 3 EAR files deployed on a JBoss server that by itself already uses a lot of memory. Deploying so many EAR files in 1 JBoss instance is definitely not recommended when using the default JVM configuration. If you crank up the JVM max perm gen space than it is possible to have such a configuration. But keep in mind that if you crank up the permGen space other parts of the JVM memory have less. A better solution is trying to integrate your applications if possible so the common libraries are not loaded multiple times. If this is not possible try to deploy them on separate servers or undeploy some of the default applications of JBoss to save memo...