Skip to content

DamiBus 0.51,本地过程调用框架(主打解耦)

Choose a tag to compare

@noear noear released this 13 Oct 07:26
· 214 commits to main since this release

本次更新:

  • 删除 Dami::intercept 接口,直接使用 Dami.bus()::intercept
  • 更名 sendAndResponse 为 sendAndRequest!!!
  • 更名 sendAndCallback 为 sendAndSubscribe!!!
  • 添加 TopicDispatcher,将调度处理与路由器开发(更方便添加监控)
  • 添加 InterceptorChain::getTargets 接口(可以知道有哪些订阅)
  • 添加 Acceptor::isSingle 接口,用于识别单发还是多发接收
  • 添加 Payload::isSubscribe 接口,用于识别订阅
  • 调整 启用新的配置方式(配置路由时,不需要重新实例化总线)
  • 调整 接口默认方法处理策略(有订阅执行订阅,无订阅者执行默认)
  • 调整 createSender 调整类加载器的主体
  • 调整 send,sendAndSubscribe 返回类型改为 bool(马上可以知道是否有订阅目标)

新增加的监视效果,基于拦截器:

public class Demo16_intercept {
    @Test
    public void main() throws Exception {
        Dami.bus().intercept((payload, chain) -> {
            System.out.println("开始监视...");
            if (chain.getTargets().size() == 0) {
                System.out.println("没有订阅...");
            } else {
                chain.getTargets().forEach(e -> System.out.println(e.getListener()));
                chain.doIntercept(payload);
            }
            System.out.println("结速监视...");
        });

        //发送事件
        Dami.bus().send("demo.hello", "world");
    }
}

另一个方案是用重载调度器的方法:

public class Demo16_dispatcher {
    @Test
    public void main() throws Exception {
        DamiConfig.configure(new TopicDispatcherMonitor());
        
        Dami.bus().send("demo.hello", "world");
    }
}

public class TopicDispatcherMonitor<C,R> extends TopicDispatcherDefault<C,R> {
    @Override
    protected void doDispatch(Payload<C, R> payload, List<TopicListenerHolder<C, R>> targets) throws Throwable {
        //开始监视...
        System.out.println("开始监视...");

        //用 i,可以避免遍历时添加监听的异常
        for (int i = 0; i < targets.size(); i++) {
            TopicListener<Payload<C,R>> listener = targets.get(i).getListener();

            //发送前监视...
            System.out.println("发送前监视...");
            listener.onEvent(payload);
            //发送后监视...
            System.out.println("发送后监视...");
        }

        //结速监视...
        System.out.println("结速监视...");
    }
}