开源一个简易轻量的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());

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

公众号的菜单已分为“分布式”、“机器学习”、“深度学习”、“NLP”、“Java深度”、“Java并发核心”、“JDK源码”、“Tomcat内核”等,可能有一款适合你的胃口。

鄙人的新书《Tomcat内核设计剖析》已经在京东销售了,有需要的朋友可以购买。感谢各位朋友。

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

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超人汪小建(seaboat)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值