Mina框架学习笔记(一)

Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.

Apache MINA is often called:

NIO framework · library,
client · server framework · library, or
a networking · socket library.
On Saturday, 25 September 2010, the Apache MINA project is pleased to announce MINA 2.0.0 ! This version fixes many issues found since we released 2.0.0-RC1.

We recommend all users to upgrade to this release. We consider this a stable and production ready release.

这是官方网上的说明。

其实之前已经学过Mina了,但现在也忘得差不多了,趁Apache发布新版本的时候,学一学新版本上的东西,顺便复习一下以前学过的知识。

本文是入门篇,先来写一个简单的socket server服务器程序,可以用telnet来连接,当服务器收到客户端的连接时会把服务器上的时间返回给客户。

首先引入四个包:

mina-core-2.0.0.jar

slf4j-api-1.6.1.jar

slf4j-jdk14-1.6.1.jar

slf4j-log4j12-1.6.1.jar

接着创建一个主服务类: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 {
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();

//This filter will log all information such as newly created
//sessions, messages received, messages sent, session closed
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
//This filter will translate binary or protocol specific data into
//message object and vice versa. We use an existing TextLine
//factory because it will handle text base message for you (
//you don't have to write the codec part)
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("UTF-8"))));

acceptor.setHandler(new TimeServerHandler());

acceptor.getSessionConfig().setReadBufferSize(BUF_SIZE);
//the first parameter defines what actions to check for when
//determining if a session is idle, the second parameter defines
//the length of time in seconds that must occur before a session
//is deemed to be idle.
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

acceptor.bind(new InetSocketAddress(PORT));
}
private static final int PORT = 8181,BUF_SIZE = 2048;
}


再创建一个handler来管理事件

view plaincopy to clipboardprint?
package mina;
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();
}
@SuppressWarnings("deprecation")
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String str = message.toString();
System.out.println("Message received:"+str);
if(str.trim().equalsIgnoreCase("quit")){
session.close();
return;
}

Date date = new Date();
session.write(date.toString());
System.out.println("Message written.");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
super.sessionCreated(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("IDLE"+session.getIdleCount(status));
}
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionOpened(session);
}

}



运行MinaTimeServer,再用telnet来连接,命令格式如:telnet 127.0.0.1 8181

(第一节结束,第二节待续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值