使用MINA2同步接收消息

时间:2018-05-14 04:55:40

标签: java mina

我想使用 Apache MINA 2 库同步发送和接收消息。所以,我想使用以下代码:

// Establish connection
NioSocketConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);

connector.getFilterChain().addLast("codec",
          new ProtocolCodecFilter(new MyProtocolCodecFactory()));

connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.setHandler(new ClientSessionHandler());
connector.getSessionConfig().setTcpNoDelay(true);
connector.getSessionConfig().setKeepAlive(true);
connector.getSessionConfig().setSendBufferSize(MAX_BUFFER_SIZE);
connector.getSessionConfig().setReadBufferSize(MAX_BUFFER_SIZE);
connector.getSessionConfig().setReceiveBufferSize(MAX_BUFFER_SIZE);
connector.getSessionConfig().setBothIdleTime(IDLE_TIME);
connector.getSessionConfig().setUseReadOperation(true);


try {
     future = connector.connect(new InetSocketAddress(IP, PORT)).awaitUninterruptibly();
} catch (RuntimeIoException e) {
     connector.dispose();
}

IoSession session = future.getSession();
session.write(withdrawal);

ReadFuture readFuture = session.read();

Object recvMessage = null;
while (recvMessage == null) {
     if (!readFuture.isClosed()) {
         readFuture.awaitUninterruptibly();
         recvMessage = readFuture.getMessage();
     }
}

recvMessagenull。这段代码有什么问题?

提前致谢。

0 个答案:

没有答案