Browse Source

异步事件增加优先级

zhou-hao 3 years ago
parent
commit
fb46f045c1

+ 6 - 0
hsweb-core/src/main/java/org/hswebframework/web/event/AsyncEvent.java

@@ -21,6 +21,12 @@ public interface AsyncEvent {
      */
     void async(Publisher<?> publisher);
 
+    /**
+     * 注册一个优先级高的任务
+     * @param publisher 任务
+     */
+    void first(Publisher<?> publisher);
+
     /**
      * 推送事件到 ApplicationEventPublisher
      *

+ 9 - 1
hsweb-core/src/main/java/org/hswebframework/web/event/DefaultAsyncEvent.java

@@ -9,6 +9,8 @@ public class DefaultAsyncEvent implements AsyncEvent {
 
     @Getter
     private Mono<Void> async = Mono.empty();
+    @Getter
+    private Mono<Void> first = Mono.empty();
 
     private boolean hasListener;
 
@@ -17,12 +19,18 @@ public class DefaultAsyncEvent implements AsyncEvent {
         this.async = async.then(Mono.from(publisher).then());
     }
 
+    @Override
+    public synchronized void first(Publisher<?> publisher) {
+        hasListener = true;
+        this.first = Mono.from(publisher).then(first);
+    }
+
     @Override
     public Mono<Void> publish(ApplicationEventPublisher eventPublisher) {
 
         eventPublisher.publishEvent(this);
 
-        return this.async;
+        return this.first.then(this.async);
     }
 
     public boolean hasListener() {