Skip to content

Commit 0a2b9bf

Browse files
LiZhenNetbeiwei30
authored andcommitted
Add shutdown command for telnet (#3280)
* telnet add shutdown command * refactor rename shutDown to shutdown * remove unregister in doDestroy * unregister the ShutdownHook when the shutdown command invoked
1 parent d470679 commit 0a2b9bf

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.rpc.protocol.dubbo.telnet;
18+
19+
import org.apache.dubbo.common.extension.Activate;
20+
import org.apache.dubbo.common.utils.StringUtils;
21+
import org.apache.dubbo.config.DubboShutdownHook;
22+
import org.apache.dubbo.remoting.Channel;
23+
import org.apache.dubbo.remoting.RemotingException;
24+
import org.apache.dubbo.remoting.telnet.TelnetHandler;
25+
import org.apache.dubbo.remoting.telnet.support.Help;
26+
27+
/**
28+
* ShutdownTelnetHandler
29+
*/
30+
@Activate
31+
@Help(parameter = "[-t <milliseconds>]", summary = "Shutdown Dubbo Application.", detail = "Shutdown Dubbo Application.")
32+
public class ShutdownTelnetHandler implements TelnetHandler {
33+
@Override
34+
public String telnet(Channel channel, String message) throws RemotingException {
35+
36+
int sleepMilliseconds = 0;
37+
if (StringUtils.isNotEmpty(message)) {
38+
String[] parameters = message.split("\\s+");
39+
if (parameters.length == 2 && parameters[0].equals("-t") && StringUtils.isInteger(parameters[1])) {
40+
sleepMilliseconds = Integer.parseInt(parameters[1]);
41+
} else {
42+
return "Invalid parameter,please input like shutdown -t 10000";
43+
}
44+
}
45+
long start = System.currentTimeMillis();
46+
if (sleepMilliseconds > 0) {
47+
try {
48+
Thread.sleep(sleepMilliseconds);
49+
} catch (InterruptedException e) {
50+
return "Failed to invoke shutdown command, cause: " + e.getMessage();
51+
}
52+
}
53+
StringBuilder buf = new StringBuilder();
54+
DubboShutdownHook.getDubboShutdownHook().unregister();
55+
DubboShutdownHook.getDubboShutdownHook().doDestroy();
56+
long end = System.currentTimeMillis();
57+
buf.append("Application has shutdown successfully");
58+
buf.append("\r\nelapsed: ");
59+
buf.append(end - start);
60+
buf.append(" ms.");
61+
return buf.toString();
62+
}
63+
}

dubbo-rpc/dubbo-rpc-dubbo/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pwd=org.apache.dubbo.rpc.protocol.dubbo.telnet.CurrentTelnetHandler
55
invoke=org.apache.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler
66
trace=org.apache.dubbo.rpc.protocol.dubbo.telnet.TraceTelnetHandler
77
count=org.apache.dubbo.rpc.protocol.dubbo.telnet.CountTelnetHandler
8-
select=org.apache.dubbo.rpc.protocol.dubbo.telnet.SelectTelnetHandler
8+
select=org.apache.dubbo.rpc.protocol.dubbo.telnet.SelectTelnetHandler
9+
shutdown=org.apache.dubbo.rpc.protocol.dubbo.telnet.ShutdownTelnetHandler
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.rpc.protocol.dubbo.telnet;
18+
19+
import org.apache.dubbo.remoting.Channel;
20+
import org.apache.dubbo.remoting.RemotingException;
21+
import org.apache.dubbo.remoting.telnet.TelnetHandler;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.mockito.Mockito.mock;
26+
27+
/**
28+
* SelectTelnetHandlerTest.java
29+
*/
30+
public class ShutdownTelnetHandlerTest {
31+
32+
private static TelnetHandler handler = new ShutdownTelnetHandler();
33+
private Channel mockChannel;
34+
35+
@SuppressWarnings("unchecked")
36+
@Test
37+
public void testInvoke() throws RemotingException {
38+
mockChannel = mock(Channel.class);
39+
String result = handler.telnet(mockChannel, "");
40+
assertTrue(result.contains("Application has shutdown successfully"));
41+
}
42+
43+
44+
@SuppressWarnings("unchecked")
45+
@Test
46+
public void testInvokeWithTimeParameter() throws RemotingException {
47+
mockChannel = mock(Channel.class);
48+
int sleepTime = 2000;
49+
long start = System.currentTimeMillis();
50+
String result = handler.telnet(mockChannel, "-t " + sleepTime);
51+
long end = System.currentTimeMillis();
52+
assertTrue(result.contains("Application has shutdown successfully") && (end - start) > sleepTime);
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)