您的位置:首页 > 其它

Mina框架学习笔记(一)

2015-04-30 09:19 316 查看
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

[java] view
plaincopy

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来管理事件

[java] view
plaincopy

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: