开源一个简易轻量的reactor网络框架

简介: githubhttps://github.com/sea-boat/net-reactornet-reactorit’s a simple and easy net frame...

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内核设计剖析》

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

目录
相关文章
|
29天前
|
存储 分布式计算 监控
Hadoop【基础知识 01+02】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
【4月更文挑战第3天】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
76 2
|
1天前
|
存储 网络协议 Linux
RTnet – 灵活的硬实时网络框架
本文介绍了开源项目 RTnet。RTnet 为以太网和其他传输媒体上的硬实时通信提供了一个可定制和可扩展的框架。 本文描述了 RTnet 的架构、核心组件和协议。
10 0
RTnet – 灵活的硬实时网络框架
|
28天前
|
JSON Kubernetes 网络架构
Kubernetes CNI 网络模型及常见开源组件
【4月更文挑战第13天】目前主流的容器网络模型是CoreOS 公司推出的 Container Network Interface(CNI)模型
|
29天前
|
网络协议 Java API
Python网络编程基础(Socket编程)Twisted框架简介
【4月更文挑战第12天】在网络编程的实践中,除了使用基本的Socket API之外,还有许多高级的网络编程库可以帮助我们更高效地构建复杂和健壮的网络应用。这些库通常提供了异步IO、事件驱动、协议实现等高级功能,使得开发者能够专注于业务逻辑的实现,而不用过多关注底层的网络细节。
|
29天前
|
分布式计算 监控 Hadoop
Hadoop【基础知识 02】【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
【4月更文挑战第3天】Hadoop【基础知识 02】【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
58 0
|
2月前
|
XML 网络协议 前端开发
Netty网络框架(三)
Netty网络框架
28 1
|
2月前
|
存储 编解码 网络协议
Netty网络框架(二)
Netty网络框架
37 0
|
存储 设计模式 网络协议
Netty网络框架(一)
Netty网络框架
39 1
|
2月前
|
网络协议 安全 网络安全
网络基础与通信原理:构建数字世界的框架
网络基础与通信原理:构建数字世界的框架
48 1
|
3月前
|
机器学习/深度学习 计算机视觉 异构计算
【CVPR2023】Backbone FasterNet:我不允许你不知道世界上还有比ShuffleNetV2还轻量的网络!
【CVPR2023】Backbone FasterNet:我不允许你不知道世界上还有比ShuffleNetV2还轻量的网络!
73 0
【CVPR2023】Backbone FasterNet:我不允许你不知道世界上还有比ShuffleNetV2还轻量的网络!