Replies: 1 comment
-
I don't know if this is a better way... @Singleton
public class EventPublisher {
private final Map<Class<?>, ApplicationEventPublisher<?>> publishers;
@SuppressWarnings("unchecked")
public EventPublisher(ApplicationEventPublisher<ACreatingEvent> creatingEventPublisher,
ApplicationEventPublisher<ACreatedEvent> createdEventPublisher,
ApplicationEventPublisher<ADeletedEvent> deletedEventPublisher) {
publishers = CollectionUtils.mapOf(
ACreatingEvent.class, creatingEventPublisher,
ACreatedEvent.class, createdEventPublisher,
ADeletedEvent.class, deletedEventPublisher
);
}
public <T> void publishEvent(T event) {
@SuppressWarnings("unchecked")
final ApplicationEventPublisher<T> publisher = (ApplicationEventPublisher<T>) publishers.get(event.getClass());
publisher.publishEvent(event);
}
} You can manage them together, then just need |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have some types of events to publish, and they have same superclass, as say:
now I'm going to publish them in my
AService
, since they are all AEvent, so I use aApplicationEventPublisher<AEvent>
but these events will not be send to my
ApplicationEventListener
s, because they are notApplicationEventListener<AEvent>
butApplicationEventListener<ACreatingEvent>
,ApplicationEventListener<ACreatedEvent>
,ApplicationEventListener<ADeletedEvent>
:so I have to:
@Inject
3 publishers that matches the Event type one by one.both ways are bad...
am I miss something, or is there a better way?
Beta Was this translation helpful? Give feedback.
All reactions