mina read方法出现BufferUnderflowException异常的解决办法
现象: 先连续发几十个很小很小的包(<10 byte) 再突然发一个大小64byte的包 这时你会发现mina就会出现以下错误
??? ??? ??? </map>
??? ??? </property>
??? </bean>
????<!-- 处理器 -->
??? <bean id="clientConnHandler" />
????<!-- socket接收器,接收客户端连接 -->
??? <bean id="ioAcceptor" destroy-method="unbind">
??? ??? <!--??? ??? <property name="defaultLocalAddress" value=":161" />-->
??? ??? <property name="handler" ref="clientConnHandler" />
??? ??? <property name="reuseAddress" value="true" />
??? ??? <property name="filterChainBuilder" ref="filterChainBuilder" />
??? </bean>
要配置协议过滤器,必须使用一个ProtocolCodecFactory ,下面是简单实现
public class MessageCodecFactory implements ProtocolCodecFactory {
??? private final MessageEncoder encoder;
??? private final MessageDecoder decoder;
????
??? public MessageCodecFactory() {
??? ??? encoder = new MessageEncoder();
??????? decoder = new MessageDecoder();
??? }
??? /* (non-Javadoc)
??? ?* @see org.apache.mina.filter.codec.ProtocolCodecFactory#getDecoder(org.apache.mina.core.session.IoSession)
??? ?*/
??? @Override
??? public ProtocolDecoder getDecoder(IoSession session) throws Exception {
??? ??? return decoder;
??? }
??? /* (non-Javadoc)
??? ?* @see org.apache.mina.filter.codec.ProtocolCodecFactory#getEncoder(org.apache.mina.core.session.IoSession)
??? ?*/
??? @Override
??? public ProtocolEncoder getEncoder(IoSession session) throws Exception {
??? ??? return encoder;
??? }
}
/**
?* 译码器,不做任何事情
?*/
public class MessageEncoder extends ProtocolEncoderAdapter {
??? /* (non-Javadoc)
??? ?* @see org.apache.mina.filter.codec.ProtocolEncoder#encode(org.apache.mina.core.session.IoSession, java.lang.Object, org.apache.mina.filter.codec.ProtocolEncoderOutput)
??? ?*/
??? @Override
??? public void encode(IoSession session, Object message,
??? ??? ??? ProtocolEncoderOutput out) throws Exception {
??? ??? //Do nothing
??? }
}?
转自http://blianchen.blog.163.com/blog/static/1310562992010101891522100/
搞定了
ps:不知这个是不是mina的bug,是不是还有别的方法配置的呢???
请教那位兄弟有更好的解决方法.