我已经构建了一个 Spring CLI 应用程序,它以异步方式与服务器通信。给定了服务器,我没有创建它,基本上我的应用程序需要打开一个 TCP 套接字并通过它发送一个 JSON,然后它发回一个 JSON。强制不要使用 CLI 参数,而是在请求的回调中,我想向用户显示一组选项,他需要通过在 CLI 上插入相应的数字来选择这些选项。很可能我没有做正确的事情,因为输入命令后,我看到spring>
在控制台上(这是一个预期的行为),它会阻止异步回调,除非我按下某些东西(当我收到回调时,CLI 不会打印任何内容,除非我按下一堆输入 - 这是意外的)。到目前为止,我使用 JLine 的命令行从控制台读取,我想要实现的是,当我从服务器获得响应并提供回调时,控制台被提供给正在运行回调的线程(我立即将回调的内容打印到控制台,我无需任何技巧即可读取输入)。
一些代码:
public void runReceiver(){
receiverThread = new Thread(() -> {
byte[] digit = null;
int nb;
Iterator<CommandListener> it;
CommandListener listener;
String message;
List<CommandListener> listenersToRemove = new ArrayList<>();
while (true) {
try {
nb = communicatorInput.readInt();
digit = new byte[nb];
communicatorInput.readFully(digit);
} catch (IOException e) {
e.printStackTrace();
}
it = listeners.iterator();
while (it.hasNext()){
listener = it.next();
if (digit != null && digit.length > 0) {
message = new String(digit);
// the message was not acknowledged
if(message.contains("NACK")){
try {
listener.onError(message);
if (listener.isDone()) {
listenersToRemove.add(listener);
}
} catch (Exception e){
e.printStackTrace();
}
} else try {
listener.onCompleted(message);
} catch (InvalidObjectException e){
Main.logger.debug(String.format("Response could not be parsed as %s", listener.getCommandType()));
} catch (Exception e){
e.printStackTrace();
}
if (listener.isDone()) {
listenersToRemove.add(listener);
}
}
}
listeners.removeAll(listenersToRemove);
}
}, "receiverThread");
receiverThread.setDaemon(true);
receiverThread.start();
然后是一个 CLI 命令(这里不需要输入):
@CliCommand(value="start", help = "Starts stuff")
public void start() throws IOException, InterruptedException {
// this method is passed to the thread with the listener
getAvailabilities().updateAvailabilities("all", "all", "all", someListener);
}
以及该侦听器的回调:
someListener = new CommandListener() {
private String source = "Start some listener";
@Override
public void onCompleted(String r) throws IOException {
System.out.println("Which would you like to start?");
getAvailabilities().printAvailableBrands();
String brandNumber = "";
while(Objects.equals(brandNumber, "")){
System.out.println("Please enter the number of the Brand: ");
//when the callback arrives here I still only see ">spring:" and I get nothing printed on the console
brandNumber = cr.readLine();
if(!isInputAllowed(brandNumber, getAvailabilities().AvailableBrands.size())){
brandNumber = "";
}
}
BrandName = getAvailabilities().AvailableBrands.get(Integer.parseInt(brandNumber) - 1);
//updating the availabilities narrows down the things I list to the console, so I send an update after every selection
getAvailabilities().updateAvailabilities("all", BrandName, "all", getInterfaceListener);
done = true;
}
这可能与有时在 Idea 中调试 CLI 时出现异常输入的问题有关,例如。当我插入start
它时No such command as ar
,如果我再次按下回车,它会说(一些)其余的:No such command as stt
。