{"id":5048,"date":"2024-01-13T19:29:40","date_gmt":"2024-01-13T11:29:40","guid":{"rendered":"https:\/\/www.kenpai.com\/cms\/5048\/"},"modified":"2024-01-13T19:29:40","modified_gmt":"2024-01-13T11:29:40","slug":"tomcat-server%e7%bb%84%e4%bb%b6%e7%9b%91%e5%90%acshutdown%e5%91%bd%e4%bb%a4%e5%85%b3%e9%97%ad%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b9%8b%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.kenpai.com\/cms\/5048\/","title":{"rendered":"tomcat server\u7ec4\u4ef6\u76d1\u542cshutdown\u547d\u4ee4\u5173\u95ed\u670d\u52a1\u5668\u4e4b\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<\/p>\n<p>\u5728tomcat \u7684server.xml\u6587\u4ef6\u4e2d\uff0c\u9876\u5c42Server\u6807\u7b7e\u53ef\u4ee5\u914d\u7f6e\u4e00\u4e2a\u76d1\u542c\u5173\u95ed\u670d\u52a1\u5668\u547d\u4ee4\u7684\u7aef\u53e3\uff0c\u4e00\u65e6\u63a5\u6536\u5230SHUTDOWN\u7684\u8bf7\u6c42\u540e\uff0c\u5219\u5c1d\u8bd5\u5173\u95ed\u6574\u4e2atomcat\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5177\u4f53\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align:\">\n<p>&lt;Server port=&quot;8005&quot; shutdown=&quot;SHUTDOWN&quot;&gt;<\/p>\n<p>\u6807\u8bc6\u76d1\u542c8005\u7aef\u53e3\uff0c\u5982\u679c\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4e3a&ldquo;SHUTDOWN&rdquo;\uff0c\u90a3\u4e48\u4f1a\u89e6\u53d1\u5173\u95ed\u670d\u52a1\u5668\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e3a\u4e86\u5b9e\u9a8c\u8fd9\u4e2a\u7aef\u53e3\u597d\u4e0d\u597d\u4f7f\uff0c\u9996\u5148\u6211\u628atomcat\u542f\u52a8\u4e86\u8d77\u6765\uff0c\u7136\u540e\u5728\u6d4f\u89c8\u5668\u53d1\u9001\u4e86\u4e00\u4e2a\u8bf7\u6c42\uff1a http:\/\/localhost:8005\/SHUTDOWN<\/p>\n<p>\u7ed3\u679ctomcat\u540e\u53f0\u65e5\u5fd7\u4e3a\uff1a<\/p>\n<p>\u8b66\u544a: StandardServer.await: Invalid command \\\\\\&#8217;GET \/SHUTDOWN HTTP\/1.1\\\\\\&#8217; received<\/p>\n<p>\u5e76\u4e14\u670d\u52a1\u5668\u6ca1\u6709\u5173\u95ed\u3002\u8fd9\u6b21\u8bf7\u6c42\u6536\u5230\u7684\u5185\u5bb9\u4e3a GET \/SHUTDOWN HTTP\/1.1 \u800c\u4e0d\u662fSHUTDOWN \uff0c\u67e5\u770b\u6e90\u7801\u624d\u53d1\u73b0\uff0c\u8fd9\u91cc\u76f4\u63a5\u4ece\u76d1\u542c\u7684socket\u62ff\u7684\u6570\u636e\uff0c\u5e76\u6ca1\u6709\u505ahttp\u8bf7\u6c42\u89e3\u6790\uff0c\u6240\u4ee5\u6536\u5230\u4e86\u4e00\u4e2ahttp get\u8bf7\u6c42\u7684\u62a5\u6587\u3002\u8981\u60f3\u6b63\u786e\u53d1\u9001SHUTDOWN\uff0c\u9700\u8981\u901a\u8fc7socket\u76f4\u63a5\u53d1\u9001\u6570\u636e\uff0c\u4f7f\u7528jdk api\u7684\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<p>java.net.Socket socket = new java.net.Socket(&quot;localhost&quot;, 8005);         socket.getOutputStream().write(&quot;SHUTDOWN&quot;.getBytes());         socket.getOutputStream().flush();         socket.getOutputStream().close();         socket.close();<\/p>\n<p>\u8fd9\u6837tomcat\u670d\u52a1\u5668\u5c31\u80fd\u6b63\u786e\u5173\u95ed\u4e86\uff0c\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n<p>\u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.catalina.core.StandardServer await \u4fe1\u606f: A valid shutdown command was received via the shutdown port. Stopping the Server instance. \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol pause \u4fe1\u606f: Pausing ProtocolHandler [&quot;http-bio-8080&quot;] \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol pause \u4fe1\u606f: Pausing ProtocolHandler [&quot;ajp-bio-8009&quot;] \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.catalina.core.StandardService stopInternal \u4fe1\u606f: Stopping service Catalina \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol stop \u4fe1\u606f: Stopping ProtocolHandler [&quot;http-bio-8080&quot;] \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol stop \u4fe1\u606f: Stopping ProtocolHandler [&quot;ajp-bio-8009&quot;] \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol destroy \u4fe1\u606f: Destroying ProtocolHandler [&quot;http-bio-8080&quot;] \u4e03\u6708 17, 2017 12:56:59 \u4e0b\u5348 org.apache.coyote.AbstractProtocol destroy \u4fe1\u606f: Destroying ProtocolHandler [&quot;ajp-bio-8009&quot;]<\/p>\n<p>\u4e0b\u9762\u770b\u4e00\u4e0b\u6e90\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<p>\u9996\u5148Bootstrap\u7c7b\u8c03\u7528\u4e86Catalina\u7684start()\u65b9\u6cd5\u542f\u52a8\u670d\u52a1\u5668\uff0c\u5728start()\u65b9\u6cd5\u6700\u540e\uff0c\u6709\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>\/**  * Catalina\u7c7b  * Start a new server instance.  *\/     public void start() {         &#8230;         if (await) {             await();             stop();         }     }<\/p>\n<p>\u53d8\u91cfawait\u5728\u542f\u52a8\u65f6\u9ed8\u8ba4\u4e3atrue\uff0cawait()\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>\/**  * Catalina\u7c7b  * Await and shutdown.  *\/ public void await() {     getServer().await(); }<\/p>\n<p>Catalina\u7c7b\u4e2d\u7684Server\u9ed8\u8ba4\u5b9e\u73b0\u7c7b\u4e3aStandardServer\uff0c\u5176await()\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>\/\/StandardServer\u7c7b \/\/\u7b80\u5316\u7248\u6e90\u7801 public void await() {     \/\/\u5982\u679c\u7aef\u53e3\u4e3a-2\u6216-1\u7684\u5904\u7406\u65b9\u5f0f      \/\/\u6307\u5b9a\u4e86\u6b63\u5e38\u7aef\u53e3      \/\/\u9996\u5148\u76d1\u542c\u6b64\u7aef\u53e3     awaitSocket = new ServerSocket(port, 1,                     InetAddress.getByName(address));     \/\/ Loop waiting for a connection and a valid command     \/\/\u5faa\u73af     while (!stopAwait) {         &#8230;         \/\/\u963b\u585e\u5728\u8fd9\u91cc \u7b49\u5f85\u6570\u636e         socket = serverSocket.accept();          \/\/\u63a5\u6536\u5230\u6570\u636e\u540e\u5f80\u4e0b\u6267\u884c \u8bfb\u53d6\u6570\u636e         \/\/ Read a set of characters from the socket         int expected = 1024; \/\/ Cut off to avoid DoS attack         while (expected &lt; shutdown.length()) {             if (random == null)                 random = new Random();             expected  = (random.nextInt() % 1024);         }         while (expected &gt; 0) {             int ch = -1;             try {                 ch = stream.read();             } catch (IOException e) {                 log.warn(&quot;StandardServer.await: read: &quot;, e);                 ch = -1;             }             \/\/ Control character or EOF (-1) terminates loop             if (ch &lt; 32 || ch == 127) {                 break;             }             command.append((char) ch);             expected&#8211;;         }          \/\/ Match against our command string         boolean match = command.toString().equals(shutdown);         \/\/\u5982\u679c\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4e3a&quot;SHUTDOWN&quot;\u6216\u5176\u4ed6\u6b63\u786e\u7684\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32         if (match) {                                                         log.info(sm.getString(&quot;standardServer.shutdownViaPort&quot;));             \/\/\u8df3\u51fawhile\u5faa\u73af await()\u65b9\u6cd5\u7ed3\u675f\uff0c\u7ebf\u7a0b\u6267\u884c\u8fd4\u56de\u5230Catalina\u7c7b\u7684start()\u65b9\u6cd5\uff0c\u7ee7\u7eed\u6267\u884cCatalina\u7684stop()\u65b9\u6cd5                         break;         } else             log.warn(&quot;StandardServer.await: Invalid command \\\\\\&#8217;&quot;                       command.toString()   &quot;\\\\\\&#8217; received&quot;);<\/p>\n<p>\u4e0b\u9762\u770b\u4e00\u4e0bCatalina\u7684stop()\u65b9\u6cd5\uff0c\u6b64\u65b9\u6cd5\u662f\u771f\u6b63\u89e6\u53d1tomcat\u670d\u52a1\u5668destroy\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>public void stop() {     &#8230;     Server s = getServer();     LifecycleState state = s.getState();     if (LifecycleState.STOPPING_PREP.compareTo(state) &lt;= 0             &amp;&amp; LifecycleState.DESTROYED.compareTo(state) &gt;= 0) {         \/\/ Nothing to do. stop() was already called     } else {         s.stop();         s.destroy();     } }<\/p>\n<p>s\u5373StandardServer\uff0c\u9996\u5148\u5224\u65ads\u7684\u72b6\u6001\uff0c\u8be5\u72b6\u6001\u662f\u4e00\u4e2avolatile\u53d8\u91cf\uff0c\u5982\u679c\u72b6\u6001\u4e3a\u6b63\u5728\u9500\u6bc1\uff0c\u5219\u4e0d\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff1b\u5426\u5219\uff0c\u8c03\u7528s\u7684stop(\uff09\u548cdestroy()\u65b9\u6cd5\u3002<\/p>\n<p>stop()\u548cdestroy()\u65b9\u6cd5\u4e3a\u62bd\u8c61\u7c7bLifecycleBase\u7684\u65b9\u6cd5\uff0ctomcat\u4e2d\u51e1\u662f\u62e5\u6709\u751f\u547d\u5468\u671f\u7684\u7ec4\u4ef6\u5747\u7ee7\u627f\u81ea\u6b64\u62bd\u8c61\u7c7b\uff0c\u5e76\u4e14\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4f7f\u7528\u4e86 <strong>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f<\/strong> \uff0c\u65b9\u6cd5\u5185\u90e8\u5b9e\u73b0\u4e86\u4e00\u90e8\u5206\u66f4\u6539\u7ec4\u4ef6\u72b6\u6001\u7684\u4ee3\u7801\uff0c\u4e4b\u540e\u8c03\u7528\u4e86\u62bd\u8c61\u65b9\u6cd5stopInternal()\u548cstopDestroy()\uff0c\u8fd9\u4fe9\u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u7559\u7ed9\u5b50\u7c7b\u53bb\u5b9e\u73b0\u3002<\/p>\n<p>\u5728StandardServer\u7684stopInternal()\u65b9\u6cd5\u4e2d\uff0c\u4f1a\u4f9d\u6b21\u8c03\u7528\u5b50\u7ec4\u4ef6Service\u7684stop()\u65b9\u6cd5\uff1a<\/p>\n<p>@Override     protected void stopInternal() throws LifecycleException {      setState(LifecycleState.STOPPING);     fireLifecycleEvent(CONFIGURE_STOP_EVENT, null);      \/\/ Stop our defined Services     for (int i = 0; i &lt; services.length; i  ) {         services[i].stop();     }      globalNamingResources.stop();      stopAwait(); }<\/p>\n<p>StandardService\u7684stopInternal()\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>protected void stopInternal() throws LifecycleException {     \/\/ Pause connectors first \u9996\u5148\u505c\u6b62connector\u518d\u63a5\u6536\u65b0\u7684\u8bf7\u6c42     synchronized (connectorsLock) {         for (Connector connector: connectors) {             connector.pause();         }     }     \/\/\u8bbe\u7f6e\u72b6\u6001     setState(LifecycleState.STOPPING);     \/\/ Stop our defined Container second \u5173\u95ed\u5bb9\u5668\uff0c\u9876\u5c42\u4e3aStandardEngine     if (container != null) {         synchronized (container) {             container.stop();         }     }     \/\/ Now stop the connectors \u505c\u6b62connector     synchronized (connectorsLock) {         for (Connector connector: connectors) {             connector.stop();         }     }     synchronized (executors) { \u5173\u95ed\u7ebf\u7a0b\u6c60         for (Executor executor: executors) {             executor.stop();         }     } }<\/p>\n<p>\u4e4b\u540e\uff0c\u6bcf\u5c42\u7ec4\u4ef6\u90fd\u662f\u8fd9\u6837\u4e00\u6b65\u4e00\u6b65\u7684\u5411\u4e0b\u8c03\u7528\uff0c\u94fe\u5f0f\u89e6\u53d1\u6574\u4e2atomcat\u7ec4\u4ef6destory\u7684\u751f\u547d\u5468\u671f\uff0c\u5b8c\u6210\u8d44\u6e90\u7684\u91ca\u653e\u548c\u9500\u6bc1\uff0c\u6700\u7ec8\u5173\u95ed\u670d\u52a1\u5668\u3002<\/p>\n<p>stop\u548cdestroy\u76f8\u4f3c\uff0c\u53ea\u662f\u5de5\u4f5c\u4e0d\u540c\uff0c\u6709\u5174\u8da3\u7684\u540c\u5fd7\u53ef\u4ee5\u7ee7\u7eed\u6df1\u5165\u7814\u7a76\u6e90\u7801\u3002<\/p>\n<p>\u66f4\u591a\u5173\u4e8e\u4e91\u670d\u52a1\u5668\uff0c<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/domain.html\" title=\"\u57df\u540d\u6ce8\u518c\" target=\"_blank\">\u57df\u540d<\/a><\/span>\u6ce8\u518c\uff0c\u865a\u62df\u4e3b\u673a\u7684\u95ee\u9898\uff0c\u8bf7\u8bbf\u95ee<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">\u57a6\u6d3e\u79d1\u6280<\/a><\/span>\u5b98\u7f51\uff1a<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">www.kenpai.com<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728tomcat \u7684server.xml\u6587\u4ef6\u4e2d\uff0c\u9876\u5c42Server\u6807\u7b7e\u53ef\u4ee5\u914d\u7f6e\u4e00\u4e2a\u76d1\u542c\u5173\u95ed\u670d\u52a1\u5668\u547d\u4ee4\u7684\u7aef\u53e3\uff0c\u4e00\u65e6\u63a5\u6536\u5230SHUTDOWN\u7684\u8bf7\u6c42\u540e\uff0c\u5219\u5c1d\u8bd5\u5173\u95ed\u6574<\/p>\n","protected":false},"author":2,"featured_media":4350,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3322],"tags":[3917],"class_list":["post-5048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ktougao","tag-tomcat-servershutdown"],"_links":{"self":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/comments?post=5048"}],"version-history":[{"count":0,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5048\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media\/4350"}],"wp:attachment":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media?parent=5048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/categories?post=5048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/tags?post=5048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}