Telnet关闭Tomcat
web.xml 中默认配置
<Server port="8005" shutdown="SHUTDOWN"></Server>
操作步骤
- telnet 127.0.0.1 8005
- 发送 【SHUTDOWN】,会发现Tomcat关闭
说明
private volatile boolean stopAwait = false;
@Override
public void await() {
// 端口设置为 -2, tomcat启动失败
if( port == -2 ) {
return;
}
// 端口设置为 -1,休眠 10s,重新检测
if( port==-1 ) {
try {
awaitThread = Thread.currentThread();
while(!stopAwait) {
try {
Thread.sleep( 10000 );
} catch( InterruptedException ex ) {
}
}
} finally {
awaitThread = null;
}
return;
}
// 监听默认设置 端口
try {
awaitSocket = new ServerSocket(port, 1,
InetAddress.getByName(address));
} catch (IOException e) {
return;
}
try {
awaitThread = Thread.currentThread();
// Loop waiting for a connection and a valid command
while (!stopAwait) {
ServerSocket serverSocket = awaitSocket;
if (serverSocket == null) {
break;
}
// Wait for the next connection
Socket socket = null;
StringBuilder command = new StringBuilder();
try {
InputStream stream;
long acceptStartTime = System.currentTimeMillis();
try {
socket = serverSocket.accept();
socket.setSoTimeout(10 * 1000); // Ten seconds
stream = socket.getInputStream();
} catch (SocketTimeoutException ste) {
continue;
} catch (AccessControlException ace) {
continue;
} catch (IOException e) {
if (stopAwait) {
break;
}
break;
}
// Read a set of characters from the socket
int expected = 1024; // Cut off to avoid DoS attack
while (expected < shutdown.length()) {
if (random == null)
random = new Random();
expected += (random.nextInt() % 1024);
}
while (expected > 0) {
int ch = -1;
try {
ch = stream.read();
} catch (IOException e) {
ch = -1;
}
// Control character or EOF (-1) terminates loop
if (ch < 32 || ch == 127) {
break;
}
command.append((char) ch);
expected--;
}
} finally {
if (socket != null) {
socket.close();
}
}
// 接收的命令 == 设置的关闭指令
boolean match = command.toString().equals(shutdown);
if (match) {
break;
} else
log.warn("StandardServer.await: Invalid command '"
+ command.toString() + "' received");
}
} finally {
ServerSocket serverSocket = awaitSocket;
awaitThread = null;
awaitSocket = null;
// Close the server socket and return
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// Ignore
}
}
}
}
修改配置
- port:修改为 【-1】
- SHUTDOWN:修改为较复杂的字符串

更多精彩