开源一个简易轻量的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
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3lhbmd6aGl6aG91/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="reactor" title="">
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 进行预定。
感谢各位朋友。
=========================
开源一个简易轻量的reactor网络框架的更多相关文章
- Snack3 3.2 发布,轻量的Json+Jsonpath框架
Snack3 是一个轻量的 JSON + Jsonpath 框架. 借鉴了 Javascript 所有变量由 var 申明,及 Xml dom 一切都是 Node 的设计.其下一切数据都以ONode表 ...
- Droplet——一款轻量的Golang应用层框架
Github地址 如标题所描述的,Droplet 是一个 轻量 的 中间层框架,何为中间层呢? 通常来说,我们的程序(注意这里我们仅仅讨论程序的范围,而非作为一个系统,因此这里不设计如 LB.Gate ...
- GeoPackage - 一个简便轻量的本地地理数据库
GeoPackage(以下简称gpkg),内部使用SQLite实现的一种单文件.与操作系统无关的地理数据库. 当前标准是1.2.1,该版本的html版说明书:https://www.geopackag ...
- 自己使用的一个.NET轻量开发结构
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIgAAABFCAIAAAAerjlvAAAE2UlEQVR4nO2a3U/bVhiH+bdyPaqpmx
- gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
一个偶然的机会接触到了golang,被它的高并发传说所吸引,就开始学这门语言,越学感觉越有意思^_^ 注册了博客园这么多年,第一次写东西,年纪大了,脑子不好使了,就得写下来,记下来,为了自己以后查阅, ...
- 关于对MyBatis.net框架的学习笔记( MyBatis.net是一款灵活性极大,sql由开发者自行在xml中编写, 轻量的ORM映射框架). 同时避免了sql硬编码到代码中不易维护的问题...
对于为什么要用ORM,为什么又要选择MyBatis.net,这个问题希望读者自行查找资料.这里直接贴出相关的调试笔记. 步骤1)下载与引用. http://code.google.com/p/myba ...
- 简洁、轻量的前端UI框架 - Hbook
Simple, lightweight front-end UI framework Get Start : http://www.bookcss.com Introduce Hbook focus ...
- 比Wireshark更轻量、更方便的抓包软件:Charles
转:http://blog.csdn.net/lixing333/article/details/42776187 之前写过一篇通过Wireshark进行抓包,分析网络连接的文章<通过WireS ...
- 一种简单,轻量,灵活的C#对象转Json对象的方案
简单,是因为只有一个类 轻量,是因为整个类代码只有300行 灵活,是因为扩展方式只需要继承重写某个方法即可 补充:修正无法处理可空值类型的bug 首先我将这个类称之为JsonBuilder,我希望它以 ...
随机推荐
- linux下nc的使用
发送端:cat test.txt | nc -l -p 6666或者nc -l -p 6666 < test.txt 有些版本不要在 -p[监听6666端 ...
- Java学习笔记:具体解释传值和传引用
传值和传引用 When you're passing primitives into a method ,you get a distinct copy of the primitive. When ...
- 提高Java代码质量的Eclipse插件之Checkstyle的使用具体解释
CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发者遵守某些编码规范的工具.它可以自己主动化代码规范检查过程.从而使得开发者从这项重要可是枯燥的任务中解脱出来. Ch ...
- 升级 Elasticsearch 集群数量实战记录
搜索引擎 升级 Elasticsearch 集群数量实战记录 现在线上有一个elasticsearch集群搜索服务有三台elasticsearch实例(es1.es2.es3),打算将其升级为5台(增 ...
- PCL深度图像(1)
目前深度图像的获取方法有激光雷达深度成像法,计算机立体视觉成像,坐标测量机法,莫尔条纹法,结构光法等等,针对深度图像的研究重点主要集中在以下几个方面,深度图像的分割技术 ,深度图像的边缘检测技术 ,基 ...
- spring security笔记
当指定http元素的auto-config=”true”时,就相当于如下内容的简写. <security:http> <security:form-login/> <se ...
- Centos 安装jdk jre
下载rpm http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html wget ht ...
- 【转】android中的数据存取-方式一:preference(配置)
这种方式应该是用起来最简单的Android读写外部数据的方法了.他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单. 透明的方式来保存一些用户个 ...
- Chrome驱动安装问题
1.org.openqa.selenium.WebDriverException: unknown error: call function result missing 'value' 原因:浏览器 ...
- 如何在集群中获得处理本次请求的web容器的端口号?
系统四台机器,每台机器部署四个Tomcat Web容器.现需要根据端口号随机切换到映射的数据源,若一台机器一个Tomcat则用IP识别,可现在一台机器四个Tomcat,因此还需要获得Web容器的端口号 ...