Bien que ce poste est en quelque âge, je suis répondu à ma propre réponse car il pourrait sauver une autre » temps
package com.creativefella;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Embedded;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TomcatServer {
private Embedded server;
private int port;
private boolean isRunning;
private static final Logger LOG = LoggerFactory.getLogger(TomcatServer.class);
private static final boolean isInfo = LOG.isInfoEnabled();
/**
* Create a new Tomcat embedded server instance. Setup looks like:
* <pre><Server>
* <Service>
* <Connector />
* <Engine>
* <Host>
* <Context />
* </Host>
* </Engine>
* </Service>
*</Server></pre>
* <Server> & <Service> will be created automcatically. We need to hook the remaining to an {@link Embedded} instnace
* @param contextPath Context path for the application
* @param port Port number to be used for the embedded Tomcat server
* @param appBase Path to the Application files (for Maven based web apps, in general: <code>/src/main/</code>)
* @param shutdownHook If true, registers a server' shutdown hook with JVM. This is useful to shutdown the server
* in erroneous cases.
* @throws Exception
*/
public TomcatServer(String contextPath, int port, String appBase, boolean shutdownHook) {
if(contextPath == null || appBase == null || appBase.length() == 0) {
throw new IllegalArgumentException("Context path or appbase should not be null");
}
if(!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
this.port = port;
server = new Embedded();
server.setName("TomcatEmbeddedServer");
Host localHost = server.createHost("localhost", appBase);
localHost.setAutoDeploy(false);
StandardContext rootContext = (StandardContext) server.createContext(contextPath, "webapp");
rootContext.setDefaultWebXml("web.xml");
localHost.addChild(rootContext);
Engine engine = server.createEngine();
engine.setDefaultHost(localHost.getName());
engine.setName("TomcatEngine");
engine.addChild(localHost);
server.addEngine(engine);
Connector connector = server.createConnector(localHost.getName(), port, false);
server.addConnector(connector);
// register shutdown hook
if(shutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if(isRunning) {
if(isInfo) LOG.info("Stopping the Tomcat server, through shutdown hook");
try {
if (server != null) {
server.stop();
}
} catch (LifecycleException e) {
LOG.error("Error while stopping the Tomcat server, through shutdown hook", e);
}
}
}
});
}
}
/**
* Start the tomcat embedded server
*/
public void start() throws LifecycleException {
if(isRunning) {
LOG.warn("Tomcat server is already running @ port={}; ignoring the start", port);
return;
}
if(isInfo) LOG.info("Starting the Tomcat server @ port={}", port);
server.setAwait(true);
server.start();
isRunning = true;
}
/**
* Stop the tomcat embedded server
*/
public void stop() throws LifecycleException {
if(!isRunning) {
LOG.warn("Tomcat server is not running @ port={}", port);
return;
}
if(isInfo) LOG.info("Stopping the Tomcat server");
server.stop();
isRunning = false;
}
public boolean isRunning() {
return isRunning;
}
}
J'ai aussi fait face à l'erreur 404
et eu du mal un certain temps.En voyant le journal 'INFO: No default web.xml
', je l'ai suspecté (si c'est un avertissement, aurait été facile à repérer). L'astuce étant l'web.xml
(rootContext.setDefaultWebXml("web.xml")
) fourni avec Tomcat (conf/web.xml
). La raison en est, il comprend le DefaultServlet, qui sert les fichiers statiques aime HTML, JS. Utilisez le web.xml
ou enregistrez le servlet manuellement dans votre code.
Utilisation:
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "/src/main/", true);
server.start();
// .....
server.stop();
Ne pas oublier de placer la valeur par défaut web.xml
dans le même répertoire de ce programme ou d'un point à l'emplacement correct.
Il convient de noter que le crochet d'arrêt est inspiré de Antonio's answer.
Intégrer la jetée à la place. Cependant, il existe [tomcat-embed] (http://code.google.com/p/tomcat-embed/). – stepancheg
Merci pour le pointeur. Cependant, cela ne semble pas complet, activement développé ou maintenu. Tous les validations sont datées du 11 mai 2008 et l'un des messages du journal l'appelle "loin d'être terminé". –
Worth à regarder dans ce billet de blog: [Embedding Tomcat 7] (http://www.copperykeenclaws.com/embedding-tomcat-7/) –