Java mina TcpCommunicationHandler extends IoHandlerAdapter

package com.pingan.emall.biz.communication.handler;




import com.pingan.emall.biz.communication.TcpSessionLock;
import com.pingan.emall.biz.communication.TcpSessionManager;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.SocketSessionConfig;


/**
 * 核心I/O線程處理類, 接收filterChain處理後的MidResponseDTO, 並釋放當前session 鎖,
 * 通知等待線程返回
 * 
 * @author LICHAO844
 *
 */
public class TcpCommunicationHandler extends IoHandlerAdapter {

private TcpSessionManager sessionManager;
private Map<Object, TcpSessionLock> locks; 

private AtomicInteger sessionCount = new AtomicInteger();


private static final Logger LOG = Logger.getLogger(TcpCommunicationHandler.class);

public TcpCommunicationHandler(TcpSessionManager sessionManager, Map<Object, TcpSessionLock> locks) {
this.sessionManager = sessionManager;
this.locks = locks;
}

@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
Object sessionId = session.getId();
// Close IoSession and remove it from session manager
sessionManager.closeSession(sessionId);

StringBuilder builder = new StringBuilder();
builder.append("sessionId : ").append(sessionId)
.append(", Exception happens in tcp communication");
LOG.error(builder.toString(), cause);
}


@Override
public void messageReceived(IoSession session, Object message)
throws Exception {

Object sessionId = session.getId();
if (sessionId == null) {
LOG.error("No sessionKey bind with mina IoSession" + session);
return;
}

TcpSessionLock lock = locks.get(sessionId);
if (lock != null) {
synchronized (lock) {
//將信息set 喚起線程 實現 異步轉同步
lock.setMessage(message);
lock.notify();
}
}
}


@Override
public void sessionClosed(IoSession session) throws Exception {
int totalSessionCount = sessionCount.decrementAndGet();
if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append("IoSession closed, total session count is ")
.append(totalSessionCount).append(", ").append(session).append(", sessionId : ")
.append(session.getId());
LOG.debug(builder.toString());
}
}


@Override
public void sessionCreated(IoSession session) throws Exception {
int totalSessionCount = sessionCount.incrementAndGet();

if (session.getConfig() instanceof SocketSessionConfig) {
SocketSessionConfig config = (SocketSessionConfig) session.getConfig();
config.setKeepAlive(true);
config.setTcpNoDelay(true);
config.setSoLinger(0);
}

if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append("IoSession created, total session count is ")
.append(totalSessionCount).append(", ").append(session)
.append(", sessionId : ").append(session.getId());
LOG.debug(builder.toString());
}
}


@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append("IoSession idle, status is ").append(status)
.append(", ").append(session).append(", TxnId : ")
.append(session.getId());
LOG.debug(builder.toString());
}

// both idle for 30s, close session
if (status == IdleStatus.BOTH_IDLE) {
Object sessionId = session.getId();
if (sessionId != null) {
sessionManager.closeSession(sessionId);
} else {
session.close(true);
}
}

}


@Override
public void sessionOpened(IoSession session) throws Exception {
if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append("IoSession opened : ").append(session).append(", sessionId : ")
.append(session.getId());
LOG.debug(builder.toString());
}
}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章