加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 外闻 > 正文

[应用服务器]掌握Tomcat应用服务器只需一分钟

发布时间:2018-12-27 06:43:38 所属栏目:外闻 来源:爱折腾的稻草
导读:副标题#e# No.1 搭建环境 1.1、下载tomcat源码 进入tomcat官网:https://tomcat.apache.org/ 下载对应版本的源码 1.2、导入Eclipse 新建一个Java项目 将Tomcat源码包中的java目录下的文件拷贝到src目录 导入外部依赖包 ant.jar ecj-4.4.jar jaxrpc.jar wsdl

首先,调用init()方法逐级初始化,接着调用start()方法进行启动,同时,每次调用伴随着生命周期状态变更事件的触发。

  • org.apache.catalina.startup.Bootstrap的程序入口main方法,具体实现如下:
  1. public static void main(String args[]) { 
  2.  
  3.         if (daemon == null) { 
  4.             // Don't set daemon until init() has completed 
  5.             Bootstrap bootstrap = new Bootstrap(); 
  6.             try { 
  7.                 bootstrap.init(); 
  8.             } catch (Throwable t) { 
  9.                 handleThrowable(t); 
  10.                 t.printStackTrace(); 
  11.                 return; 
  12.             } 
  13.             daemon = bootstrap; 
  14.         } else { 
  15.             // When running as a service the call to stop will be on a new 
  16.             // thread so make sure the correct class loader is used to prevent 
  17.             // a range of class not found exceptions. 
  18.             Thread.currentThread().setContextClassLoader(daemon.catalinaLoader); 
  19.         } 
  20.  
  21.         try { 
  22.             String command = "start"; 
  23.             if (args.length > 0) { 
  24.                 command = args[args.length - 1]; 
  25.             } 
  26.  
  27.             if (command.equals("startd")) { 
  28.                 args[args.length - 1] = "start"; 
  29.                 daemon.load(args); 
  30.                 daemon.start(); 
  31.             } else if (command.equals("stopd")) { 
  32.                 args[args.length - 1] = "stop"; 
  33.                 daemon.stop(); 
  34.             } else if (command.equals("start")) { 
  35.                 daemon.setAwait(true); 
  36.                 daemon.load(args); 
  37.                 daemon.start(); 
  38.             } else if (command.equals("stop")) { 
  39.                 daemon.stopServer(args); 
  40.             } else if (command.equals("configtest")) { 
  41.                 daemon.load(args); 
  42.                 if (null==daemon.getServer()) { 
  43.                     System.exit(1); 
  44.                 } 
  45.                 System.exit(0); 
  46.             } else { 
  47.                 log.warn("Bootstrap: command "" + command + "" does not exist."); 
  48.             } 
  49.         } catch (Throwable t) { 
  50.             // Unwrap the Exception for clearer error reporting 
  51.             if (t instanceof InvocationTargetException && 
  52.                     t.getCause() != null) { 
  53.                 t = t.getCause(); 
  54.             } 
  55.             handleThrowable(t); 
  56.             t.printStackTrace(); 
  57.             System.exit(1); 
  58.         } 
  59.  
  • org.apache.catalina.startup.Bootstrap的初始化方法,具体实现如下:
  1. public void init() throws Exception  { 
  2.         // 1、设置catalina.home的配置:将catalina.home系统属性设置为当前工作目录(如果尚未设置)。 
  3.         setCatalinaHome(); 
  4.     // 2、设置catalina.base的配置:如果没有设置的话,将当前的工作目录为了catalina.base的设置 
  5.         setCatalinaBase(); 
  6.     // 3、初始化类加载器:commonLoader、catalinaLoader、sharedLoader 
  7.         initClassLoaders(); 
  8.  
  9.         Thread.currentThread().setContextClassLoader(catalinaLoader); 
  10.  
  11.         SecurityClassLoad.securityClassLoad(catalinaLoader); 
  12.  
  13.         // 加载我们的启动类并调用其process()方法 
  14.         if (log.isDebugEnabled()) 
  15.             log.debug("Loading startup class"); 
  16.     //4、加载启动类 
  17.         Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina"); 
  18.         //5、实例化启动类 
  19.     Object startupInstance = startupClass.newInstance(); 
  20.  
  21.         if (log.isDebugEnabled()) 
  22.             log.debug("Setting startup class properties"); 
  23.  
  24.     //6、设置方法参数 
  25.     String methodName = "setParentClassLoader"; 
  26.         Class<?> paramTypes[] = new Class[1]; 
  27.         paramTypes[0] = Class.forName("java.lang.ClassLoader"); 
  28.         Object paramValues[] = new Object[1]; 
  29.         paramValues[0] = sharedLoader; 
  30.         Method method = startupInstance.getClass().getMethod(methodName, paramTypes); 
  31.     // 7、调用启动类的setParentClassLoader方法设置共享扩展类加载器 
  32.         method.invoke(startupInstance, paramValues); 
  33.  
  34.         catalinaDaemon = startupInstance; 
  35.  
  • org.apache.catalina.startup.Bootstrap的start()方法,具体实现如下:
  1. /** 
  2. * Start the Catalina daemon. 
  3. */ 
  4. public void start() throws Exception { 
  5.     // 如果启动类为实例化,则调用init()方法 
  6.     if( catalinaDaemon==null ) init(); 
  7.  
  8.     //获取启动类的start方法 
  9.     Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null); 
  10.     //调用启动类的start方法,即调用org.apache.catalina.startup.Catalina的start()方法 
  11.     method.invoke(catalinaDaemon, (Object [])null); 
  12.  

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读