Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Avoiding file-locking when developing with embedded Jetty

Hi

I would like to hear if someone could suggest how to scratch this itch I often have when using Jetty embedded in applications.

I am often using Jetty during development. I'd like for static files that are normally packaged into a war or jar file to be easy to edit when I am developing (that is, when the files are not packaged). At the same time, I want my code to be as similar as possible during development and in production.

My best attempt so far has been to place the static content under src/main/resources/webapp and package it into the Jar-file.

In order to avoid locking the files when I'm running the server in the debugger, I've implemented the following:

    public static WebAppContext createApplicationContext() {
        WebAppContext webapp = new WebAppContext("/webapp", "/app");

        if (SimpleServer.class.getResource(webapp.getWar()).getProtocol().equals("file")) {
            // Avoid locking static content when running exploded
            webapp.setWar("src/main/resources/webapp");
            webapp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
        }
        return webapp;
    }

This runs in a main method like so:

public class SimpleServer {

    public static void main(String[] args) throws Exception {
        HandlerList handlers = new HandlerList();
        handlers.addHandler(new ShutdownHandler("randomtoken", false, true));
        handlers.addHandler(createApplicationContext());

        Server server = new Server(5000);
        server.setHandler(handlers);
        server.start();

        System.out.println("Started " + server.getURI());
    }
}

As the rest of the code is extremely simple, the magic replacement of the target file with the source file and the setting of the very poorly documented "org.eclipse.jetty.servlet.Default.useFileMappedBuffer" parameter both feel really frustrating. The code is magic enough that I've ended up creating a "framework" to run it which is clearly not what I want.


1. Are there currently better ways of doing this?
2. Is there any way something that accomplishes the same could be added to Jetty itself?


~Johannes



Back to the top