Sunday, March 13, 2011

Configuring Jetty 7 programatically using Wicket and Spring

UPDATE: I don't know why I couldn't get this running before (maybe the example in the documentation was updated?) but I got a Wicket servlet filter application working in an embedded Jetty 7 server that was configured using the existing web.xml. This is all you need prior to calling start().

Server server = new Server();
server.setStopAtShutdown(true);
server.setGracefulShutdown(ALLOWED_SHUTDOWN_TIME);

SocketConnector connector = new SocketConnector();
connector.setPort(8080);
server.addConnector(connector);

WebAppContext context = new WebAppContext();
context.setDescriptor("config/web.xml");
context.setResourceBase("build");
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);

Anyway, here's the rest of my original post:


I recently spent some time beating my head against a rock, trying to get my Wicket- and Spring-based web application running in an embedded Jetty 7 server. Running an embedded server through the debugger is a convenient way to debug a web application. And if you configure it to run the application from the project's build directory, it also has the added benefit of picking up changes on the fly.

Anyway, although I haven't seen any announcements about it, it seems that Jetty was adopted by (or is at lease now in cahoots with) Eclipse, as of version 7. The problem is that the API has changed a fair amount and updated examples are hard to come by. I've even had trouble with some of the examples on the Jetty site.

However, with a bit of tinkering, I came up with the code below. It adheres to the recommended practice of using the Wicket servlet filter instead of the Wicket servlet. And in this case, I am using the SpringWebApplicationFactory to configure my application through Spring.

public class JettyServer {
 private static int DEFAULT_MAXIMUM_IDLE_TIME = 1000*60*60;
 private static int ALLOWED_SHUTDOWN_TIME = 1000*5;

 public static void main(String[] args) {
  Server server = new Server();
  server.setStopAtShutdown(true);
  server.setGracefulShutdown(ALLOWED_SHUTDOWN_TIME);
  
  SocketConnector connector = new SocketConnector();
  connector.setPort(8080);
  // Settings from the Wicket quickstart archetype...
  connector.setMaxIdleTime(DEFAULT_MAXIMUM_IDLE_TIME);
  connector.setSoLingerTime(-1);
  server.addConnector(connector);

  ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
  servletContextHandler.addServlet(DefaultServlet.class, "/");
  servletContextHandler.setAttribute("contextConfigLocation","classpath:spring/spring-config.xml");
  server.setHandler(servletContextHandler);

  FilterHolder filterHolder = new FilterHolder(new WicketFilter());
  filterHolder.setInitParameter("applicationFactoryClassName", "org.apache.wicket.spring.SpringWebApplicationFactory");
  servletContextHandler.addFilter(filterHolder, "/*", FilterMapping.REQUEST);

  servletContextHandler.getInitParams().put("contextConfigLocation", "classpath:spring/spring-config.xml");
  servletContextHandler.addEventListener(new ContextLoaderListener());

  try {
   server.start();
   System.out.println("Started Jetty.  Press [return] to shutdown.");
   System.in.read();
   System.out.println("Stopping Jetty..."); 
   server.stop();
   server.join();
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(100);
  }
 }
}

No comments:

Post a Comment