Mina2.0 同步通讯bug的临时解决方案
mina同步通讯的实现很简单,需要设置一个iosession参数:
session.getConfig().setUseReadOperation(true);
?实现通讯操作如下:
?
ReadFuture readFuture = this.session.read();if (readFuture.await(this.timeout, TimeUnit.MILLISECONDS)) {if (this.interval > 0)// 帧间隔Thread.sleep(this.interval);return (AbstractModbusResponse) readFuture.getMessage();} else {// 读超时try {((AbstractIoSession) session).offerReadFuture(null);// 针对同步实现的bug} catch (Exception e) {e.printStackTrace();}throw new ModbusTimeOutException("read time out");}
?在这篇文章中也有描述:
http://scholers.iteye.com/blog/789156
?
该bug主要表现在如果出现了较多的超时现象,会出现无法恢复通讯的现象实现同步(不出异常只是不停地超时)。
反复看mina的源代码,经过多次尝试,我找出了一个临时解决方案:
出现超时后加入下面一行即可:
((AbstractIoSession) session).offerReadFuture(null);// 针对同步实现的bug
?执行该方法会抛出异常,需要捕获一下。