tConsoleTelnet 開(kāi)發(fā)環(huán)境
tConsole 是一個(gè) Telnet Console 框架,提供一個(gè) Telnet 環(huán)境支持,給予沒(méi)有界面類(lèi)的應(yīng)用一個(gè)可以通過(guò)命令行進(jìn)行交互的工具。
特性
- 支持監(jiān)聽(tīng)本地端口提供 Telnet 交互的界面。
- 支持基于標(biāo)準(zhǔn)輸入輸出構(gòu)建交互控制臺(tái)的能力。
- 利用 tConsole 可以輕松構(gòu)建命令工具包。
樣例
實(shí)現(xiàn)一個(gè)控制臺(tái)命令。
public class HelloWordExecutor implements TelExecutor {
/** 命令的幫助信息,在 help <command> 時(shí)候輸出這個(gè)信息 */
public String helpInfo() {
return "hello help.";
}
/** 執(zhí)行命令體 */
public String doCommand(TelCommand telCommand) throws Throwable {
return "you say ->" + telCommand.getCommandName();
}
}
Server 模式
利用 telnet 命令來(lái)交互
public static void main(String[] args) {
AppContext appContext = Hasor.create().build((TelModule) apiBinder -> {
TelnetBuilder telnetBuild = apiBinder.asTelnet("127.0.0.1", 2180);
telnetBuild.addExecutor("hello").to(HelloWordExecutor.class);
}
appContext.joinSignal();
}
輸入 telnet 127.0.0.1 2180 之后
>telnet 127.0.0.1 2180
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
--------------------------------------------
Welcome to tConsole!
login : Tue Jan 07 14:26:29 CST 2020 now. form /127.0.0.1:60023
workAt : /127.0.0.1:2180
Tips: You can enter a 'help' or 'help -a' for more information.
use the 'exit' or 'quit' out of the console.
--------------------------------------------
tConsole>
Host 模式
充當(dāng)命令工具包,建議利用 Spring Boot 的 fat jar 打包能力整合使用。
public static void main(String[] args) {
AppContext appContext = Hasor.create().build((TelModule) apiBinder -> {
HostBuilder hostBuild = apiBinder.asHostWithSTDO().preCommand(args);
hostBuild.addExecutor("hello").to(HelloWordExecutor.class);
}
}
輸入 java xxx.jar hello 執(zhí)行 hello 命令。
評(píng)論
圖片
表情
