在客戶端和服務(wù)端的中定義相關(guān)的業(yè)務(wù)邏輯類的實(shí)現(xiàn):通過(guò)實(shí)現(xiàn)IoHandler 中實(shí)現(xiàn)方法,操作各種業(yè)務(wù)。
一般的開發(fā)中采用IoHandlerAdapter,通過(guò)重寫部分的方法實(shí)現(xiàn)的相關(guān)的業(yè)務(wù)邏輯;
IoHandler
IoHandler具有以下功能
- sessionCreated sessionCreated
- sessionOpened sessionOpened
- sessionClosed sessionClosed
- sessionIdle sessionIdle
- exceptionCaught exceptionCaught
- messageReceived messageReceived
- messageSent messageSent
sessionCreated事件
Session Created event is fired when a new connection is created.
會(huì)話創(chuàng)建事件被激發(fā)時(shí),一個(gè)新的連接被創(chuàng)建。
For TCP its the result of connection accept, and for UDP this is generated when a UDP packet is received.
對(duì)于TCP的連接其結(jié)果接受,這是為UDP時(shí)生成一個(gè)UDP數(shù)據(jù)包接收。
This function can be used to initialize session attributes, and perform one time activities for a particular connection.
該功能可用于初始化會(huì)話屬性,并執(zhí)行一次活動(dòng)的特定連接。
This function is invoked from the I/O processor thread context, hence should be implemented in a way that it consumes minimal amount of time, as the same thread handles multiple sessions.
調(diào)用此函數(shù)從I / O處理器線程上下文,因此應(yīng)該采取一種方式,它消耗最少量的時(shí)間,相同的線程處理多個(gè)會(huì)話執(zhí)行。
sessionOpened Event sessionOpened事件
Session opened event is invoked when a connection is opened.
調(diào)用的事件是打開一個(gè)連接。
Its is always called after sessionCreated event.它總是sessionCreated事件后調(diào)用。 If a thread model is configured, this function is called in a thread other than the I/O processor thread.
如果一個(gè)線程模型配置,該函數(shù)被調(diào)用的線程比I / O處理器線程等。
sessionClosed Event sessionClosed事件
Session Closed event is closed, when a session is closed.
會(huì)話關(guān)閉時(shí),當(dāng)一個(gè)會(huì)話結(jié)束。
Session cleaning activities like cash cleanup can be performed here.
sessionIdle Event sessionIdle事件
Session Idle event is fired when a session becomes idle.
會(huì)議空閑事件被激發(fā)時(shí),會(huì)變?yōu)榭臻e。
This function is not invoked for UDP transport.
此功能沒有援引UDP傳輸。
exceptionCaught Event exceptionCaught事件
This functions is called, when an Exception is thrown by user code or by MINA.
這個(gè)函數(shù)被調(diào)用,當(dāng)異常是由用戶代碼或MINA的異常。
The connection is closed, if its an IOException.連接關(guān)閉,如果它的一個(gè)IOException。
messageReceived Event messageReceived事件
Message Received event is fired whenever a message is received.
消息收稿事件被激發(fā)時(shí),收到一條消息。
This is where the most of the processing of an application happens.
這就是對(duì)一個(gè)應(yīng)用程序處理大部分發(fā)生。
You need to take care of all the message type you expect here.
您需要考慮所有類型的信息在這里照顧你期望。
messageSent Event messageSent事件
Message Sent event is fired, whenever a message aka response has been sent(calling IoSession.write()).消息發(fā)送的事件被激發(fā),每當(dāng)有消息又名反應(yīng)已發(fā)送(呼叫IoSession.write())。
客戶端的業(yè)務(wù)邏輯處理器:
源代碼如下:
package cn.com.unutrip.java.opensource.mina.sumup;
import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import cn.com.unutrip.java.opensource.mina.sumup.message.AddMessage; import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage; /** * * 處理所有I / O活動(dòng)的MINA的激發(fā)。 * The interface is hub of all activities done at the end of the Filter Chain. * 該接口是在過(guò)濾器鏈的末端進(jìn)行的所有活動(dòng)的中心。 * IoHandler has following functions IoHandler具有以下功能 * sessionCreated : * 會(huì)話創(chuàng)建事件被激發(fā)時(shí),一個(gè)新的連接被創(chuàng)建。 For TCP its the result of connection accept, and for UDP this is generated when a UDP packet is received.對(duì)于TCP的連接其結(jié)果接受,這是為UDP時(shí)生成一個(gè)UDP數(shù)據(jù)包接收。 This function can be used to initialize session attributes, and perform one time activities for a particular connection.該功能可用于初始化會(huì)話屬性,并執(zhí)行一次活動(dòng)的特定連接。 *This function is invoked from the I/O processor thread context, hence should be implemented in a way that it consumes minimal amount of time, as the same thread handles multiple sessions.調(diào)用此函數(shù)從I / O處理器線程上下文,因此應(yīng)該采取一種方式,它消耗最少量的時(shí)間,相同的線程處理多個(gè)會(huì)話執(zhí)行。 * * sessionOpened :會(huì)話開始一個(gè)連接時(shí)調(diào)用此事件,它總在sessionCreated事件后調(diào)用,如果一個(gè)線程模型配置 * 該函數(shù)被調(diào)用的的線程IoHandler。 * * sessionClosed : 當(dāng)一個(gè)會(huì)話結(jié)束,session清理活動(dòng)的活動(dòng)的連接。 * * sessionIdle : 此方法被激發(fā)時(shí),會(huì)變成空閑,此功能沒有援引UDP傳輸。 * * exceptionCaught : 當(dāng)用戶代碼或者mina異常是,連接關(guān)閉,如果它的一個(gè)連接IOException * * messageReceived : 當(dāng)接受到一個(gè)消息時(shí)被激發(fā),對(duì)應(yīng)一個(gè)應(yīng)用程序的處理的大部分業(yè)務(wù)邏輯, * * messageSent : 消息發(fā)送的時(shí)間被激發(fā), * * 調(diào)用的事件是打開一個(gè)連接。 Its is always called after sessionCreated event.它總是sessionCreated事件后調(diào)用。 * If a thread model is configured, this function is called in a thread other than the I/O processor thread. * 如果一個(gè)線程模型配置,該函數(shù)被調(diào)用的線程比I / O處理器線程等。
* * 客戶端業(yè)務(wù)處理器的應(yīng)用 * * @author longgangbai * */ public class ClientSessionHandler extends IoHandlerAdapter {
private final static Logger logger = LoggerFactory.getLogger(ClientSessionHandler.class); private final int[] values; private boolean finished; public ClientSessionHandler(int[] values) { this.values = values; } public boolean isFinished() { return finished; } /** * * 調(diào)用的事件是打開一個(gè)連接。 Its is always called after sessionCreated event. * 它總是sessionCreated事件后調(diào)用。 * If a thread model is configured, this function is called in a thread other than the I/O processor thread. * 如果一個(gè)線程模型配置,該函數(shù)被調(diào)用的線程比I / O處理器線程等。 */ @Override public void sessionOpened(IoSession session) { logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionOpened "); // send summation requests for (int i = 0; i < values.length; i++) { AddMessage m = new AddMessage(); m.setSequence(i); m.setValue(values[i]); session.write(m); } } /** * 接受來(lái)自服務(wù)端的數(shù)據(jù)信息 * 接受到消息事件被激發(fā)時(shí),收到一條消息。 * This is where the most of the processing of an application happens. * 這就是對(duì)一個(gè)應(yīng)用程序處理大部分發(fā)生。 * You need to take care of all the message type you expect here. * 您需要考慮所有類型的信息在這里照顧你期望。(重要的業(yè)務(wù)邏輯的寫這里) */ @Override public void messageReceived(IoSession session, Object message) { logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%messageReceived "); // server only sends ResultMessage. otherwise, we will have to identify // its type using instanceof operator. ResultMessage rm = (ResultMessage) message; if (rm.isOk()) { // server returned OK code. // if received the result message which has the last sequence // number, // it is time to disconnect. if (rm.getSequence() == values.length - 1) { // print the sum and disconnect. logger.error("The sum: " + rm.getValue()); session.close(true); finished = true; } } else { // seever returned error code because of overflow, etc. logger.error("Server error, disconnecting..."); session.close(true); finished = true; } } /** * 發(fā)生異常時(shí)的操作 * 當(dāng)異常是由用戶代碼或MINA的異常。 The connection is closed, if its an IOException.連接關(guān)閉,如果它的一個(gè)IOException */ @Override public void exceptionCaught(IoSession session, Throwable cause) { logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%exceptionCaught "); session.close(true); } } 服務(wù)端的業(yè)務(wù)邏輯處理器:
package cn.com.unutrip.java.opensource.mina.sumup;
import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import cn.com.unutrip.java.opensource.mina.sumup.message.AddMessage; import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage; /** * 創(chuàng)建服務(wù)端的業(yè)務(wù)處理器 * @author longgangbai * */ public class ServerSessionHandler extends IoHandlerAdapter{ private static final String SUM_KEY="sum"; private final static Logger logger=LoggerFactory.getLogger(ServerSessionHandler.class); @Override public void sessionOpened(IoSession session){ logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionOpened "); //設(shè)置Idle的時(shí)間為60秒 session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60); //初始化總的和為0 session.setAttribute(SUM_KEY,new Integer(0)); } @Override public void sessionIdle(IoSession session,IdleStatus statis) { logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionIdle "); logger.info("Disconnecting the idle."); // disconnect an idle client session.close(true);
} @Override public void exceptionCaught(IoSession session, Throwable cause) { logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%exceptionCaught "); // close the connection on exceptional situation session.close(true); }
@Override public void messageReceived(IoSession session ,Object message){ logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%messageReceived "); //客戶端發(fā)送的對(duì)象必須住注冊(cè)的對(duì)象一致 AddMessage am=(AddMessage)message; //將客戶端的發(fā)送的數(shù)據(jù)添加到當(dāng)前總的金額中 int sum=(Integer)session.getAttribute(SUM_KEY); int value=am.getValue(); long expectedsum=(long)sum+value; if(expectedsum>Integer.MAX_VALUE||expectedsum { // ResultMessage rm=new ResultMessage(); rm.setSequence(am.getSequence()); rm.setOk(false); session.write(rm); }else{ sum=(int)expectedsum; session.setAttribute(SUM_KEY,new Integer(sum)); ResultMessage rm=new ResultMessage(); rm.setSequence(am.getSequence()); rm.setOk(true); rm.setValue(sum); session.write(rm); } }
}
文章尚未寫完,請(qǐng)關(guān)注下文。
|