找回密码
 立即注册
首页 业界区 业界 DamiBus v1.1.0 发布(给单体多模块解耦)

DamiBus v1.1.0 发布(给单体多模块解耦)

副我 2025-9-16 08:59:51
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 测试):
  1. public class SendTest {
  2.     static Integer count = 0;
  3.     public static void main(String[] args) {
  4.         Dami.bus().listen("test.demo", e -> { count = count + 1; });
  5.         long start = System.currentTimeMillis();
  6.         for (int i = 0; i < 10_000_000; i++) {
  7.             Dami.bus().send("test.demo", "1");
  8.         }
  9.         System.out.println(System.currentTimeMillis() - start + "::" + count);
  10.     }
  11. }
复制代码
疑问:
为什么不用分布式消息队列呢?定位不同(侧重,单体多模块解耦)。
示例

demo21_send
  1. //总线风格。bus()
  2. public class Deom11 {
  3.     static String topic = "demo.hello";
  4.     public static void main(String[] args) {
  5.         //监听事件
  6.         Dami.bus().listen(topic, payload -> {
  7.             System.err.println(payload); //可以有多个订阅
  8.         });
  9.         Dami.bus().listen(topic, payload -> {
  10.             CompletableFuture.runAsync(()-> { //也可以异步消费
  11.                 System.err.println(payload);
  12.             });
  13.         });
  14.         //发送事件
  15.         Dami.bus().send(topic, "{name:'noear',say:'hello'}");
  16.     }
  17. }
复制代码
demo12_call
  1. //泛型总线风格。<C,R>bus()
  2. public class Demo12 {
  3.     static String topic = "demo.hello";
  4.     public static void main(String[] args) {
  5.         //监听事件
  6.         Dami.<String,String>bus().listen(topic, payload -> {
  7.             System.err.println(payload);
  8.             if (payload.requiredReply()) {
  9.                 payload.reply("hi!");
  10.             }
  11.         });
  12.         //发送事件 //要求有答复(即,返回值)
  13.         String rst1 = Dami.<String,String>bus().call(topic, "world");
  14.         //发送事件 //要求有答复(即,返回值) //支持默认值(没有订阅时触发)
  15.         //String rst1 = Dami.<String,String>bus().call(topic, "world", ()->"demo");
  16.         System.out.println(rst1);
  17.     }
  18. }
复制代码
项目地址


  • https://gitee.com/noear/dami
  • https://github.com/noear/dami

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册