mina的初学笔记
最近在被研究mina,写点笔记吧,都是粗浅的内容,如果你一点都不懂,那看看无妨了。
这是下载mina的网址
http://mina.apache.org/downloads.html
下载了Binaries和sources之后,建一个工程,就可以跑例子了
先从Start那个例子开始吧。
首先是一个server类,MinaTimeServer,
定义 一个Port
private static final int PORT = 9123;
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) );
@Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { cause.printStackTrace(); }
@Override public void messageReceived( IoSession session, Object message ) throws Exception { System.out.println(message.getClass()); String str = message.toString(); if( str.trim().equalsIgnoreCase("quit") ) { // "Quit" ? let's get out ... session.close(true); return; } // Send the current date back to the client Date date = new Date(); session.write( date.toString() ); System.out.println("Message written..."); }
@Override public void sessionIdle( IoSession session, IdleStatus status ) throws Exception { System.out.println( "IDLE " + session.getIdleCount( status )); }
System.out.println(message.getClass());
acceptor.getStatistics().getReadBytesThroughput()
@Override public void sessionCreated(IoSession session) { session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); // We're going to use SSL negotiation notification. session.setAttribute(SslFilter.USE_NOTIFICATION); }
@Override public void messageReceived(IoSession session, Object message) throws Exception { LOGGER.info( "Received : " + message ); // Write the received data back to remote peer session.write(((IoBuffer) message).duplicate()); }