[设计模式]基于事件驱动机制解耦

0、项目示例

  • 基础服务拆分方法请求生命周期触发UMP监控、日志打印、异常获取等
  • 用户信息变更触发生效、失效、变更逻辑变化

1、事件结构

│ XXXAspect.java
│ IAspectHandler.java

├─annotation
│ XXXAspect.java

├─event
│ Event.java
│ EventEnums.java
│ EventListener.java
│ EventPublisher.java

├─handler
│ AbstractHandler.java
│ AAAHandler.java
│ BBBHandler.java
│ CCCHandler.java
│ DDDHandler.java

└─holder
XXXHolder.java

2、事件枚举

/**
 * @author: guanjian
 * @date: 2020/4/10 17:49
 * @description: 事件类型枚举
 */
public enum EventEnums {
    _BEFORE("before"),
    _AFTER("after"),
    _AFTERRETURNING("afterreturning"),
    _AROUND("around"),
    _AFTERTHROWING("afterthrowing"),
    ;
 
    String value;
 
    EventEnums(String value) {
        this.value = value;
    }
 
    public String getValue() {
        return value;
    }
}

3、事件定义

/**
 * @author: guanjian
 * @date: 2020/4/10 17:49
 * @description: 事件传递实体
 */
public class Event<EventEnums> extends ApplicationEvent {
 
    private EventEnums eventName;
 
    public Event(EventEnums eventName) {
        super(eventName);
        this.eventName = eventName;
    }
 
    public EventEnums getEventName() {
        return eventName;
    }
 
    public void setEventName(EventEnums eventName) {
        this.eventName = eventName;
    }
}

4、事件监听

/**
 * @author: guanjian
 * @date: 2020/4/10 17:34
 * @description: 事件监听器
 */
@Component("eventListener")
public class EventListener implements ApplicationContextAware {
 
    public final static Map<String, IAspectHandler> EVENT_HOLDER = Maps.newConcurrentMap();
 
    @Async
    @org.springframework.context.event.EventListener
    public void hanleEvent(Event<EventEnums> event) {
        if (CollectionUtils.isEmpty(EVENT_HOLDER.entrySet())) return;
 
        switch (event.getEventName()) {
            case _BEFORE:
                EVENT_HOLDER.keySet().stream()
                        .forEach(x -> {
                            EVENT_HOLDER.get(x).before();
                        });
                break;
            case _AFTER:
                EVENT_HOLDER.keySet().stream()
                        .forEach(x -> {
                            EVENT_HOLDER.get(x).after();
                        });
                break;
            case _AFTERTHROWING:
                EVENT_HOLDER.keySet().stream()
                        .forEach(x -> {
                            EVENT_HOLDER.get(x).exception();
                        });
                break;
            case _AFTERRETURNING:
                EVENT_HOLDER.keySet().stream()
                        .forEach(x -> {
                            EVENT_HOLDER.get(x).result();
                        });
                break;
            default:
                throw new IllegalStateException(event.getEventName() + " is undefined eventType.");
        }
    }
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        EVENT_HOLDER.putAll(applicationContext.getBeansOfType(IAspectHandler.class));
    }
}

5、事件发布

/**
 * @author: guanjian
 * @date: 2020/4/10 17:37
 * @description: 事件发布器
 */
@Component("eventPublisher")
public class EventPublisher implements ApplicationEventPublisherAware {
 
    private ApplicationEventPublisher publisher;
 
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        publisher = applicationEventPublisher;
    }
 
    public void publishEvent(Event event) {
        publisher.publishEvent(event);
    }
}
posted @ 2021-02-22 14:18  大摩羯先生  阅读(34)  评论(0编辑  收藏  举报