当前位置 : 主页 > 网络编程 > 其它编程 >

嵌入式Tomcat的使用(即使用java代码控制tomcat服务)

来源:互联网 收集:自由互联 发布时间:2023-07-02
好长时间没有写文章了,有点懈怠了最近在为公司开发Eclipse插件,需要有用到嵌入式Tomcat的地方,所以上网查找资料研究了一下。开始时是想用Runtime.exec 好长时间没有写文章了,有点懈
好长时间没有写文章了,有点懈怠了最近在为公司开发Eclipse插件,需要有用到嵌入式Tomcat的地方,所以上网查找资料研究了一下。开始时是想用Runtime.exec

好长时间没有写文章了,有点懈怠了...最近在为公司开发Eclipse插件,需要有用到嵌入式Tomcat的地方,所以上网查找资料研究了一下。

开始时是想用Runtime.exec方法来操作普通tomcat中的startup.bat和shutdown.bat文件来达到启动关闭的效果,但是一直未能解决隐藏其dos窗口,后来有感于MyEclipse自带的Tomcat插件,我把它的插件取出来观察,发现其引用了一个tomcat.jar的包...最后才知道Apache早给我们想好了,提供给我们这么一个特制的嵌入式Tomcat.

首先要去tomcat官网下载embedded版本的tomcat,我所下载的是apache-tomcat-5.5.34-embed.tar.gz,下载后解压出来的目录结构为:

将lib里面所有的jar包都引入到我们的项目里面来 代码如下: package com.dhcc.platform;import org.apache.catalina.Context;import org.apache.catalina.Engine;import org.apache.catalina.Host;import org.apache.catalina.Lifecycle;import org.apache.catalina.LifecycleEvent;import org.apache.catalina.LifecycleException;import org.apache.catalina.LifecycleListener;import org.apache.catalina.connector.Connector;import org.apache.catalina.startup.Embedded;/** * 嵌入式Tomcat管理器 * @author lifeng * 2011-11-24 下午03:00:13 */public class EmbeddedTomcatLauncher {private Embedded tomcat = null;private String path;//EmbeddedTomcat所在路径private String projectName;//所要发布的项目名private int port;/** * 服务是否正在启用状态中 */private boolean isRunning;public EmbeddedTomcatLauncher(String path , String projectName,int port) {this.path = path;this.projectName = projectName;this.port = port;isRunning = false;}public EmbeddedTomcatLauncher(String path , String projectName) {this.path = path;this.projectName = projectName;this.port = 8080;isRunning = false;}public static void main(String[] args) {EmbeddedTomcatLauncher tomcatLauncher = new EmbeddedTomcatLauncher("D:/temp/tomcat-embed","FileTree");tomcatLauncher.startTomcat();}/** * 启动Tomcat服务 * @author lifeng */public void startTomcat(){try {if(!isRunning){initEmbedded();tomcat.start();isRunning = true;}} catch (LifecycleException e) {e.printStackTrace();}}/** * 停止Tomcat服务 * @author lifeng */public void stopTomcat(){try {if(isRunning){tomcat.stop();isRunning = false;}} catch (LifecycleException e) {e.printStackTrace();}}/** * 重启Tomcat服务 * @author lifeng */public void restartTomcat(){try {if(isRunning){tomcat.stop();initEmbedded();tomcat.start();System.out.println();}else {initEmbedded();tomcat.start();}} catch (LifecycleException e) {e.printStackTrace();}}/** * 初始化Tomcat服务信息 * @author lifeng */public void initEmbedded() {tomcat = new Embedded();tomcat.setCatalinaHome(path);Engine engine = tomcat.createEngine();engine.setName("DhccEngine");Host host = tomcat.createHost("localhost", path + "\\webapps");Context myCOntext= tomcat.createContext("/"+projectName, path + "/webapps/" + projectName);host.addChild(myContext);engine.addChild(host);engine.setDefaultHost("localhost");tomcat.addEngine(engine);Connector cOnnector= tomcat.createConnector("localhost", port, false);tomcat.addConnector(connector);tomcat.addLifecycleListener(new LifecycleListener() {public void lifecycleEvent(LifecycleEvent le) {String lc = le.getType();if(lc.equals(Lifecycle.START_EVENT)){System.out.println("start!");isRunning = true;}else if (lc.equals(Lifecycle.STOP_EVENT)){System.out.println("stop!");isRunning = false;}else if (lc.equals(Lifecycle.DESTROY_EVENT)){try {EmbeddedTomcatLauncher.this.tomcat.stop();isRunning = false;} catch (LifecycleException e) {e.printStackTrace();}}}});}}【感谢龙石为本站提供数据交换平台,http://www.longshidata.com/pages/exchange.html】
上一篇:react中antd Upload手动上传的示例
下一篇:没有了
网友评论