这个Mina Echo Server包括服务器主类Main, 两个Filter类:MyFilter1, MyFilter2, 和一个handler类:EchoProtocolHandler。
import java.net.InetSocketAddress;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class Main {
public static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("myfilter1", new MyFilter1());
acceptor.getFilterChain().addLast("myfilter2", new MyFilter2());
acceptor.setHandler(new EchoProtocolHandler());
acceptor.bind(new InetSocketAddress(8293));
System.out.println("Listening on port " + PORT);
}
}
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyFilter1 implements IoFilter {
private static final Logger LOGGER = LoggerFactory
.getLogger(MyFilter1.class);
public void messageReceived(NextFilter nextFilter, IoSession session,
Object message) throws Exception {
LOGGER.info("MessageReceived!");
nextFilter.messageReceived(session, message);
}
public void messageSent(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest) throws Exception {
LOGGER.info("messageSent!");
nextFilter.messageSent(session, writeRequest);
}
public void filterWrite(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest) throws Exception {
LOGGER.info("filterWrite!");
nextFilter.filterWrite(session, writeRequest);
}
// some other methods must implemented.
}
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyFilter2 implements IoFilter {
private static final Logger LOGGER = LoggerFactory
.getLogger(MyFilter2.class);
public void messageReceived(NextFilter nextFilter, IoSession session,
Object message) throws Exception {
LOGGER.info("MessageReceived!");
nextFilter.messageReceived(session, message);
}
public void messageSent(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest) throws Exception {
LOGGER.info("messageSent!");
nextFilter.messageSent(session, writeRequest);
}
public void filterWrite(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest) throws Exception {
LOGGER.info("filterWrite!");
nextFilter.filterWrite(session, writeRequest);
}
// some other methods must implemented.
}
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EchoProtocolHandler extends IoHandlerAdapter {
private static final Logger LOGGER = LoggerFactory
.getLogger(EchoProtocolHandler.class);
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
session.close(true);
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
LOGGER.info("Received : " + message);
session.write(((IoBuffer) message).duplicate());
}
@Override
public void messageSent(IoSession session, Object message) {
LOGGER.info("messageSent " + message);
}
}
用XShell运行telnet localhost 8293,发一个字符串"hi",运行结果如下:
Listening on port 8293
[INFO] [2012-03-31 14:03:38,575] [com.leechau.mina.echoServer.MyFilter1] - [MessageReceived!]
[INFO] [2012-03-31 14:03:38,576] [com.leechau.mina.echoServer.MyFilter2] - [MessageReceived!]
[INFO] [2012-03-31 14:03:38,577] [com.leechau.mina.echoServer.EchoProtocolHandler] - [Received : HeapBuffer[pos=0 lim=4 cap=2048: 68 69 0D 0A]]
[INFO] [2012-03-31 14:03:38,578] [com.leechau.mina.echoServer.MyFilter2] - [filterWrite!]
[INFO] [2012-03-31 14:03:38,578] [com.leechau.mina.echoServer.MyFilter1] - [filterWrite!]
[INFO] [2012-03-31 14:03:38,580] [com.leechau.mina.echoServer.MyFilter1] - [messageSent!]
[INFO] [2012-03-31 14:03:38,580] [com.leechau.mina.echoServer.MyFilter2] - [messageSent!]
[INFO] [2012-03-31 14:03:38,580] [com.leechau.mina.echoServer.EchoProtocolHandler] - [messageSent HeapBuffer[pos=0 lim=4 cap=2048: 68 69 0D 0A]]
可以看到messageReceived事件从Filter链顶端传播到handler;filterWrite事件发生在handler开始发送消息,但还未写入socket时,仅对Filter链有效,不作用于handler,从链尾向链首传播;messageSent事件发生在socket已被写入之后,从链顶端传播到handler;