Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1278110
  • 博文数量: 185
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 3934
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-11 13:11
个人简介

iihero@ChinaUnix, ehero.[iihero] 数据库技术的痴迷爱好者. 您可以通过iihero AT qq.com联系到我 以下是我的三本图书: Sybase ASE in Action, Oracle Spatial及OCI高级编程, Java2网络协议内幕

文章分类

全部博文(185)

文章存档

2014年(4)

2013年(181)

分类: 嵌入式

2013-07-23 09:47:11

本文系本人原创性实验工作,如若转载,请尊重个人劳动,注明出处。

这阵子,由于实验的需要,需要通过程序启动和停止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()停止。


[java]
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.net.InetAddress;  
  4. import java.net.UnknownHostException;  
  5.   
  6. import org.apache.catalina.Context;  
  7. import org.apache.catalina.Engine;  
  8. import org.apache.catalina.Host;  
  9. import org.apache.catalina.startup.Embedded;  
  10.   
  11. /*** 
  12.  * A very simple embedded tomcat server, which launches a single webapp: demo. 
  13.  * The flag file :  tomcat.stop locates at $CATALINA_HOME/ is used to notify the  
  14.  * control thread to stop tomcat engine or noe.  
  15.  */  
  16. public class TomcatServer  
  17. {  
  18.       
  19.     public static void stop()  
  20.     {  
  21.         final String path = System.getProperty("tomcat.home""d:/jakarta-tomcat-5.0.28-embed");  
  22.         try  
  23.         {  
  24.             new File(path).createNewFile();  
  25.         }  
  26.         catch (IOException ex)  
  27.         {  
  28.             throw new RuntimeException(ex);  
  29.         }  
  30.     }  
  31.       
  32.     public static void start()  
  33.     {  
  34.         // set the path for tomcat embed   
  35.         final String path = System.getProperty("tomcat.home""d:/jakarta-tomcat-5.0.28-embed");  
  36.   
  37.         final Embedded tc = createTomcat(path);  
  38.         Thread startThread = new Thread()  
  39.         {  
  40.             public void run()  
  41.             {  
  42.                 try  
  43.                 {  
  44.                     tc.start();  
  45.                     System.out.println("Tomcat Server 5.0.28 started .....");  
  46.                 }  
  47.                 catch (Exception e)  
  48.                 {  
  49.                     e.printStackTrace();  
  50.                     throw new RuntimeException(e);  
  51.                 }  
  52.             }  
  53.         };  
  54.         startThread.start();  
  55.         try  
  56.         {  
  57.             startThread.join();  
  58.         }  
  59.         catch (Exception ex)  
  60.         {  
  61.             ex.printStackTrace();  
  62.         }  
  63.               
  64.         Thread controlThread = new Thread()   
  65.         {  
  66.             public void run()  
  67.             {  
  68.                 for (;;)  
  69.                 {  
  70.                     try  
  71.                     {  
  72.                         Thread.sleep(2000);  
  73.                         File f = new File(path + "/tomcat.stop");  
  74.                         if (f.exists())  
  75.                         {  
  76.                             try  
  77.                             {  
  78.                                 tc.stop();  
  79.                                 System.out.println("Tomcat Server 5.0.28 stopped .....");  
  80.                             }  
  81.                             catch (Exception e)  
  82.                             {  
  83.                                 e.printStackTrace();  
  84.                                 throw new RuntimeException(e);  
  85.                             }  
  86.                             f.delete();  
  87.                             break;  
  88.                         }  
  89.                     }  
  90.                     catch (Exception ex)  
  91.                     {  
  92.                         ex.printStackTrace();  
  93.                     }  
  94.                 }  
  95.             }  
  96.         };  
  97.         controlThread.start();  
  98.         try  
  99.         {  
  100.             controlThread.join();  
  101.         }  
  102.         catch (Exception ex)  
  103.         {  
  104.             ex.printStackTrace();  
  105.         }          
  106.     }  
  107.       
  108.   
  109.     private static Embedded createTomcat(String path)  
  110.     {  
  111.         // create Tomcat Server Instance   
  112.         Embedded tomcat = new Embedded();  
  113.         // set the tomcat home   
  114.         tomcat.setCatalinaHome(path);  
  115.         // create the Tomcat engine   
  116.         Engine engine = tomcat.createEngine();  
  117.         engine.setName("TomcatServer of ProxyTest");  
  118.         // create the host   
  119.         Host host = tomcat.createHost("localhost", tomcat.getCatalinaHome() + "/webapps");  
  120.         // put the host into engine   
  121.         engine.addChild(host);  
  122.         engine.setDefaultHost(host.getName());  
  123.   
  124.         String contextPath = host.getAppBase() + "/demo";  
  125.         if (!new File(contextPath).exists())  
  126.         {  
  127.             System.err.println("Please test if the contextPath exists");  
  128.             return null;  
  129.         }  
  130.         System.out.println("contextPath: " + contextPath);  
  131.         Context context = tomcat.createContext("/demo", contextPath);  
  132.         host.addChild(context);  
  133.   
  134.         tomcat.addEngine(engine);  
  135.         try  
  136.         {  
  137.             tomcat.addConnector(tomcat.createConnector(InetAddress.getByName("127.0.0.1"), 8080,  
  138.                     false));  
  139.         }  
  140.         catch (UnknownHostException e)  
  141.         {  
  142.             System.err  
  143.                     .println("can not bind tomcat Server to the localhost 127.0.0.1:8080;test the host is free");  
  144.             e.printStackTrace();  
  145.             tomcat = null;  
  146.             throw new RuntimeException(e);  
  147.         }  
  148.         return tomcat;  
  149.     }  
  150.       
  151.     public static void main(String[] args)  
  152.     {  
  153.         start();  
  154.     }  
  155. }  





阅读(3592) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~