io.naraplatform.daysman.daysboy.event.EventSubscriber Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of daysman-daysboy Show documentation
Show all versions of daysman-daysboy Show documentation
A application used as an example on how to set up pushing its components to the Central Repository.
The newest version!
package io.naraplatform.daysman.daysboy.event;
import io.naradrama.prologue.domain.cqrs.broker.StreamEventMessage;
import io.naraplatform.daysman.mediator.spec.EventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class EventSubscriber {
//
private List handlers = new ArrayList<>();
private boolean scanned;
private synchronized void register(ApplicationContext context) {
//
if (scanned) {
return;
}
for (String name : context.getBeanDefinitionNames()) {
Object bean = context.getBean(name);
register(bean);
}
scanned = true;
}
private void register(Object bean) {
//
Class> clazz = ClassUtils.getUserClass(bean.getClass());
if (!clazz.getName().endsWith("Handler") && !clazz.getName().endsWith("Handlers")) {
return;
}
for (Method method : clazz.getDeclaredMethods()) {
if (isDaysmanEventHandler(method)) {
log.info("add Method --> class:{} methods:{}", clazz.getName(), method.getName());
handlers.add(new MethodInvokeEventHandler(bean, method));
}
}
}
private boolean isDaysmanEventHandler(Method method) {
//
for (Annotation annotation : method.getDeclaredAnnotations()) {
if (annotation.annotationType() == DaysmanEventHandler.class) {
return true;
}
}
return false;
}
public void convey(StreamEventMessage streamEvent) {
//
if (!scanned) {
register(ApplicationContextUtils.getApplicationContext());
}
handlers.forEach(handler -> handler.handle(streamEvent));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy