Skip to content

Instantly share code, notes, and snippets.

@ygaller
Created July 24, 2017 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygaller/17cde9e0d9750b062c8218c92df31790 to your computer and use it in GitHub Desktop.
Save ygaller/17cde9e0d9750b062c8218c92df31790 to your computer and use it in GitHub Desktop.
public class ApplicationMain {
public static void main(String[] args) {
get("/hello", (request, response) -> "world");
}
}
public class ApplicationMain {
public static void main(String[] args) {
Logger logger = Logger.getLogger(ApplicationMain.class);
SparkUtils.createServerWithRequestLog(logger);
get("/hello", (request, response) -> "world");
}
}
public class EmbeddedJettyFactoryConstructor {
AbstractNCSARequestLog requestLog;
public EmbeddedJettyFactoryConstructor(AbstractNCSARequestLog requestLog) {
this.requestLog = requestLog;
}
EmbeddedJettyFactory create() {
return new EmbeddedJettyFactory((maxThreads, minThreads, threadTimeoutMillis) -> {
Server server;
if (maxThreads > 0) {
int max = maxThreads > 0 ? maxThreads : 200;
int min = minThreads > 0 ? minThreads : 8;
int idleTimeout = threadTimeoutMillis > 0 ? threadTimeoutMillis : '\uea60';
server = new Server(new QueuedThreadPool(max, min, idleTimeout));
} else {
server = new Server();
}
server.setRequestLog(requestLog);
return server;
});
}
}
public class RequestLogFactory {
private Logger logger;
public RequestLogFactory(Logger logger) {
this.logger = logger;
}
AbstractNCSARequestLog create() {
return new AbstractNCSARequestLog() {
@Override
protected boolean isEnabled() {
return true;
}
@Override
public void write(String s) throws IOException {
logger.info(s);
}
};
}
}
public class SparkUtils {
public static void createServerWithRequestLog(Logger logger) {
EmbeddedJettyFactory factory = createEmbeddedJettyFactoryWithRequestLog(logger);
EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, factory);
}
private static EmbeddedJettyFactory createEmbeddedJettyFactoryWithRequestLog(org.apache.log4j.Logger logger) {
AbstractNCSARequestLog requestLog = new RequestLogFactory(logger).create();
return new EmbeddedJettyFactoryConstructor(requestLog).create();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment