本文主要是介绍mina 2.07 官方举例 MinaTimerServer 中的类关系,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、代码
1、主类
package org.soachina;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.soachina.TimeServerHandler;
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));}
}
2、TimeServerHandler
package org.soachina;import java.util.Date;import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;public class TimeServerHandler extends IoHandlerAdapter {@Overridepublic void exceptionCaught(IoSession session, Throwable cause)throws Exception {cause.printStackTrace();}@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {String str = message.toString();if (str.trim().equalsIgnoreCase("quit")) {session.close();return;}//下面把日期注释掉了,让其返回客户端发来的字符串//Date date = new Date(); session.write(str); // session.write(date.toString());System.out.println("Message written..." + str);}@Overridepublic void sessionIdle( IoSession session, IdleStatus status ) throws Exception{System.out.println( "IDLE " + session.getIdleCount( status ));}
}
二、类关系
三、源代码地址
http://mina.apache.org/mina-project/quick-start-guide.html
这篇关于mina 2.07 官方举例 MinaTimerServer 中的类关系的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!