org.jetlinks.rule.engine.api.EventBus Maven / Gradle / Ivy
package org.jetlinks.rule.engine.api;
import org.jetlinks.rule.engine.api.codec.Codecs;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* 基于订阅发布的事件总线,可用于事件传递,消息转发等.
*
*
* subscribe("/rule-engine/{rule-id}/{task-id}/input")
*
*
* @author zhouhao
* @see 1.1.0
* @see org.jetlinks.core.topic.Topic
*/
public interface EventBus {
/**
* 订阅事件,Topic使用/分割。
*
* @param topic topic
* @param 事件类型泛型
* @param type 事件类型
* @return 事件流
*/
Flux subscribe(String topic, Decoder type);
default Flux subscribe(String topic, Class type) {
return subscribe(topic, Codecs.lookup(type));
}
/**
* 推送事件
*
* @param topic Topic
* @param eventStream 事件流
* @return 接收的订阅者数量
*/
Mono publish(String topic, Encoder encoder, Publisher extends T> eventStream);
default Mono publish(String topic, Encoder encoder, T event) {
return publish(topic, encoder, Mono.just(event));
}
Flux subscribe(String topic);
Mono publish(String topic, Publisher event);
@SuppressWarnings("all")
default Mono publish(String topic, T event) {
return publish(topic, (Encoder) Codecs.lookup(event.getClass()), event);
}
}