Solon v3.5.2

dami - bus 之通用事件(send)

</> markdown

1、发送(通用事件)与监听(通用事件)

public class DemoApp {
    static String topic = "demo.hello";

    public static void main(String[] args) {
        //监听事件
        Dami.bus().listen(topic, event -> {
            System.err.println(event.getPayload());
        });

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

2、使用泛型(自由定制的基础)

泛型可指定“荷载”(payload)的类型

public class DemoApp {
    static String topic = "demo.hello";

    public static void main(String[] args) {
        //监听事件
        Dami.bus().<String>listen(topic, event -> {
            System.err.println(event.getPayload());
        });

        //发送事件
        Dami.bus().<String>send(topic, "hello");
    }
}

3、使用附件数据

发送事件时,可使用 topic + playload 分开发送,也可使用 event 整体发送。使用 event 整体发送时,可以添加附件。

public class DemoApp {
    static String topic = "demo.hello";

    public static void main(String[] args) {
        //监听事件
        Dami.bus().<String>listen(topic, event -> {
            System.err.println(event.getPayload());
            System.err.println(event.getAttach()); //附件
        });

        //发送事件
        Dami.bus().send(new SimpleEvent<>(topic, "hello", Map.of("from", "noear")));
    }
}