flow - 事件总线(事件广播、解耦)
流事件总线(基于大米事件总线 - DamiBus),是上下文级别的(或实例级别的)。可以为扩展提供“解耦”支持。
# flow/f1.chain.yml
id: f1
layout:
- task: |
//发送事件
context.eventBus().send("demo.topic", "hello1");
- task: |
//发送事件并做为请求(要求给答复)//使用泛型
String rst = context.<String,String>eventBus().sendAndRequest("demo.topic", "hello2");
System.out.println(rst);
- task: |
//发送事件并做为请求(要求给答复)//使用泛型 //再使用默认值(如果没有订阅)
String rst = context.<String,String>eventBus().sendAndRequest("demo.topic", "hello2", ()-> "def");
System.out.println(rst);
代码应用:
public class DemoTest {
@Test
public void case1() throws Exception {
FlowEngine flowEngine = FlowEngine.newInstance();
flowEngine.load("classpath:flow/*.yml");
FlowContext context = new FlowContext();
//事件监听(即订阅)
context.eventBus().listen("demo.topic", event -> {
System.out.println(event.getContent());
if(event.isRequest()){
event.reply("ok"); //如果是请求,则给予答复
}
});
flowEngine.eval("f1", context);
}
}