高吞吐量的Java事件总线:MBassador

MBassador是一款在多线程环境中为高吞吐量而优化的功能丰富的java事件总线。注解驱动、同步/异步事件发布,强/弱引用,动态事件过滤。

MBassador是实现发布/订阅模式的轻量,高性能的事件总线。它主要为了能易于使用的丰富的和可扩展的功能,同时保持资源效率和性能。

MBassador的高性能核心是一种专门的数据结构,可提供无阻塞的读写器,并最大限度地减少写入者的锁争用,从而将读/写并发访问性能的损耗降低到最小化。

MBassador的代码是生产就绪的:86%的指令覆盖率,82%的分支覆盖率,高度随机和同时运行的测试集,在过去18个月中没有报告严重的错误。在不彻底测试代码的情况下,不会对内核进行任何修改。

使用很简单,bus = new MBassador()创建单例,配置消息处理器使用@[author][author][author]Handler[/author][/author]注解[/author],然后将其注册到总线中bus.subscribe(aListener),这样就可以开始发送消息bus.post(message).now() 或bus.post(message).asynchronously().


// Define your handlers

@ Listener(references = References.Strong)
class SimpleFileListener{

@ Handler
public void handle(File file){
// do something with the file
}

@ Handler(delivery = Invoke.Asynchronously)
public void expensiveOperation(File file){
// do something with the file
}

@ Handler(condition =
"msg.size >= 10000")
@ Enveloped(messages = {HashMap.class, LinkedList.class})
public void handleLarge(MessageEnvelope envelope) {
// handle objects without common super type
}

}

// somewhere else in your code

MBassador bus = new MBassador();
bus.subscribe (new SimpleFileListener());
bus.post(new File(
"/tmp/smallfile.csv")).now();
bus.post(new File(
"/tmp/bigfile.csv")).asynchronously();

GitHub - bennidi/mbassador: A feature-rich Java ev

性能测试:https://blog.gotofinal.com/java/diorite/benchmark/2017/06/11/event-bus.html
[该贴被banq于2017-06-13 22:23修改过]