浏览代码

优化事件处理

zhou-hao 3 年之前
父节点
当前提交
4d374489dd

+ 67 - 0
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/events/EntityEventHelper.java

@@ -1,9 +1,18 @@
 package org.hswebframework.web.crud.events;
 
+import org.hswebframework.web.api.crud.entity.Entity;
+import org.hswebframework.web.event.AsyncEvent;
+import org.hswebframework.web.event.GenericsPayloadApplicationEvent;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.util.context.Context;
 
+import java.util.List;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
 /**
  * 实体事件帮助器
  *
@@ -57,4 +66,62 @@ public class EntityEventHelper {
     public static <T> Flux<T> setDoNotFireEvent(Flux<T> stream) {
         return stream.subscriberContext(Context.of(doEventContextKey, false));
     }
+
+    public static <T> Mono<Void> publishSavedEvent(Object source,
+                                                   Class<T> entityType,
+                                                   List<T> entities,
+                                                   Consumer<GenericsPayloadApplicationEvent<EntitySavedEvent<T>>> publisher) {
+        return publishEvent(source, entityType, () -> new EntitySavedEvent<>(entities, entityType), publisher);
+    }
+
+    public static <T extends Entity> Mono<Void> publishModifyEvent(Object source,
+                                                                   Class<T> entityType,
+                                                                   List<T> before,
+                                                                   Consumer<T> afterTransfer,
+                                                                   Consumer<GenericsPayloadApplicationEvent<EntityModifyEvent<T>>> publisher) {
+        return publishEvent(source,
+                            entityType,
+                            () -> new EntityModifyEvent<>(before,
+                                                          before
+                                                                  .stream()
+                                                                  .map(t -> t.copyTo(entityType))
+                                                                  .peek(afterTransfer)
+                                                                  .collect(Collectors.toList()),
+                                                          entityType),
+                            publisher);
+    }
+
+    public static <T> Mono<Void> publishModifyEvent(Object source,
+                                                    Class<T> entityType,
+                                                    List<T> before,
+                                                    List<T> after,
+                                                    Consumer<GenericsPayloadApplicationEvent<EntityModifyEvent<T>>> publisher) {
+        return publishEvent(source, entityType, () -> new EntityModifyEvent<>(before, after, entityType), publisher);
+    }
+
+    public static <T> Mono<Void> publishDeletedEvent(Object source,
+                                                     Class<T> entityType,
+                                                     List<T> entities,
+                                                     Consumer<GenericsPayloadApplicationEvent<EntityDeletedEvent<T>>> publisher) {
+        return publishEvent(source, entityType, () -> new EntityDeletedEvent<>(entities, entityType), publisher);
+    }
+
+    public static <T> Mono<Void> publishCreatedEvent(Object source,
+                                                     Class<T> entityType,
+                                                     List<T> entities,
+                                                     Consumer<GenericsPayloadApplicationEvent<EntityCreatedEvent<T>>> publisher) {
+        return publishEvent(source, entityType, () -> new EntityCreatedEvent<>(entities, entityType), publisher);
+    }
+
+    public static <T, E extends AsyncEvent> Mono<Void> publishEvent(Object source,
+                                                                    Class<T> entityType,
+                                                                    Supplier<E> eventSupplier,
+                                                                    Consumer<GenericsPayloadApplicationEvent<E>> publisher) {
+        E event = eventSupplier.get();
+        if (event == null) {
+            return Mono.empty();
+        }
+        publisher.accept(new GenericsPayloadApplicationEvent<>(source, event, entityType));
+        return event.getAsync();
+    }
 }

+ 47 - 38
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/events/EntityEventListener.java

@@ -4,6 +4,7 @@ package org.hswebframework.web.crud.events;
 import lombok.AllArgsConstructor;
 import org.apache.commons.beanutils.BeanUtilsBean;
 import org.apache.commons.collections.CollectionUtils;
+import org.hswebframework.ezorm.core.GlobalConfig;
 import org.hswebframework.ezorm.core.param.QueryParam;
 import org.hswebframework.ezorm.rdb.events.*;
 import org.hswebframework.ezorm.rdb.events.EventListener;
@@ -30,6 +31,8 @@ import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.BiFunction;
 import java.util.function.Supplier;
 
+import static org.hswebframework.web.crud.events.EntityEventHelper.*;
+
 @SuppressWarnings("all")
 @AllArgsConstructor
 public class EntityEventListener implements EventListener {
@@ -155,12 +158,9 @@ public class EntityEventListener implements EventListener {
                                     //set null
                                     for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                                         if (stringObjectEntry.getValue() == null || stringObjectEntry.getValue() instanceof NullValue) {
-                                            try {
-                                                BeanUtilsBean
-                                                        .getInstance()
-                                                        .setProperty(data, stringObjectEntry.getKey(), null);
-                                            } catch (Throwable ignore) {
-                                            }
+                                            GlobalConfig
+                                                    .getPropertyOperator()
+                                                    .setProperty(data, stringObjectEntry.getKey(), null);
                                         }
                                     }
                                     return data;
@@ -181,18 +181,19 @@ public class EntityEventListener implements EventListener {
                                          Class<Object> type,
                                          Function3<List<Object>, List<Object>, Class<Object>, AsyncEvent> mapper) {
 
-        AsyncEvent event = mapper.apply(before, after, type);
-        eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, event, type));
-        return event.getAsync();
+        return publishEvent(this,
+                            type,
+                            () -> mapper.apply(before, after, type),
+                            eventPublisher::publishEvent);
     }
 
     protected Mono<Void> sendDeleteEvent(List<Object> olds,
                                          Class<Object> type,
                                          BiFunction<List<Object>, Class<Object>, AsyncEvent> eventBuilder) {
-
-        AsyncEvent deletedEvent = eventBuilder.apply(olds, type);
-        eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, deletedEvent, type));
-        return deletedEvent.getAsync();
+        return publishEvent(this,
+                            type,
+                            () -> eventBuilder.apply(olds, type),
+                            eventPublisher::publishEvent);
     }
 
     protected void handleUpdateBefore(DSLUpdate<?, ?> update, EventContext context) {
@@ -248,17 +249,14 @@ public class EntityEventListener implements EventListener {
                        if (isEnabled(entityType, EntityEventType.modify, EntityEventPhase.after)) {
                            holder.after(v -> this
                                    .doAsyncEvent(() -> {
-                                       return Mono
-                                               .defer(() -> {
-                                                   Tuple2<List<Object>, List<Object>> _tmp = updated.getAndSet(null);
-                                                   if (_tmp != null) {
-                                                       return sendUpdateEvent(_tmp.getT1(),
-                                                                              _tmp.getT2(),
-                                                                              entityType,
-                                                                              EntityModifyEvent::new);
-                                                   }
-                                                   return Mono.empty();
-                                               });
+                                       Tuple2<List<Object>, List<Object>> _tmp = updated.getAndSet(null);
+                                       if (_tmp != null) {
+                                           return sendUpdateEvent(_tmp.getT1(),
+                                                                  _tmp.getT2(),
+                                                                  entityType,
+                                                                  EntityModifyEvent::new);
+                                       }
+                                       return Mono.empty();
                                    }));
                        }
 
@@ -364,8 +362,10 @@ public class EntityEventListener implements EventListener {
                            if (null != prepareEvent && isEnabled(clazz, entityEventType, EntityEventPhase.prepare)) {
                                holder.before(
                                        this.doAsyncEvent(() -> {
-                                           eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, prepareEvent, clazz));
-                                           return prepareEvent.getAsync();
+                                           return publishEvent(this,
+                                                               clazz,
+                                                               () -> prepareEvent,
+                                                               eventPublisher::publishEvent);
                                        })
                                );
                            }
@@ -373,16 +373,20 @@ public class EntityEventListener implements EventListener {
                            if (null != beforeEvent && isEnabled(clazz, entityEventType, EntityEventPhase.before)) {
                                holder.invoke(
                                        this.doAsyncEvent(() -> {
-                                           eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, beforeEvent, clazz));
-                                           return beforeEvent.getAsync();
+                                           return publishEvent(this,
+                                                               clazz,
+                                                               () -> beforeEvent,
+                                                               eventPublisher::publishEvent);
                                        })
                                );
                            }
                            if (null != afterEvent && isEnabled(clazz, entityEventType, EntityEventPhase.after)) {
                                holder.after(v -> {
                                    return this.doAsyncEvent(() -> {
-                                       eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, afterEvent, clazz));
-                                       return afterEvent.getAsync();
+                                       return publishEvent(this,
+                                                           clazz,
+                                                           () -> afterEvent,
+                                                           eventPublisher::publishEvent);
                                    });
                                });
                            }
@@ -426,8 +430,10 @@ public class EntityEventListener implements EventListener {
                            if (null != prepareEvent && isEnabled(clazz, entityEventType, EntityEventPhase.prepare)) {
                                holder.before(
                                        this.doAsyncEvent(() -> {
-                                           eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, prepareEvent, clazz));
-                                           return prepareEvent.getAsync();
+                                           return publishEvent(this,
+                                                               clazz,
+                                                               () -> prepareEvent,
+                                                               eventPublisher::publishEvent);
                                        })
                                );
                            }
@@ -435,16 +441,20 @@ public class EntityEventListener implements EventListener {
                            if (null != beforeEvent && isEnabled(clazz, entityEventType, EntityEventPhase.before)) {
                                holder.invoke(
                                        this.doAsyncEvent(() -> {
-                                           eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, beforeEvent, clazz));
-                                           return beforeEvent.getAsync();
+                                           return publishEvent(this,
+                                                               clazz,
+                                                               () -> beforeEvent,
+                                                               eventPublisher::publishEvent);
                                        })
                                );
                            }
                            if (null != afterEvent && isEnabled(clazz, entityEventType, EntityEventPhase.after)) {
                                holder.after(v -> {
                                    return this.doAsyncEvent(() -> {
-                                       eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, afterEvent, clazz));
-                                       return afterEvent.getAsync();
+                                       return publishEvent(this,
+                                                           clazz,
+                                                           () -> afterEvent,
+                                                           eventPublisher::publishEvent);
                                    });
                                });
                            }
@@ -458,8 +468,7 @@ public class EntityEventListener implements EventListener {
     }
 
     protected Mono<Void> doAsyncEvent(Supplier<Mono<Void>> eventSupplier) {
-        return EntityEventHelper
-                .isDoFireEvent(true)
+        return isDoFireEvent(true)
                 .filter(Boolean::booleanValue)
                 .flatMap(ignore -> {
                     return eventSupplier.get();