Created
July 24, 2017 19:31
-
-
Save ygaller/17cde9e0d9750b062c8218c92df31790 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ApplicationMain { | |
public static void main(String[] args) { | |
get("/hello", (request, response) -> "world"); | |
} | |
} |
This file contains hidden or 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
public class ApplicationMain { | |
public static void main(String[] args) { | |
Logger logger = Logger.getLogger(ApplicationMain.class); | |
SparkUtils.createServerWithRequestLog(logger); | |
get("/hello", (request, response) -> "world"); | |
} | |
} |
This file contains hidden or 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
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; | |
}); | |
} | |
} |
This file contains hidden or 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
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); | |
} | |
}; | |
} | |
} |
This file contains hidden or 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
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