use of io.spine.core.EventClass in project core-java by SpineEventEngine.
the class AggregateRepository method onRegistered.
/**
* {@inheritDoc}
*
* <p>{@linkplain io.spine.server.commandbus.CommandBus#register(
* io.spine.server.bus.MessageDispatcher) Registers} itself with the {@code CommandBus} of the
* parent {@code BoundedContext}.
*/
@Override
public void onRegistered() {
super.onRegistered();
final BoundedContext boundedContext = getBoundedContext();
final Set<CommandClass> commandClasses = getMessageClasses();
final DelegatingEventDispatcher<I> eventDispatcher;
eventDispatcher = DelegatingEventDispatcher.of(this);
final Set<EventClass> eventClasses = eventDispatcher.getMessageClasses();
final ExternalMessageDispatcher<I> extEventDispatcher;
extEventDispatcher = eventDispatcher.getExternalDispatcher();
final Set<ExternalMessageClass> extEventClasses = extEventDispatcher.getMessageClasses();
final DelegatingRejectionDispatcher<I> rejectionDispatcher;
rejectionDispatcher = DelegatingRejectionDispatcher.of(this);
final Set<RejectionClass> rejectionClasses = rejectionDispatcher.getMessageClasses();
final ExternalMessageDispatcher<I> extRejectionDispatcher;
extRejectionDispatcher = rejectionDispatcher.getExternalDispatcher();
final Set<ExternalMessageClass> extRejectionClasses = extRejectionDispatcher.getMessageClasses();
if (commandClasses.isEmpty() && eventClasses.isEmpty() && rejectionClasses.isEmpty() && extEventClasses.isEmpty() && extRejectionClasses.isEmpty()) {
throw newIllegalStateException("Aggregates of the repository %s neither handle commands" + " nor react on events or rejections.", this);
}
registerInCommandBus(boundedContext, commandClasses);
registerInEventBus(boundedContext, eventDispatcher, eventClasses);
registerInRejectionBus(boundedContext, rejectionDispatcher, rejectionClasses);
registerExtMessageDispatcher(boundedContext, extEventDispatcher, extEventClasses);
registerExtMessageDispatcher(boundedContext, extRejectionDispatcher, extRejectionClasses);
this.commandErrorHandler = CommandErrorHandler.with(boundedContext.getRejectionBus());
}
use of io.spine.core.EventClass in project core-java by SpineEventEngine.
the class DelegatingEventDispatcher method getExternalDispatcher.
/**
* Wraps this dispatcher to an external event dispatcher.
*
* @return the external rejection dispatcher proxying calls to the underlying instance
*/
public ExternalMessageDispatcher<I> getExternalDispatcher() {
return new ExternalMessageDispatcher<I>() {
@Override
public Set<ExternalMessageClass> getMessageClasses() {
final Set<EventClass> eventClasses = delegate.getExternalEventClasses();
return ExternalMessageClass.fromEventClasses(eventClasses);
}
@Override
public Set<I> dispatch(ExternalMessageEnvelope envelope) {
final EventEnvelope eventEnvelope = asEventEnvelope(envelope);
final Set<I> ids = delegate.dispatchEvent(eventEnvelope);
return ids;
}
@Override
public void onError(ExternalMessageEnvelope envelope, RuntimeException exception) {
final EventEnvelope eventEnvelope = asEventEnvelope(envelope);
delegate.onError(eventEnvelope, exception);
}
};
}
use of io.spine.core.EventClass in project core-java by SpineEventEngine.
the class EventBusShould method unregister_dispatchers.
@Test
public void unregister_dispatchers() {
final EventDispatcher dispatcherOne = new BareDispatcher();
final EventDispatcher dispatcherTwo = new BareDispatcher();
final EventClass eventClass = EventClass.of(ProjectCreated.class);
eventBus.register(dispatcherOne);
eventBus.register(dispatcherTwo);
eventBus.unregister(dispatcherOne);
final Set<? extends EventDispatcher<?>> dispatchers = eventBus.getDispatchers(eventClass);
// Check we don't have 1st dispatcher, but have 2nd.
assertFalse(dispatchers.contains(dispatcherOne));
assertTrue(dispatchers.contains(dispatcherTwo));
eventBus.unregister(dispatcherTwo);
assertFalse(eventBus.getDispatchers(eventClass).contains(dispatcherTwo));
}
use of io.spine.core.EventClass in project core-java by SpineEventEngine.
the class EventBusShould method unregister_subscribers.
@Test
public void unregister_subscribers() {
final EventSubscriber subscriberOne = new ProjectCreatedSubscriber();
final EventSubscriber subscriberTwo = new ProjectCreatedSubscriber();
eventBus.register(subscriberOne);
eventBus.register(subscriberTwo);
final EventClass eventClass = EventClass.of(ProjectCreated.class);
eventBus.unregister(subscriberOne);
// Check that the 2nd subscriber with the same event subscriber method remains
// after the 1st subscriber unregisters.
final Collection<? extends EventDispatcher<?>> subscribers = eventBus.getDispatchers(eventClass);
assertFalse(subscribers.contains(subscriberOne));
assertTrue(subscribers.contains(subscriberTwo));
// Check that after 2nd subscriber us unregisters he's no longer in
eventBus.unregister(subscriberTwo);
assertFalse(eventBus.getDispatchers(eventClass).contains(subscriberTwo));
}
use of io.spine.core.EventClass in project core-java by SpineEventEngine.
the class EventBusShould method unregister_registries_on_close.
@Test
public void unregister_registries_on_close() throws Exception {
final EventStore eventStore = spy(mock(EventStore.class));
final EventBus eventBus = EventBus.newBuilder().setEventStore(eventStore).build();
eventBus.register(new BareDispatcher());
eventBus.register(new ProjectCreatedSubscriber());
final EventClass eventClass = EventClass.of(ProjectCreated.class);
eventBus.close();
assertTrue(eventBus.getDispatchers(eventClass).isEmpty());
verify(eventStore).close();
}
Aggregations