1、mina使用的版本是2.0.4
2、spring中mina的配置如下:
<bean id="serverHandler"
class="cn.test.main.mina.MinaServerHandler">
<property name="mainServer" ref="mainServer"></property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.net.SocketAddress">
<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
</entry>
</map>
</property>
</bean>
<!-- 开始运行socket服务 -->
<bean id="ioAcceptor"
class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
init-method="bind" destroy-method="unbind">
<!-- mina服务启动端口 -->
<property name="defaultLocalAddress" value=":9999" />
<property name="handler" ref="serverHandler" />
<property name="reuseAddress" value="true" />
</bean>
3、cn.test.main.mina.MinaServerHandler类为自定义类必须继承IoHandlerAdapter接口
我是以字节方式去读取数据的,代码内容(其中有些业务处理逻辑不再详述)如下:
<!-- 报文接收完成后的业务处理主类 -->
private MainServer mainServer;
// 读取次数
private final String times_str = "times_str";
// 读取报文内容
private final String readmsg_str = "readmsg_str";
// 报文长度
private final String msgLen_str = "msgLen_str";
// 当前读取长度
private final String msgtempLen_str = "msgtempLen_str";
public void setMainServer(MainServer mainServer) {
this.mainServer = mainServer;
}
/**
* @生成时间: 2011-11-30 下午04:11:02
* @方法说明: 服务端接收消息
* @参数:
* @返回值:
* @异常:
**/
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
Object times = session.getAttribute(times_str);
Object readmsg_temp = session.getAttribute(readmsg_str);
byte[] readmsg = readmsg_temp==null?null:(byte[])readmsg_temp;
Object msgLen_temp = session.getAttribute(msgLen_str);
int msgLen = msgLen_temp==null?0:(Integer)msgLen_temp;
Object msgtempLen_temp = session.getAttribute(msgtempLen_str);
int msgtempLen = msgtempLen_temp==null?0:(Integer)msgtempLen_temp;
byte[] temp = null;
try{
IoBuffer buff = (IoBuffer) message;
int tempLen = buff.limit();
temp = new byte[tempLen];
buff.get(temp);
if(null == times){
msgLen = (int)ByteTransLongTool.byteToLong(CharUtil.getSubByte(temp, 0, 4));
readmsg = new byte[msgLen];
session.setAttribute(msgLen_str, msgLen);
session.setAttribute(readmsg_str, readmsg);
session.setAttribute(times_str, "");
}else{
readmsg = (byte[])session.getAttribute(readmsg_str);
}
CharUtil.setBigByte(readmsg, msgtempLen, temp, tempLen);
msgtempLen += tempLen;
session.setAttribute(readmsg_str, readmsg);
session.setAttribute(msgtempLen_str, msgtempLen);
if(msgtempLen >= msgLen){
byte[] retmsg = mainServer.execute(readmsg);
session.removeAttribute(times_str);
session.removeAttribute(readmsg_str);
session.removeAttribute(msgLen_str);
session.removeAttribute(msgtempLen_str);
session.write(IoBuffer.wrap(retmsg));
}
}catch(Exception ex){
session.removeAttribute(times_str);
session.removeAttribute(readmsg_str);
session.removeAttribute(msgLen_str);
session.removeAttribute(msgtempLen_str);
_log.error("报文解析失败,报文内容为:[" + new String(temp) + "]");
throw ex;
}
}
/**
* @生成时间: 2011-12-27 下午01:31:09
* @方法说明: 发送返回报文
* @参数:
* @返回值:
* @异常:
**/
public void sendMessage(IoSession session, byte[] newByte){
byte[] retmsg = null;
try {
retmsg = mainServer.execute(newByte);
} catch (Exception ex) {
_log.error("服务器交易信息处理失败:", ex);
retmsg = ExceptionMessageProcess.getExceptMessage(ex,
mainServer);
}
session.write(IoBuffer.wrap(retmsg));
}
/**
* @生成时间: 2011-11-30 下午04:11:18
* @方法说明: 接收完成后结束当前连接
* @参数:
* @返回值:
* @异常:
**/
public void messageSent(IoSession session, Object message) throws Exception {
_log.debug("服务器与客户端:" + session.getRemoteAddress() + "断开连接。");
session.close(true);
}
/**
* @生成时间: 2011-11-30 下午04:11:31
* @方法说明: 客户端与服务器建立连接
* @参数:
* @返回值:
* @异常:
**/
public void sessionCreated(IoSession session) throws Exception {
_log.debug("服务器与客户端:" + session.getRemoteAddress() + "建立连接...");
}
/**
* @生成时间: 2011-12-27 下午02:55:05
* @方法说明: 异常处理关闭服务
* @参数:
* @返回值:
* @异常:
**/
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
_log.error("服务器发生异常:", cause);
session.close(true);
}