开源一个简易轻量的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,我希望它以 ...
随机推荐
- Web前端开发测试题阅读笔记
引自: http://www.w3cplus.com/css/front-end-web-development-quiz.html Q7:下面代码弹出值是什么? x = 1; function ba ...
- 【C】——APUE小程序之递归遍历目录
递归降序遍历目录层次结构,并按文件类型计数. 先介绍相关的函数: #include<dirent.h> DIR *opendir(const char *pathname); //打开目录 ...
- Mongo的备份和恢复(mongodump 和mongorestore )
http://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html --备份单个表mongodump -u superuser -p 1 ...
- jquery easyui toolbar 分割线问题
http://bbs.csdn.net/topics/390507228 —————————————————————————————————— 将“<div class="datagr ...
- 解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content
A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常. java.lang.IllegalStateException: The application's ...
- 如何能够通过代码来重启Android手机?
String cmd = "su -c reboot";//让手机从启 try { Runtime.getRuntime().exec(cmd); } catch (IOExcep ...
- python 调用 C语言函数
python可以直接调用C语言的函数,本文记录用ctypes调用c语言的方法. test.c #include <stdio.h> int test(char *temp) { print ...
- Oracle数据库的语句级读一致性
数据库:Oracle 前提:假定100万行的记录,在最后一行有一个数据a=1. 实验:你在9:00的时候查找数据,9:05的时候查到最后一行:但是,在9:01的时候有人修改了最后一行数据并提交(com ...
- elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))
一.分词器 1. 认识分词器 1.1 Analyzer 分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...
- Zookeeper系统设计的优点
转自:Zookeeper系统设计的优点.http://webcache.googleusercontent.com/search?q=cache:s6fr40t_5ncJ:www.chaozh.com ...