基于mina的服务端业务逻辑层和数据访问层

40 views
Skip to first unread message

Kevin Chen

unread,
May 31, 2012, 12:57:01 AM5/31/12
to Apache Mina高性能通信框架研究邮件列表
经过测试发现mina的IoHandler是单例的,是否有配置改成每个session一个IoHandler实例?

目前采用如下方式处理具体的业务消息,
就是把业务逻辑和数据访问的部分都交给session的attribute对象处理。
大家看看这样可以不?

//-------------------code---------------------
@Override
public void sessionOpened(IoSession session) throws Exception {
super.sessionOpened(session);
session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
session.setAttribute("agencyMsgProcessor", new
AgencyMsgProcessor(session));
session.setAttribute("productMsgProcessor", new
ProductMsgProcessor(session));
session.setAttribute("userMsgProcessor", new
UserMsgProcessor(session));
session.setAttribute("policyMsgProcessor", new
PolicyMsgProcessor(session));
}

@Override
public void messageReceived(IoSession session, Object message) throws
Exception {
super.messageReceived(session, message);
//获取到一条完整的消息数据
IoBuffer buff = (IoBuffer)message;
int id = buff.getInt(4);
buff.position(0);//还原初始位置
MsgBase msg = MessageFactory.newMsg(id, buff);
if(msg!=null) {

((AgencyMsgProcessor)session.getAttribute("agencyMsgProcessor")).processMsg(msg);

((ProductMsgProcessor)session.getAttribute("productMsgProcessor")).processMsg(msg);

((UserMsgProcessor)session.getAttribute("userMsgProcessor")).processMsg(msg);

((PolicyMsgProcessor)session.getAttribute("policyMsgProcessor")).processMsg(msg);
}
}
Reply all
Reply to author
Forward
0 new messages