一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Apache Mina的學(xué)習(xí)應(yīng)用(三)

 集微筆記 2014-02-27

  在客戶端和服務(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)注下文。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    年轻女房东2中文字幕| 国产亚洲二区精品美女久久| 91偷拍视频久久精品| 国产精品不卡免费视频| 国产日产欧美精品大秀| 国产成人人人97超碰熟女| 一区二区三区欧美高清| 国产精品人妻熟女毛片av久 | 国产中文字幕一区二区| 亚洲精品中文字幕欧美| 国产高清在线不卡一区| 日韩国产亚洲一区二区三区| 91欧美日韩一区人妻少妇| 久久青青草原中文字幕| 视频在线免费观看你懂的 | 国产欧美一区二区色综合| 日韩精品福利在线观看| 国产欧美日韩精品一区二| 国产欧美日韩精品一区二区| 国产视频福利一区二区| 五月婷婷六月丁香在线观看 | 老外那个很粗大做起来很爽| 久久99夜色精品噜噜亚洲av| av在线免费观看一区二区三区| 日本高清不卡在线一区| 亚洲欧美日韩国产成人| 亚洲男人的天堂久久a| 国产亚洲不卡一区二区| 午夜福利直播在线视频| 五月婷婷六月丁香在线观看| 欧美成人高清在线播放| 日本少妇aa特黄大片| 九九热在线免费在线观看| 在线观看免费午夜福利| 91超频在线视频中文字幕| 在线观看免费视频你懂的| av国产熟妇露脸在线观看| 日韩精品中文在线观看| 日本国产欧美精品视频| 国产午夜在线精品视频| 国产偷拍盗摄一区二区|