|
| 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 | +} |
0 commit comments