APACHE MINA研究

    技术2022-05-12  0

    从一个简单的EchoProtocolHandler入手

    程序EchoProtocolHandler

    /**

    *    @className    EchoProtocolHandler.java

    *    @author     gaoling

    *    @createtime 2009-下午05:03:04

    *    @version     TODO

    *    @purpose     TODO

    *    

    *    @Notes         TODO

    *    @Example:    TODO

    */

    package com.gaoling.minatest3;

     

    import org.apache.mina.core.buffer.IoBuffer;

    import org.apache.mina.core.service.IoHandler;

    import org.apache.mina.core.service.IoHandlerAdapter;

    import org.apache.mina.core.session.IdleStatus;

    import org.apache.mina.core.session.IoSession;

    import org.apache.mina.filter.ssl.SslFilter;

    import org.slf4j.Logger;

    import org.slf4j.LoggerFactory;

     

    public class EchoProtocolHandler extends IoHandlerAdapter {

    private final Logger logger = LoggerFactory.getLogger(getClass());

     

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

    }

     

    @Override

    public void sessionClosed(IoSession session) throws Exception {

    logger.info("CLOSED");

    }

     

    @Override

    public void sessionOpened(IoSession session) throws Exception {

    logger.info("OPENED");

    }

     

    @Override

    public void sessionIdle(IoSession session, IdleStatus status) {

    logger.info("*** IDLE #" + session.getIdleCount(IdleStatus.BOTH_IDLE) + " ***");

     

    }

     

    @Override

    public void exceptionCaught(IoSession session, Throwable cause) {

    session.close(true);

    }

     

    @Override

    public void messageReceived(IoSession session, Object message)

    throws Exception {

    // Write the received data back to remote peer

            //这个message org.apache.mina.core.buffer.SimpleBufferAllocator$SimpleBuffer的实例

        System.out.println("打印消息:"+message);

    session.write(((IoBuffer) message).duplicate());

    }

    }

     

    程序main

    /**

    *    @className    Main.java

    *    @author     gaoling

    *    @createtime 2009-下午05:05:12

    *    @version     TODO

    *    @purpose     TODO

    *    

    *    @Notes         TODO

    *    @Example:    TODO

    */

    package com.gaoling.minatest3;

     

    import java.net.InetSocketAddress;

    import java.nio.channels.Selector;

    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;

    import org.apache.mina.core.service.AbstractIoService;

    import org.apache.mina.core.service.IoService;

    import org.apache.mina.core.service.IoServiceListener;

    import org.apache.mina.core.service.IoServiceStatistics;

    import org.apache.mina.core.service.SimpleIoProcessorPool;

    import org.apache.mina.core.session.IoSessionConfig;

    import org.apache.mina.filter.ssl.SslFilter;

    import org.apache.mina.transport.socket.DefaultSocketSessionConfig;

    import org.apache.mina.transport.socket.SocketAcceptor;

    import org.apache.mina.transport.socket.nio.NioProcessor;

    import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

     

    import java.util.concurrent.Executor;

    import java.util.concurrent.Executors;;

     

    public class Main {

    /** 监听端口号*/

    private static final int PORT = 5678;

     

    public static void main(String[] args) throws Exception {

        //启动一个service

    SocketAcceptor acceptor = new NioSocketAcceptor();

    //绑定一个处理handle

    acceptor.setHandler(new EchoProtocolHandler());

    //绑定一个端口

    acceptor.bind(new InetSocketAddress(PORT));

    }

     

    }

    自启一个客户端,执行结果


    最新回复(0)