Search in sources :

Example 1 with EventClass

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());
}
Also used : ExternalMessageClass(io.spine.server.integration.ExternalMessageClass) SPI(io.spine.annotation.SPI) EventClass(io.spine.core.EventClass) BoundedContext(io.spine.server.BoundedContext) CommandClass(io.spine.core.CommandClass) RejectionClass(io.spine.core.RejectionClass)

Example 2 with EventClass

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);
        }
    };
}
Also used : EventEnvelope(io.spine.core.EventEnvelope) ExternalMessageClass(io.spine.server.integration.ExternalMessageClass) EventClass(io.spine.core.EventClass) ExternalMessageEnvelope(io.spine.server.integration.ExternalMessageEnvelope) ExternalMessageDispatcher(io.spine.server.integration.ExternalMessageDispatcher)

Example 3 with EventClass

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));
}
Also used : EventClass(io.spine.core.EventClass) Test(org.junit.Test)

Example 4 with EventClass

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));
}
Also used : EventClass(io.spine.core.EventClass) Test(org.junit.Test)

Example 5 with EventClass

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();
}
Also used : EventClass(io.spine.core.EventClass) Test(org.junit.Test)

Aggregations

EventClass (io.spine.core.EventClass)9 Test (org.junit.Test)4 ImmutableSet (com.google.common.collect.ImmutableSet)2 ExternalMessageClass (io.spine.server.integration.ExternalMessageClass)2 SPI (io.spine.annotation.SPI)1 CommandClass (io.spine.core.CommandClass)1 EventEnvelope (io.spine.core.EventEnvelope)1 RejectionClass (io.spine.core.RejectionClass)1 BoundedContext (io.spine.server.BoundedContext)1 EventFilter (io.spine.server.event.EventFilter)1 ExternalMessageDispatcher (io.spine.server.integration.ExternalMessageDispatcher)1 ExternalMessageEnvelope (io.spine.server.integration.ExternalMessageEnvelope)1