All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.spring.eventbus.BaseEventBus Maven / Gradle / Ivy

package org.onetwo.common.spring.eventbus;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.google.common.eventbus.EventBus;

/**
 * @author wayshall
 * 
*/ abstract public class BaseEventBus implements InitializingBean { @Autowired private ApplicationContext applicationContext; private EventBus eventBus; private Class listennerClass; public BaseEventBus(String name) { this(name, null); } public BaseEventBus(String name, Class listennerType) { this.eventBus = new EventBus(name + "EventBus"); this.listennerClass = listennerType; } @Override public void afterPropertiesSet() throws Exception { if (listennerClass!=null) { Map beanMap = applicationContext.getBeansWithAnnotation(listennerClass); beanMap.forEach((name, bean)->{ register(bean); }); } } final public void register(Object listener){ eventBus.register(listener); } final public void post(Object event){ this.eventBus.post(event); } final protected EventBus getEventBus() { return eventBus; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy