Last active
August 29, 2015 14:02
-
-
Save abn/98aa0cd4c8b303fdd36c to your computer and use it in GitHub Desktop.
Embedded Jetty PoC Quickstart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with this | |
* work for additional information regarding copyright ownership. The ASF | |
* licenses this file to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
import javax.servlet.http.HttpServlet; | |
import org.eclipse.jetty.server.HttpConfiguration; | |
import org.eclipse.jetty.server.HttpConnectionFactory; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
import org.eclipse.jetty.servlet.ServletHolder; | |
/** | |
* | |
* Embedded Jetty PoC Quickstart | |
* | |
* Dependencies: | |
* org.eclipse.jetty:jetty-server:9.2.1.v20140609 | |
* | |
* @author Arun Babu Neelicattu (abn) | |
* | |
*/ | |
public class EmbeddedJettyServer { | |
private static final Server server; | |
private static final ServletContextHandler rootContext; | |
private static boolean RUNNING = false; | |
private static final Integer PORT = 8080; | |
static { | |
System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); | |
server = new Server(); | |
rootContext = new ServletContextHandler(ServletContextHandler.SESSIONS); | |
rootContext.setContextPath("/"); | |
server.setHandler(rootContext); | |
} | |
public static boolean isRunning() { | |
return RUNNING; | |
} | |
public static Integer getPort() { | |
return PORT; | |
} | |
public static ServletContextHandler getRootContext() { | |
return rootContext; | |
} | |
public static void addServlet(ServletContextHandler context, | |
HttpServlet servlet, String pathSpec) { | |
getRootContext().addServlet(new ServletHolder(servlet), pathSpec); | |
} | |
public static void addServlet(HttpServlet servlet, String pathSpec) { | |
addServlet(getRootContext(), servlet, pathSpec); | |
} | |
public static void setHandler(ServletContextHandler context) { | |
server.setHandler(rootContext); | |
} | |
public static void startJetty() { | |
try { | |
HttpConfiguration http_config = new HttpConfiguration(); | |
http_config.setOutputBufferSize(32768); | |
ServerConnector http = new ServerConnector(server, | |
new HttpConnectionFactory(http_config)); | |
http.setPort(getPort()); | |
http.setIdleTimeout(30000); | |
server.addConnector(http); | |
server.start(); | |
RUNNING = true; | |
} catch (Exception ex) { | |
System.err.println(ex); | |
} | |
} | |
public static void stopJetty() throws Exception { | |
server.stop(); | |
RUNNING = false; | |
} | |
public static void main(String[] args) throws Exception { | |
startJetty(); | |
stopJetty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment