Skip to content

Instantly share code, notes, and snippets.

@jfarcand
Created February 5, 2015 16:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfarcand/08fd1fd00a99041dd03a to your computer and use it in GitHub Desktop.
Save jfarcand/08fd1fd00a99041dd03a to your computer and use it in GitHub Desktop.
Jetty 9 with Atmosphere
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
Main main = new Main();
main.run();
}
private void run() throws Exception {
Server server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
http_config.addCustomizer(new ForwardedRequestCustomizer());
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
http.setPort(8080);
http.setIdleTimeout(30000);
server.setConnectors(new Connector[]{http});
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
resource_handler.setResourceBase("/Users/jfarcand/workspace/atmosphere-samples/samples/websocket-chat/src/main/webapp");
ServletHolder atmosphereServletHolder = new ServletHolder(AtmosphereServlet.class);
atmosphereServletHolder.setInitParameter(ApplicationConfig.ANNOTATION_PACKAGE, "org.atmosphere.samples.chat");
atmosphereServletHolder.setInitParameter(ApplicationConfig.WEBSOCKET_CONTENT_TYPE, "application/json");
atmosphereServletHolder.setInitParameter(ApplicationConfig.PROPERTY_COMET_SUPPORT, Jetty9AsyncSupportWithWebSocket.class.getName());
atmosphereServletHolder.setAsyncSupported(true);
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.addServlet(atmosphereServletHolder, "/chat/*");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler });
server.setHandler(handlers);
server.setStopAtShutdown(true);
server.start();
server.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment