Posts

Showing posts with the label java

Configure Tomcat as a deamon on Linux

Since I keep on forgetting how to do this I decided to put this on my blog to make it easier for me and other people that have the same issue as me. It's pretty simple actually but yesterday I caught myself forgetting how it needs to be done. Basically you need to follow to following steps to successfully. Step 1 - Creating startup deamon file You can take the following file and adjust it accordingly. You have to create this file in /etc/init.d/. #Jenkins startup script #!/bin/sh -e #Jenkins startup script #chkconfig: 2345 80 05 #description: Jenkins CI # Define some variables # Name of app APP=Jenkins # Name of the user to run as USER=tomcat # Location of application's bin directory BASE=/var/tomcat/instances/jenkins # Location of Java JDK export JAVA_HOME=/opt/java/jdk1.6.0_11 case "$1" in # Start command start) echo "Starting $APP" /bin/su -m $USER -c "cd $BASE/logs && $BASE/bin/startup.sh &> /dev/null" ;; # Stop...

Cargo-itest extended

I have been doing some work on the cargo-itest project and extended it with the following features: - JOnas support - Jetty support - Auto detection of configuration files for JBoss - Extended logging information to be able to detect problems in an easier way These changes will be in the next release 1.2.0. The auto detection is a nice one since it automatically picks up .properties files and -ds.xml (Data source files) from a preconfigured location so you don't have to copy them selves to the correct location. :-) In the future we will try to implement configuration by annotation since some users prefer doing configuration through annotation instead of an XML file. In this case a Spring context file.

Shared configuration on Tomcat 6

In Tomcat 5 there was the famous ${catalina.home}/shared/classes which you could use for sharing class files, properties files etc. Tomcat 6 by default doesn't come with this directory. In a first glance it seems that you can't do it anymore :-(.. But that's not true :-).. You need to configure Tomcat to point to a directory which you want to be shared location so applications can pick up resources from the classpath. The configuration is very simple. Just go to ${catalina.home}/conf and open catalina.properties. Add the path to the  shared.loader property. It will look something like this: # # List of comma-separated paths defining the contents of the "shared"  # classloader. Prefixes should be used to define what is the repository type. # Path may be relative to the CATALINA_BASE path or absolute. If left as blank, # the "common" loader will be used as Catalina's "shared" loader. # Examples: #     "foo": Add this folder as a cla...

Fastest XML parser

In most applications you need to do some XML parsing to either provide XML to external systems or parse XML from external systems or of course both. Since the systems I work on use a lot of XML and they need to be as fast as possible I did some tests on several XML parsers. I tested the following parsers/XML frameworks: JDom Piccolo StaxMate XStream JAXB 2.1 All parsers were tested in isolation with the following parameters: 25 threads running simultaniously Every thread calls the parser 100 times The results are as follows. JDom Avg. parsing time: 325.24 ms Avg. memory usage: 307 KB Piccolo Avg. parsing time: 88.08 ms Avg. memory usage: 454 KB StaxMate Avg. parsing time: 96.16 ms Avg. memory usage: 203 KB XStream Avg. parsing time: 77.04 ms Avg. memory usage: 319 KB Jaxb 2.1 Avg. parsing time: 1778.12 ms Avg. memory usage: 618 KB As you can see XStream is the fastest one of them all. The total ranking of the parsers you can find below: XStream Piccolo StaxMate JDom JAXB 2.1 ...

Cargo-itest continues

The 0.3 version of my cargo-itest project now also supports Tomcat. So now Tomcat users can also write integration tests with fairly little effort. I am also extending it with an AbstractDefaultHibernateDeploymentTest which can be used to easily generate your database schema from your annotated classes. Before it wasn't possible to execute SQL scripts for test data. Now I added the functionality to enable you to execute SQL scripts before the deployment tests are run and also after. The execution of the SQL scripts will be before and after transaction so it will not be rolled back by the test case itself. Internally it uses the AbstractTransactionalJUnit4SpringContextTests but you only need to configure a list of file names in your itest-context.xml file which in turn will automatically be picked up by the test. Also added support for applications that don't use a database at all with the AbstractDefaultNoDbDeploymentTest class. In the near future I will also implement containe...

Cargo-itest open source project

I recently started an open source project called cargo-itest. This was started up because when I worked on several projects I needed to create integration tests for them which all had similarities. I was previously doing it with the maven2 cargo plug-in but wasn't really fond of it because it most of the times didn't work and when it worked it stopped working after editing the configuration. In other words it was very unstable. This initiated me to start to use the java API of Cargo instead of the maven plugin. By using the java API it already removes the dependency from your build system in this case Maven. Having no dependency on the build system gives you a couple of advantages: You can run it in any IDE that supports JUnit. You can switch easily to another build system without changing your tests and test configuration. These are general advantages of just using the API but also part of using my open source project cargo-itest. This project enables you to easily start creat...

Specify context name in Tomcat

When developing web applications that are distributed as WAR files running in Tomcat most of the times you want to be able to specify the context name to a nicer name than Tomcat provides by default. Which is the name of the WAR file itself. When i.e. you are keeping the version within the name of the WAR this will result in some weird name like myapp-1.2. This is most of the times not what you want. Most of the times you want a nice context name like myapp without the version in it. According to the Tomcat documentation you can do it in 3 ways: Edit the server.xml Add a context.xml to the $CATALINA_HOME/conf/Catalina/localhost with the name of your context. Add a context.xml to your META-INF directory in your WAR file The first two options just work fine only the last one doesn't do the job for you for some reason. Personally the second option is the best option since it's not intrusive to your application and also not to Tomcat since you leave the server.xml alone. This way y...

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. :-)

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...

The Google Webtoolkit

Developing web applications in Java for a couple of years now the big annoyance part always was the GUI part. Making it work in every browser, implementing AJAX, etc.. AJAX is hot nowadays and when demands are rising I started looking for an easy to use AJAX framework to build a web application GUI in. I came across many frameworks like YUI (Yahoo User Interfaces), DWR in combination with spring MVC, Wicket and many more. The one that stood out the most was GWT (Google Web Toolkit). The nice thing about this framework is that you only code in Java :-). No more HTML, Javascript etc.. But if you really want to use HTML and Javascript (In some specific cases) you can, so full flexibility is available. How does it work than? Your package structure needs to have a package called ...client and ...server. All code that is written in the client application is compiled to javascripts and HTML files are created where needed. The javascript that is created is compliant to the most used brow...