本文系本人原创性实验工作,如若转载,请尊重个人劳动,注明出处。
这阵子,由于实验的需要,需要通过程序启动和停止Tomcat Web Server。很早以前就知道有Embedded tomcat。如果不使用嵌入式服务器,直接调用命令行
startup.bat, shutdown.bat,将输入输出重定向,是比较容易的,但是这样的tomcat比较臃肿,也不好看。
网上也有一些实例,但只是告诉我们启动tomcat,却没有合适的停止tomcat的示例。
于是就尝试写一个TomcatServer.java, 用于启动和停止嵌入式的tomcat server。
原理很简单:
一个启动线程,一个停止线程。停止线程不断监听$TOMCAT_HOME/tomcat.stop 标志文件,如果有这个文件,直接stop tomcat engine,然后删除这个文件。
嵌入式tomcat作用还是蛮大的,你可以拿它做一个Web admin console工具,嵌入到你的任何系统当中。
准备工作:
1. 下载tomcat5.0.28embed.zip,能google到。总共也就4M的样子:
2. 将它解压缩到d:\, 这样得到目录d:\jakarta-tomcat-5.0.28-embed
3. 准备一个demo.war,将其解压到d:\jakarta-tomcat-5.0.28-embed\webapps\下边,确保有一个demo目录,用作webapp。因为下边的程序要用到。
4. 在d:\jakarta-tomcat-5.0.28-embed下边编辑一个java源程序TomcatServer.java, 内容如下:
这个程序使用很简单,TomcatServer.start()启动,TomcatServer.stop()停止。
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.startup.Embedded;
/***
* A very simple embedded tomcat server, which launches a single webapp: demo.
* The flag file : tomcat.stop locates at $CATALINA_HOME/ is used to notify the
* control thread to stop tomcat engine or noe.
*/
public class TomcatServer
{
public static void stop()
{
final String path = System.getProperty("tomcat.home", "d:/jakarta-tomcat-5.0.28-embed");
try
{
new File(path).createNewFile();
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
}
public static void start()
{
// set the path for tomcat embed
final String path = System.getProperty("tomcat.home", "d:/jakarta-tomcat-5.0.28-embed");
final Embedded tc = createTomcat(path);
Thread startThread = new Thread()
{
public void run()
{
try
{
tc.start();
System.out.println("Tomcat Server 5.0.28 started .....");
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
};
startThread.start();
try
{
startThread.join();
}
catch (Exception ex)
{
ex.printStackTrace();
}
Thread controlThread = new Thread()
{
public void run()
{
for (;;)
{
try
{
Thread.sleep(2000);
File f = new File(path + "/tomcat.stop");
if (f.exists())
{
try
{
tc.stop();
System.out.println("Tomcat Server 5.0.28 stopped .....");
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
f.delete();
break;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
};
controlThread.start();
try
{
controlThread.join();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private static Embedded createTomcat(String path)
{
// create Tomcat Server Instance
Embedded tomcat = new Embedded();
// set the tomcat home
tomcat.setCatalinaHome(path);
// create the Tomcat engine
Engine engine = tomcat.createEngine();
engine.setName("TomcatServer of ProxyTest");
// create the host
Host host = tomcat.createHost("localhost", tomcat.getCatalinaHome() + "/webapps");
// put the host into engine
engine.addChild(host);
engine.setDefaultHost(host.getName());
String contextPath = host.getAppBase() + "/demo";
if (!new File(contextPath).exists())
{
System.err.println("Please test if the contextPath exists");
return null;
}
System.out.println("contextPath: " + contextPath);
Context context = tomcat.createContext("/demo", contextPath);
host.addChild(context);
tomcat.addEngine(engine);
try
{
tomcat.addConnector(tomcat.createConnector(InetAddress.getByName("127.0.0.1"), 8080,
false));
}
catch (UnknownHostException e)
{
System.err
.println("can not bind tomcat Server to the localhost 127.0.0.1:8080;test the host is free");
e.printStackTrace();
tomcat = null;
throw new RuntimeException(e);
}
return tomcat;
}
public static void main(String[] args)
{
start();
}
}