DamiBus,专为单体多模块之间通讯解耦而设计(尤其是未知模块、隔离模块、领域模块)。零依赖。
特点
结合 Bus 与 RPC 的概念,可作事件分发,可作接口调用。
- 支持事务传导(同步分发、异常透传)
- 支持事件标识、拦截器(方便跟踪)
- 支持监听者排序、附件传递(多监听时,可相互合作)
- 支持 Bus 和 Api 两种体验风格
与常见的 EventBus、ApiBean 的区别
DamiBusEventBusApiDamiBus 的情况说明广播有有无发送(send) + 监听(listen)
以及 Api 模式应答有无有调用(call) + 监听(listen) + 答复(reply)
以及 Api 模式耦合弱-弱+强++如果涉及类加载器隔离:请在主程序标为编译,在其它模块标为可选。
本次更新了什么?
新版本简化了体验!
- 调整 DamiBus:sendAndSubscribe 标为弃用
- 调整 DamiBus:sendAndRequest 标为弃用(由 call 替代)
- 调整 Payload:isSubscribe 标为弃用
- 调整 Payload:isRequest 标为弃用(由 requiredReply 替代)
- 添加 DamiBus:call 方法,意为调用(要求有一个答复)
- 添加 Payload:requiredReply 方法,意为要求答复(或必须答复)
性能测试?
瞬发 1000万个事件,1秒左右可发完(基于 jdk11 测试):- public class SendTest {
- static Integer count = 0;
- public static void main(String[] args) {
- Dami.bus().listen("test.demo", e -> { count = count + 1; });
- long start = System.currentTimeMillis();
- for (int i = 0; i < 10_000_000; i++) {
- Dami.bus().send("test.demo", "1");
- }
- System.out.println(System.currentTimeMillis() - start + "::" + count);
- }
- }
复制代码 疑问:
为什么不用分布式消息队列呢?定位不同(侧重,单体多模块解耦)。
示例
demo21_send
- //总线风格。bus()
- public class Deom11 {
- static String topic = "demo.hello";
- public static void main(String[] args) {
- //监听事件
- Dami.bus().listen(topic, payload -> {
- System.err.println(payload); //可以有多个订阅
- });
- Dami.bus().listen(topic, payload -> {
- CompletableFuture.runAsync(()-> { //也可以异步消费
- System.err.println(payload);
- });
- });
- //发送事件
- Dami.bus().send(topic, "{name:'noear',say:'hello'}");
- }
- }
复制代码 demo12_call
- //泛型总线风格。<C,R>bus()
- public class Demo12 {
- static String topic = "demo.hello";
- public static void main(String[] args) {
- //监听事件
- Dami.<String,String>bus().listen(topic, payload -> {
- System.err.println(payload);
- if (payload.requiredReply()) {
- payload.reply("hi!");
- }
- });
- //发送事件 //要求有答复(即,返回值)
- String rst1 = Dami.<String,String>bus().call(topic, "world");
- //发送事件 //要求有答复(即,返回值) //支持默认值(没有订阅时触发)
- //String rst1 = Dami.<String,String>bus().call(topic, "world", ()->"demo");
- System.out.println(rst1);
- }
- }
复制代码 项目地址
- https://gitee.com/noear/dami
- https://github.com/noear/dami
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |