来自客户端的 Apache Mina TCP 会话跟踪
我使用 Apache Mina 创建了一个 TCP 客户端。我添加了一个 while 循环来不断检查端口的活跃度。一旦服务器端建立连接,循环就会被打破并建立连接。我从未来获取会话并用它进行通信。 有没有更好的方法来做到这一点。我可以要求连接等待直到其启动,而不是循环。
while(true){
try {
ConnectFuture future = ioConnector.connect(new InetSocketAddress(Port),
new TriggerReceiverHandler(), SOCKET_CONFIG);
System.out.println("Message Receiver started and listening on port "+ Port);
Thread.sleep(1000);
session = future.getSession();
if(session != null)
break;
} catch (InterruptedException e) {
e.printStackTrace();
}catch(Exception ce){
if(ce.getCause() instanceof ConnectException)
System.out.println("Retrying connection");
}
}
另一个问题是,如果服务器宕机了,我希望服务器一直等待连接直到上线,我该怎么办?
I have created a TCP client using Apache Mina. I have added a while loop to constantly check the liveness of the port. Once the connection is up on the server side, the loop is broken and the connection is made. i get the session from future and use it to communicate.
Is there a better way to do this. instead of loop can i ask the connection to wait till its up.
while(true){
try {
ConnectFuture future = ioConnector.connect(new InetSocketAddress(Port),
new TriggerReceiverHandler(), SOCKET_CONFIG);
System.out.println("Message Receiver started and listening on port "+ Port);
Thread.sleep(1000);
session = future.getSession();
if(session != null)
break;
} catch (InterruptedException e) {
e.printStackTrace();
}catch(Exception ce){
if(ce.getCause() instanceof ConnectException)
System.out.println("Retrying connection");
}
}
Another question is, If the server is down and I want the server to keep waiting for the connection till its up, what should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是,到目前为止这是不可能的,因为只有当我们尝试连接时才知道连接状态。一项修改是代替
Thread.sleep(1000);
,我们可以在 1.0+ 版本中添加future.join()
,或者在 2.0 版本中添加未来的侦听器+The answer is, As of now its not possible, as the connection state is known only when we attempt to connect. One modification is instead of the
Thread.sleep(1000);
we can addfuture.join()
in version 1.0+ or add a listener for the future in case of 2.0+