MENU

事件中心

May 15, 2022 • 学习笔记

事件Event

public interface IListener extends java.util.EventListener

使用AtomicLong做EventIDCounter

private static final AtomicLong eventIdCounter = new AtomicLong(1);

使用线程池做事件发送

for (final IListener listener : topicListenersMap.get(topic)) {
    if (event.isAsyncPublish()) {
        getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                notifyEvent(listener, event);
            }
        });
    } else {
        notifyEvent(listener, event);
    }
}

//线程池
threadPool = new ThreadPoolExecutor(5, 5, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
                new NamedThreadFactory("Phenix-EventCenter", true));

topic存储

protected final Map<EventTopic, BlockingQueue<IListener>> topicListenersMap = new ConcurrentHashMap<EventTopic, BlockingQueue<IListener>>();