栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

手写事件发布订阅框架

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写事件发布订阅框架

这里写自定义目录标题
  • 假设新需求
    • 代码
      • 管理监听器
      • 注解区分是否需要异步
      • 异步加载监听器
      • 发布事件通知监听器
      • 父监听器
      • 父listener,所有listener都要实现ParentListener
    • 代码实现例子
      • 创建一个NoticeEvent 的listener监听器
      • 总结

假设新需求

假如现在有一个新需求,当用户触发某一个动作的时候需要通知给不同业务模块去做处理,这个过程不能是同步的,你需要如何去做?

看到这个需求你是不是想到发布订阅观察者模式,而spring就是用了这种模式实现事件监听的,下面是我手写的代码,我们可以一起探讨一下,(#.#)

代码 管理监听器

首先我们需要有一个类来帮助我们管理我们的指定的监听器。再容器初始化的时候就要加载。

package com.judy.learn.core;

import cn.hutool.core.collection.CollectionUtil;
import com.judy.learn.event.ParentEvent;
import com.judy.learn.listener.ParentListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;


@Component
@Slf4j
public class EventManager implements ApplicationContextAware {


    //定义存放监听器容器
    private HashMap, List> coreMap = new HashMap<>();

    private static ApplicationContext applicationContext;

    private static final String EVENT = "event";

    

    @PostConstruct
    public void initListenerApplication() {
        Map beansOfType = applicationContext.getBeansOfType(ParentListener.class);
        if (Objects.isNull(beansOfType)) {
            log.info("获取监听器类 is null");
            return;
        }
        beansOfType.forEach((key, value) -> {
            Method[] declaredMethods = ReflectionUtils.getDeclaredMethods(value.getClass());
            for (Method declaredMethod : declaredMethods) {
                if (declaredMethod.getName().equals(EVENT)) {
                    Parameter parameter = declaredMethod.getParameters()[0];
                    //参数必须是event的子类
                    if (parameter.getName().equals(ParentEvent.class.getName())) {
                        continue;
                    }
                    //注册监听器
                    this.registerListener((Class) parameter.getType(), value);
                }
            }
        });
    }

    
    public void registerListener(Class event, ParentListener eventListener) {
        List parentListeners = coreMap.get(event);
        if (CollectionUtils.isEmpty(parentListeners)) {
            parentListeners = new ArrayList<>();
        }
        parentListeners.add(eventListener);
        coreMap.put(event, parentListeners);
    }


    
    public void removeListener(Class event) {
        coreMap.remove(event);
    }

    
    public void notifyListener(ParentEvent parentEvent) {
        if (Objects.nonNull(coreMap)) {
            List parentListeners = coreMap.get(parentEvent.getClass());
            if (CollectionUtil.isNotEmpty(parentListeners)) {
                parentListeners.forEach(parentListener -> parentListener.event(parentEvent));
            }
        }
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        EventManager.applicationContext = applicationContext;
    }
}

注解区分是否需要异步

当然我们也可以使用异步去加载我们的监听器,这样会提高我们的效率,也就是使用线程池来加载我们的监听器,我们可以通过注解的方式来区分是否需要使用异步加载


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AsyncExecuteListener {
}

异步加载监听器
package com.judy.learn.core;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.judy.learn.event.ParentEvent;
import com.judy.learn.listener.ParentListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Component
@Slf4j
public class AsyncEventManager implements ApplicationContextAware {
    public Map, List> asyncMap = new HashMap<>();

    private static ApplicationContext applicationContext;

    private static final String EVENT = "event";

    //线程池去通知监听器
    private static ExecutorService eventPool = new ThreadPoolExecutor(4,
            8, 30L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(512), new ThreadFactoryBuilder().setNamePrefix("event-pool-%d").build());


    

    @PostConstruct
    public void initListenerApplication() {
        Map beansOfType = applicationContext.getBeansOfType(ParentListener.class);
        if (Objects.isNull(beansOfType)) {
            log.info("获取监听器类 is null");
            return;
        }
        beansOfType.forEach((key, value) -> {
            Method[] declaredMethods = ReflectionUtils.getDeclaredMethods(value.getClass());
            for (Method declaredMethod : declaredMethods) {
                if (declaredMethod.getName().equals(EVENT)) {
                    Parameter parameter = declaredMethod.getParameters()[0];
                    //参数必须是event的子类
                    if (parameter.getName().equals(ParentEvent.class.getName())) {
                        continue;
                    }
                    //注册监听器
                    this.registerListener((Class) parameter.getType(), value);
                }
            }
        });
    }

    
    private void registerListener(Class type, ParentListener value) {
        List parentListeners = asyncMap.get(type);
        if (CollectionUtils.isEmpty(parentListeners)) {
            parentListeners = new ArrayList<>();
        }
        parentListeners.add(value);
        asyncMap.put(type, parentListeners);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AsyncEventManager.applicationContext = applicationContext;
    }

    
    public void notifyListener(ParentEvent type) {
        List parentListeners = asyncMap.get(type.getClass());
        if (CollectionUtils.isEmpty(parentListeners)) {
            return;
        }
        parentListeners.forEach(parentListener -> {
                    AsyncExecuteListener annotation = parentListener.getClass().getAnnotation(AsyncExecuteListener.class);
                    if (Objects.isNull(annotation)) {
                        //同步执行
                        parentListener.event(type);
                    } else {
                        //多线程异步执行
                        eventPool.execute(() -> parentListener.event(type));
                    }
                }
        );
    }
}

发布事件通知监听器

@Component
public class ParentPublisher {

    @Autowired
    private EventManager eventManager;
    public void publisher(E event) {
        eventManager.notifyListener(event);
    }
}
父监听器

所有的监听器都要集成ParentEvent


public class ParentEvent extends EventObject {
    
    public ParentEvent(Object source) {
        super(source);
    }
}
父listener,所有listener都要实现ParentListener

public interface ParentListener {
    
    void event(E e);
}

代码实现例子

创建一个通知学生姓名和写别的事件

public class NoticeEvent extends ParentEvent {

    @Getter
    private Student student;

    
    public NoticeEvent(Object source, Student student) {
        super(source);
        this.student = student;
    }

}

创建一个NoticeEvent 的listener监听器
@Component
@Slf4j
public class NoticeListener implements ParentListener {
    @Override
    public void event(NoticeEvent noticeEvent) {
        log.info("通知学生:{},性别:{}", noticeEvent.getStudent().getName(), noticeEvent.getStudent().getSex());
    }
}

@RequestMapping("/student")
@RestController
public class StudentController {
    @Autowired
    private ParentPublisher noticeEventParentPublisher;


    @GetMapping
    public void studentNotice(String sex,String name) {
        Student student = Student.builder().sex(sex).name(name).build();
        NoticeEvent noticeEvent = new NoticeEvent(this, student);
        noticeEventParentPublisher.publisher(noticeEvent);
    }
}

总结

实现思路不止一种,思维方式不止一种!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969621.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号