mina的初学笔记

最近在被研究mina,写点笔记吧,都是粗浅的内容,如果你一点都不懂,那看看无妨了。
这是下载mina的网址
http://mina.apache.org/downloads.html
下载了Binaries和sources之后,建一个工程,就可以跑例子了
先从Start那个例子开始吧。

首先是一个server类,MinaTimeServer,
定义 一个Port
private static final int PORT = 9123;

然后在main函数中,
开启一个非阻塞的接受,即服务器
IoAcceptor acceptor = new NioSocketAcceptor();

添加filter,这个是log的,没什么好说的
acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );

这个可以好好说说。filterchain就是一个filter的链,这个是codec的,
也就是说解码用的,TextLineCodeFactory是其中的一个,即每一行来编解码。
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));

下面是set一个handler,也就是怎么解决client传入的数据
acceptor.setHandler( new TimeServerHandler() );

这个就≈io里面的buffer
acceptor.getSessionConfig().setReadBufferSize( 2048 );

设置idle,其中10应该是10秒
acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );

最后绑定上,ok了
acceptor.bind( new InetSocketAddress(PORT) );


TimeServerHandler类extends了IoHandlerAdapter,其中IoHandlerAdapter只有几个空实现。再F3到IoHandler接口,有挺多的方法。通过名字也挺好辨认的:
sessionCreated,建立session;
sessionOpened,打开session;
sessionClosed,关闭session;
sessionIdle,session闲置;
exceptionCaught,捕捉异常;
messageReceived,接受信息;
messageSent,发送消息。

其中最常用的几个也就算是
    @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 ));
}


这三个了。
messageReceived方法中的两个参数,IoSession session就是session了,Object message就是client发来的信息,如果用
System.out.println(message.getClass());

会发现时String型的,这是因为用到了codec的原因。否则返回的将会是一个IoBuffer。
后面的session.write();就是返回给用户的值
至于后来的sessionIdle,就是设置在session闲置的时候要做的事。

下面接着看echoserver这个例子,main跟最简单的例子基本上一样,
但是
acceptor.getStatistics().getReadBytesThroughput()

这个方法不知道是干什么的,总在控制台输出,很讨厌。而且不论我输入什么,这个值都不变,总是0.0,期待牛人讲解一下。

而在handler里
    
@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);
}

设置了一些在上个例子里写在main里面的东西,说明这样写更加专业。

@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());
}

这里就揭示了为什么会输入一个字符,输出的确实两个
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值