apache mina 同步接收客户端消息

我们知道,在MINA2中,发送和接受时两个独立的工作线程,但是可以设置一个参数,当服务端发送消息之后同步读取客户端的返回: 

Java代码   收藏代码
  1. session.getConfig().setUseReadOperation(true);  



近日,采用MINA2(RC)的同步读取方法,发现无法真的同步读取客户端的返回; 
场景是:服务端发送一个消息给客户端,需要同步等待客户端的一个消息回执,然后服务端的程序继续执行; 
 

Java代码   收藏代码
  1. sendSession.getConfig().setUseReadOperation(true);  
  2.         WriteFuture future = sendSession.write(xmlMsgBean); // 发送数据  
  3.         future.awaitUninterruptibly(); // 等待发送数据操作完成  
  4.         if (future.getException() != null) {  
  5.             throw new AppException(future.getException().getMessage());  
  6.         }  
  7.         if (future.isWritten()) {  
  8.             // 数据已经被成功发送  
  9.             logger.debug("数据已经被成功发送");  
  10.             ReadFuture readFuture = sendSession.read();  
  11.             readFuture.awaitUninterruptibly();  
  12.             if (readFuture.getException() != null) {  
  13.                 throw new AppException(readFuture.getException().getMessage());  
  14.             }  
  15.             sendSession.getConfig().setUseReadOperation(false);  
  16.             return ((XmlMsgBean) readFuture.getMessage()).getStrErrMsg();  
  17.         } else {  
  18.             // 数据发送失败  
  19.             logger.debug("数据发送失败");  
  20.         }  
  21.     





  后来用GOOGLE搜索了一下,发现在MINA的官网上,老外同样问了一个一模一样的问题,并且提了一个BUG上去,但是目前BUG的状态还是open; 
  https://issues.apache.org/jira/browse/DIRMINA-777 
 

Java代码   收藏代码
  1. I'm attempting to perform a synchronous write/read in a demux-based client application with MINA 2.0 RC1, but it seems to get stuck. Here is my code:   
  2.   
  3. {code}   
  4. public boolean login(final String username, final String password) {   
  5.     // block inbound messages   
  6.     session.getConfig().setUseReadOperation(true);   
  7.   
  8.     // send the login request   
  9.     final LoginRequest loginRequest = new LoginRequest(username, password);   
  10.     final WriteFuture writeFuture = session.write(loginRequest);   
  11.     writeFuture.awaitUninterruptibly();   
  12.   
  13.     if (writeFuture.getException() != null) {   
  14.         session.getConfig().setUseReadOperation(false);   
  15.         return false;   
  16.     }   
  17.   
  18.     // retrieve the login response   
  19.     final ReadFuture readFuture = session.read();   
  20.     readFuture.awaitUninterruptibly();   
  21.   
  22.     if (readFuture.getException() != null) {   
  23.         session.getConfig().setUseReadOperation(false);   
  24.         return false;   
  25.     }   
  26.   
  27.     // stop blocking inbound messages   
  28.     session.getConfig().setUseReadOperation(false);   
  29.   
  30.     // determine if the login info provided was valid   
  31.     final LoginResponse loginResponse = (LoginResponse)readFuture.getMessage();   
  32.     return loginResponse.getSuccess();   
  33. }   
  34. {code}   
  35.   
  36. I can see on the server side that the LoginRequest object is retrieved, and a LoginResponse message is sent. On the client side, the DemuxingProtocolCodecFactory receives the response, but after throwing in some logging, I can see that the client gets stuck on the call to `readFuture.awaitUninterruptibly() `.   
  37.   
  38. I can't for the life of me figure out why it is stuck here based upon my own code. I properly set the read operation to true on the session config, meaning that messages should be blocked. However, it seems as if the message no longer exists by time I try to read response messages synchronously.  


  
Key: DIRMINA-777  
Type:  Bug  
Status:  Open  
Priority:  Blocker  
Assignee: Unassigned  
Reporter: Matt Huggins  
Votes: 0  
Watchers: 0  

猜你喜欢

转载自q364035622.iteye.com/blog/1860640