Friday, May 20, 2011

add static resource directories to jetty-maven-plugin 7.x

When using jetty-maven-plugin during development of a web application with the command mvn jetty:run. I need to use some static resources which I DO NOT want to put in src/main/webapp. I put DEV resources into src/test/webapp instead. But jetty does not load these resources by default. So the following jetty-maven-plugin configuration show how to provide static resources.

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <configuration>
        <webAppConfig>
        ...
            <resourceBases>
                <resourceBase>src/main/webapp</resourceBase>
                <resourceBase>src/test/webapp</resourceBase>
            </resourceBases>
        ...
        </webAppConfig>
    </configuration>
</plugin>

Tuesday, May 10, 2011

memory leak when doing redeploy with jetty

When using the mycila-testing-jetty plugin for webapp integration tests, I encounter memory leak problem with the infamous OutOfMemoryException (OOM) : PermGen Space.

You can have a OOM on heap space and permanent generation (PermGen) space in your JVM.
The heap space is mainly for short term and mid term memory allocation. The permanent generation space, as named, is for long term object such as class object which are loaded by classLoader and retained during the whole life of the JVM.

But in case of webapp running in a servlet container such as jetty, you sometime need to undeploy and redeploy your application to apply code change. With the mycila-testing-jetty plugin you can choose to either restart your server, redeploy your webapp or keep them running.

During my tests, restarting the server or keeping them runnning them was OK. But I'd always got OOM when keeping the jetty server running and only doing redeploy of the webapp. So I've track down the leak using Eclipse Memory Analyzer and jvisualvm tools.

The error come from how jetty keep sessions.

The problem is that session created during the lifetime of the webappContext are not invalidated on undeploy and so stay in the Server._sessionIdManager keeping all the webappContext class and making OutOfMemoryException PermGen Space happening.

SessionIdManager only invalidate session if the webapp ask it to. I think that except for cluster session management, when undeploying a webappContext then all the session created by it must be invalidated and cleared.

To work around, you can change the server sessionIdManager before redeploying the new webappContext :

server.setSessionIdManager(new HashSessionIdManager());


Bug issue : memory leak when doing successive redeploy of webappContext due to not invalidated session in server

Thursday, May 5, 2011

Java WAR execution source code location

this.getClass().getProtectionDomain().getCodeSource().getLocation();