Skip to content

Instantly share code, notes, and snippets.

@linusfoldemo
Created February 25, 2015 07:38
Show Gist options
  • Save linusfoldemo/8fa87969d4a54010c15f to your computer and use it in GitHub Desktop.
Save linusfoldemo/8fa87969d4a54010c15f to your computer and use it in GitHub Desktop.
Start embedded tomcat
public static void start(final Environment environment, final int port) {
Environment.set(environment);
v
final Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
final File workDir = Files.createTempDir();
final Context context = tomcat.addContext("", workDir.getAbsolutePath());
context.addServletContainerInitializer(new SpringServletContainerInitializer(),
ImmutableSet.<Class<?>>of(Application.class));
try {
tomcat.start();
} catch (LifecycleException e) {
throw new IllegalStateException("Unable to start embedded tomcat", e);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
tomcat.stop();
FileUtils.deleteDirectory(workDir);
} catch (IOException | LifecycleException e) {
throw new IllegalStateException("Unable to stop tomcat", e);
}
}
});
tomcat.getServer().await();
}
public static class RunLocal {
public static void main(String[] args) {
start(Environment.local, 8080);
}
}
public static class RunDev {
public static void main(String[] args) {
start(Environment.dev, 8081);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment