java - 米娜 : Performing synchronous write requests/read responses

标签 java sockets synchronous apache-mina

我正在尝试使用 MINA 2.0 RC1 在基于多路分解的客户端应用程序中执行同步写入/读取,但它似乎卡住了。这是我的代码:

public boolean login(final String username, final String password) {
    // block inbound messages
    session.getConfig().setUseReadOperation(true);

    // send the login request
    final LoginRequest loginRequest = new LoginRequest(username, password);
    final WriteFuture writeFuture = session.write(loginRequest);
    writeFuture.awaitUninterruptibly();

    if (writeFuture.getException() != null) {
        session.getConfig().setUseReadOperation(false);
        return false;
    }

    // retrieve the login response
    final ReadFuture readFuture = session.read();
    readFuture.awaitUninterruptibly();

    if (readFuture.getException() != null) {
        session.getConfig().setUseReadOperation(false);
        return false;
    }

    // stop blocking inbound messages
    session.getConfig().setUseReadOperation(false);

    // determine if the login info provided was valid
    final LoginResponse loginResponse = (LoginResponse)readFuture.getMessage();
    return loginResponse.getSuccess();
}

我可以在服务器端看到检索到 LoginRequest 对象,并发送了一条 LoginResponse 消息。在客户端,DemuxingProtocolCodecFactory 收到响应,但在输入一些日志记录后,我可以看到客户端卡在对 readFuture.awaitUninterruptibly() 的调用上。

根据我自己的代码,我一辈子都弄不明白为什么它会卡在这里。我在 session 配置中正确地将读取操作设置为 true,这意味着应该阻止消息。但是,当我尝试同步读取响应消息时,消息似乎不再存在。

关于为什么这对我不起作用的任何线索?

最佳答案

这对我不起作用的原因是我的代码中其他地方存在问题,我愚蠢地忽略了实现消息响应编码器/解码器。啊。无论如何,我的问题中的代码在我修复后立即起作用。

关于java - 米娜 : Performing synchronous write requests/read responses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2550444/

相关文章:

c++ - 如何使异步 API 符合需要同步性的 API?

javascript - 如何故意卡住浏览器窗口(如警报、确认和提示)?

java - Spring Data MongoDB addToSet() 与复杂对象

python - 无法关闭 KeyboardInterrupt 上的套接字

C 客户端只能连接到 localhost。

C# TCP Socket流消息困惑

swift - 将 Alamofire 请求转换为同步?

java - 在android中并行下载位图

java - 在 Web 应用程序 Maven 中包含 jar 文件

java - 带有 Java 的 Firebase Admin SDK