日期:2014-05-16  浏览次数:21050 次

杞細Apache MINA 蹇€熷叆闂?
1銆備粙缁?

Apache MINA 鏄竴涓綉缁滃簲鐢ㄦ鏋讹紝鏈夊姪浜庣敤鎴烽潪甯告柟渚垮湴寮€鍙戦珮鎬ц兘銆侀珮浼哥缉鎬х殑缃戠粶搴旂敤銆傚畠閫氳繃Java NIO鎻愪緵浜嗕竴涓娊璞$殑銆佷簨浠堕┍鍔ㄧ殑銆佸紓姝ョ殑浣嶄簬鍚勭浼犺緭鍗忚锛堝TCP/IP鍜孶DP/IP锛変箣涓婄殑API锛?

Apache MINA 閫氬父鍙绉颁箣涓猴細

NIO 妗嗘灦搴擄紱
瀹㈡埛绔?鏈嶅姟鍣ㄦ鏋跺簱锛?
鎴栬€呬竴涓綉缁渟ocket搴撱€?

鐒惰€岋紝瀹冩墍鎻愪緵鐨勫姛鑳借繙涓嶆杩欎簺銆?

锛堜互涓婂唴瀹瑰ぇ鑷寸炕璇戣嚜Apache MINA缃戠珯锛?

濡傛湡瀹樻柟鏂囨。鐨勪粙缁嶏紝Apache MINA 鏄竴涓綉缁滃簲鐢ㄧ▼搴忔鏋讹紝瀹冨Java涓殑socket鍜孨IO杩涜浜嗘湁鏁堝拰娓呮櫚鐨勫皝瑁咃紝鏂逛究寮€鍙戜汉鍛樺紑鍙慣CP/UDP绋嬪簭锛屼粠鑰屾姏寮€鍦ㄤ娇鐢ㄥ師濮嬬殑socket鏃堕渶瑕佽€冭檻鐨勫悇绉嶇箒鏉傝€屽張鐑︿汉闂锛堢嚎绋嬨€佹€ц兘銆佷細璇濈瓑锛夛紝鎶婃洿澶氱簿鍔涗笓钁楀湪搴旂敤涓殑涓氬姟閫昏緫鐨勫紑鍙戜笂銆?

Apache MINA 鏈変袱涓富瑕佺増鏈細2.0 鍜?1.1锛?.0涓?.1鏈夎緝澶х殑鍖哄埆锛屽叾閲囩敤java NIO杩涜寮€鍙戯紝浣垮緱鎬ц兘寰楀埌鏈夋晥鐨勬彁鍗囷紝鍦ㄦ帴鍙f柟闈篃鏈変笉灏忕殑鍙樺寲锛屽叿浣撲俊鎭彲浠ュ弬瑙佸叾缃戠珯璇存槑銆?

涓嬮潰鐨勪粙缁嶄互 Apache MINA 2.0 涓轰緥銆?

2銆傝蒋浠跺噯澶?

瑕佺紪鍐欏拰杩愯涓€涓熀浜嶢pache MINA 2.0鐨勭▼搴忥紝闇€瑕丣DK 5.0浠ヤ笂鐗堟湰锛岃繕闇€瑕佷互涓嬭蒋浠讹細

锛?锛塎INA 2.x Core

涓嬭浇鍦板潃锛歨ttp://mina.apache.org/downloads.html

锛?锛塖LF4J 1.3.0 or greater

涓嬭浇鍦板潃锛歨ttp://www.slf4j.org/download.html

杩欓噷闇€瑕佺敤鍒颁袱涓?SLF4J 鐨刯ar鍖咃細slf4j-api.jar 鍜?slf4j-jdk14.jar銆?

鍒嗗埆瑙e帇涓嬭浇鐨凪INA 2.x Core 鍜孲LF4J鏂囦欢锛屾壘鍑轰笅鍒梛ar鍖呮枃浠讹細

mina-core-2.0.0-M1.jar
slf4j-api.jar
slf4j-jdk14.jar

3銆傜紪鍐欎竴涓狝pache MINA 鏃堕棿鏈嶅姟鍣?

璇ョ▼搴忕殑鍔熻兘闈炲父绠€鍗曪紝灏辨槸褰撳鎴风杩炴帴鍒版湇鍔″櫒鐨?123绔彛鍚庯紝绋嬪簭灏嗘湇鍔″櫒褰撳墠鐨勬椂闂翠俊鎭互瀛楃涓茬殑褰㈠紡鍙戦€佺粰瀹㈡埛绔€?

鎴戜滑鍙互鐢‥clipse鏉ュ垱寤虹紪鍐欒繖涓▼搴忋€?

锛?锛夊湪Eclipse涓垱寤轰竴涓狫ava椤圭洰锛屼緥濡?TimeServerProject锛岀劧鍚庡皢mina-core-2.0.0-M1.jar銆乻lf4j-api.jar 鍜?slf4j-jdk14.jar杩欎笁涓枃浠舵坊鍔犲埌椤圭洰鐨凩ibraries涓€?

锛?锛夊垱寤轰竴涓?MinaTimeServer 绫伙紝鍏跺唴瀹逛负锛?
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 
public class MinaTimeServer {
private static final int PORT = 9123;
 
public static void main(String[] args) throws IOException {
   IoAcceptor acceptor = new NioSocketAcceptor();
 
   acceptor.getFilterChain().addLast("logger", new LoggingFilter());
   acceptor.getFilterChain().addLast(
     "codec",
     new ProtocolCodecFilter(new TextLineCodecFactory(Charset
       .forName("UTF-8"))));
 
   acceptor.setHandler(new TimeServerHandler());
   acceptor.getSessionConfig().setReadBufferSize(2048);
   acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
   acceptor.bind(new InetSocketAddress(PORT));
}
}

锛?锛夊垱寤轰竴涓?TimeServerHandler 绫伙紝鍏跺唴瀹逛负
import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
 
public class TimeServerHandler extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
    throws Exception {
   cause.printStackTrace();
}
 
@Override
public void messageReceived(IoSession session, Object message)
    throws Exception {
   String str = message.toString();
   System.out.println("Message read:");
   System.out.println(str);
 
   Date date = new Date();
   session.write(date.toString());
   System.out.println("Message written...");
   session.close();
}
 
@Override
public void sessionIdle(IoSession session, IdleStatus status)
    throws Exception {
   System.out.println("IDLE " + session.getIdleCount(status));
}
}

锛?锛夌紪璇戝苟杩愯MinaTimeServer绫?

锛?锛夋墦寮€鎮ㄧ殑娴忚鍣紝鍦ㄥ湴鍧€鏍忎腑杈撳叆 http://127.0.0.1:9123/锛岀劧鍚庡洖杞︼紝浣犲氨鍙互鐪嬪埌鏈嶅姟鍣ㄧ殑鏃堕棿鏄剧ず鍦ㄦ偍鐨勬祻瑙堝櫒鍐呬簡銆傚悓鏃舵偍涔熷彲浠ュ湪杩愯MinaTimeServer鐨勬帶鍒跺彴绐楀彛涓湅鍒扮被浼间笅闈㈢殑淇℃伅锛?
Message read:2008-4-11 12:22:44 org.apache.mina.filter.logging.Lo