zoukankan      html  css  js  c++  java
  • 开源一个简易轻量的reactor网络框架

    github

    https://github.com/sea-boat/net-reactor

    net-reactor

    it’s a simple and easy net framework with nio mode written by java

    reactor model

    reactor

    how-to

    just simply like:

    public class MyHandler implements Handler {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
        private long readSize;
    
        /**
         * The logic to deal with the received data.
         *  
         * It means that reactor will trigger this function once the data is received.
         * @throws IOException 
         */
        public void handle(FrontendConnection connection) throws IOException {
            Buffer buff = connection.getReadBuffer();
            readSize = +readSize + buff.position();
            LOGGER.info(connection.getId() + " connection has receive " + readSize);
    
        }
    
    }
    Handler handler = new MyHandler();
    ReactorPool reactorPool = new ReactorPool(Runtime.getRuntime().availableProcessors(), handler);
    new Acceptor(reactorPool, acceptorName, host, port).start();

    adding a connection event or a connection multi-event:

    public class RegisterHandler implements ConnectionEventHandler {
        private static final Logger LOGGER = LoggerFactory
                .getLogger(RegisterHandler.class);
    
        private static int INTERESTED = ConnectionEvents.REGISTE;
    
        public void event(FrontendConnection connection) {
            if ((event & INTERESTED) != 0) {
                //do something here 
            }
        }
    
    }
    Handler handler = new NetHandler();
    ConnectionEventHandler connectionEventHandler = new RegisterHandler();
    ReactorPool reactorPool = new ReactorPool(Runtime.getRuntime().availableProcessors(), handler);
    Acceptor acceptor = new Acceptor(reactorPool, acceptorName, host, port);
    acceptor.addConnectionEventHandler(connectionEventHandler);
    acceptor.start();
    public class ConnectionLogHandler implements ConnectionEventHandler {
        private static final Logger LOGGER = LoggerFactory
                .getLogger(ConnectionLogHandler.class);
        private static int INTERESTED = ConnectionEvents.ACCEPT
                | ConnectionEvents.CLOSE;
    
        public void event(Connection connection, int event) {
            if ((event & INTERESTED) != 0) {
                if ((event & ConnectionEvents.ACCEPT) != 0)
                    LOGGER.info("accept connection,id is " + connection.getId());
                if ((event & ConnectionEvents.CLOSE) != 0)
                    LOGGER.info("close connection,id is " + connection.getId());
            }
        }
    }

    implements the connection

    public class XXXConnection extends Connection {
    
        private String name;
    
        public XXXConnection(SocketChannel channel, long id, Reactor reactor) {
            super(channel, id, reactor);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    public class XXXConnectionFactory implements ConnectionFactory {
    
        public XXXConnection createConnection(SocketChannel channel, long id,
                Reactor reactor) {
            return new XXXConnection(channel, id, reactor);
        }
    
    }
    Acceptor acceptor = new Acceptor(reactorPool, acceptorName, host,port);
    acceptor.setConnectionFactory(new xxxConnectionFactory());

    ========广告时间========

    鄙人的新书《Tomcat内核设计剖析》已经在京东销售了。有须要的朋友能够到 https://item.jd.com/12185360.html 进行预定。

    感谢各位朋友。

    为什么写《Tomcat内核设计剖析》

    =========================

  • 相关阅读:
    shell编程基础
    centos7怎么查看、打开和关闭防火墙
    Linux下查看根目录各文件内存占用情况
    两台Linux系统之间传输文件的几种方法
    rabbitmq3.8版本默认用户guest访问报错User can only log in via localhost解决方案
    docker 部署java web应用_使用Docker部署JavaWeb项目
    ubuntu安装curl命令
    docker重新进入容器时“/etc/profile”中环境变量失效问题的解决
    在docker容器中安装软件,并重新生成镜像
    CentOS7安装RabbitMQ
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7375302.html
Copyright © 2011-2022 走看看