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

com.sghd.common.event.AbstractReceiver Maven / Gradle / Ivy

The newest version!
package com.sghd.common.event;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 抽象事件订阅者,用于简化编码
 */
public abstract class AbstractReceiver implements Receiver {
	
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected EventBus eventBus;

	@PostConstruct
	protected void init() {
		for (String name : getEventNames()) {
			eventBus.register(name, this);
		}
	}

	/**
	 * 获取该订阅者负责处理的事件名数组
	 * @return
	 */
	public abstract String[] getEventNames();
	
	@Override
	public final void onEvent(Event event) {
		T content = event.getBody();
		doEvent(content);
	}
	
	/**
	 * 事件处理方法
	 * @param event 事件消息体
	 */
	public abstract void doEvent(T event);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy