Skip to content
JakeHoward edited this page Jul 16, 2015 · 1 revision

In this section we show you how to run Utterlyidle applications using either embedded web servers or servlet containers.

Running your application as a service from a main method and using an embedded web server has a lot of advantages. You can run your application exactly the same way in development and in production. You don't have to configure and maintain a complex application server and you can avoid lots of problems with PermGen and ClassLoaders.

HTTP Server comes with Java 6 and no extra dependencies are required.

Application application = ApplicationBuilder.application().addAnnotated(HelloResource.class).build();
ServerConfiguration configuration = ServerConfiguration.defaultConfiguration();
new com.googlecode.utterlyidle.httpserver.RestServer(application, configuration);

Nothing beats built-in HTTP server when it comes to fast startup times so it's our recommended server for automated acceptance testing.

You can override default configuration by specifying explicit host, port, base path and maximum number of threads.

ServerConfiguration configuration = ServerConfiguration.defaultConfiguration().
        bindAddress(InetAddress.getByName("192.168.1.103")).
        port(8000).
        basePath(BasePath.basePath("base")).
        maxThreadNumber(10);

If you want to use Simple framework you have to include the following dependency:

mvn:org.simpleframework:simple:jar:4.1.21

Application application = ApplicationBuilder.application().addAnnotated(HelloResource.class).build();
ServerConfiguration configuration = ServerConfiguration.defaultConfiguration();
new com.googlecode.utterlyidle.simpleframework.RestServer(application, configuration);

If you want to use embedded Jetty you have to include the following dependencies:

org.mortbay.jetty:jetty-util:jar:6.1.26

org.mortbay.jetty:jetty:jar:6.1.26

Application application = ApplicationBuilder.application().addAnnotated(HelloResource.class).build();
ServerConfiguration configuration = ServerConfiguration.defaultConfiguration();
new com.googlecode.utterlyidle.jetty.RestServer(application, configuration);

Servlet containers

Using embedded HTTP servers lets you create self-contained applications that can be started from a main method with a simple script. However if you're stuck with one of the Servlet containers or you would be missing some of the features they provide, you can still use Utterlyidle. Just add the Utterlyidle servlet to web.xml and declare your REST application as an init parameter:

<servlet>
    <servlet-name>ApplicationServlet</servlet-name>
    <servlet-class>com.googlecode.utterlyidle.servlet.ApplicationServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
       <param-name>application</param-name>
       <param-value>com.mycompany.MyRestApplication</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>ApplicationServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Clone this wiki locally