MINA2.0 实例--TimeServer(二)

2014-11-24 10:11:59 · 作者: · 浏览: 1
re you missing a protocol encoder ”;设置具体的处理程序,即SimpleTimeServerHandler.java,并绑定到相应的监听端口,地址默认是本机的IP。

3). 停止服务器,则客户端窗口会直接“遗失对主机的连接”。

4). 本实例尽量做到最简单化,因此没有开启日志打印,官方的版本(包含在apache-mina-2.0.7-src.zip的“apache-mina-2.0.7\src\mina-example\src\main\java\org\apache\mina\example\gettingstarted\timeserver”中),如下:

除了mina-core-2.0.7.jar和slf4j-api-1.6.6.jar,还需引入slf4j-simple-1.6.6.jar,该jar包是slf4j的默认日志处理包;类包含两个:MinaTimeServer.java和TimeServerHandler.java,分别如下:


[java]
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;

/**
* The Time Server handler : it return the current date when a message is received,
* or close the session if the "quit" message is received.
*
* @author Apache MINA Project
*/
public class TimeServerHandler extends IoHandlerAdapter
{
/**
* Trap exceptions.
*/
@Override
public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
{
cause.printStackTrace();
}

/**
* If the message is 'quit', we exit by closing the session. Otherwise,
* we return the current date.
*/
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
{
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...");
}

/**
* On idle, we just write a message on the console
*/
@Override
public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
{
System.out.println( "IDLE " + session.getIdleCount( status ));
}
}

[java]
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;

/**
* A minimal 'time' server, returning the current date. Opening
* a telnet server, you will get the current date by typing
* any string followed by a new line.
*
* In order to quit, just send the 'quit' message.
*
* @author Apache MINA Project
*/
public class MinaTimeServer {
/** We will use a port above 1024 to be able to launch the server with a standard user */
private static final int PORT = 9123;

/**
* The server implementation. It's based on TCP, and uses a logging filter
* plus a text line decoder.
*/
public static void main(String[] args) throws IOException {
// Create the acceptor
IoAcceptor acceptor = new NioSocketAcceptor();

// Add two filters : a logger and a codec
acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset