Search in sources :

Example 1 with RejectionClass

use of io.spine.core.RejectionClass 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 RejectionClass

use of io.spine.core.RejectionClass in project core-java by SpineEventEngine.

the class ExternalMessageClass method fromRejectionClasses.

/**
 * Transforms a given set of {@linkplain RejectionClass rejection classes} into a set
 * of {@code ExternalMessageClass}es by wrapping each rejection class
 * into an external message class.
 *
 * @param classes the set of rejection classes to transform
 * @return a set of {@code ExternalMessageClass}es, each wrapping an item from the original set
 */
public static Set<ExternalMessageClass> fromRejectionClasses(Set<RejectionClass> classes) {
    checkNotNull(classes);
    final ImmutableSet.Builder<ExternalMessageClass> builder = ImmutableSet.builder();
    for (RejectionClass rejectionClass : classes) {
        builder.add(of(rejectionClass));
    }
    return builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) RejectionClass(io.spine.core.RejectionClass)

Example 3 with RejectionClass

use of io.spine.core.RejectionClass in project core-java by SpineEventEngine.

the class RejectionBusShould method register_dispatchers.

@Test
public void register_dispatchers() {
    final RejectionDispatcher<?> dispatcher = new BareDispatcher();
    rejectionBus.register(dispatcher);
    final RejectionClass rejectionClass = RejectionClass.of(InvalidProjectName.class);
    assertTrue(rejectionBus.getDispatchers(rejectionClass).contains(dispatcher));
}
Also used : RejectionClass(io.spine.core.RejectionClass) BareDispatcher(io.spine.server.rejection.given.BareDispatcher) Test(org.junit.Test)

Example 4 with RejectionClass

use of io.spine.core.RejectionClass in project core-java by SpineEventEngine.

the class DelegatingRejectionDispatcher method getExternalDispatcher.

/**
 * Wraps this dispatcher to an external rejection 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<RejectionClass> rejectionClasses = delegate.getExternalRejectionClasses();
            return ExternalMessageClass.fromRejectionClasses(rejectionClasses);
        }

        @Override
        public Set<I> dispatch(ExternalMessageEnvelope envelope) {
            final ExternalMessage externalMessage = envelope.getOuterObject();
            final Rejection rejection = unpack(externalMessage.getOriginalMessage());
            final Set<I> ids = delegate.dispatchRejection(RejectionEnvelope.of(rejection));
            return ids;
        }

        @Override
        public void onError(ExternalMessageEnvelope envelope, RuntimeException exception) {
            final MessageClass messageClass = envelope.getMessageClass();
            final String messageId = Stringifiers.toString(envelope.getId());
            final String errorMessage = format("Error dispatching external rejection (class: %s, id: %s)", messageClass, messageId);
            log().error(errorMessage, exception);
        }
    };
}
Also used : Rejection(io.spine.core.Rejection) ExternalMessageClass(io.spine.server.integration.ExternalMessageClass) ExternalMessageClass(io.spine.server.integration.ExternalMessageClass) MessageClass(io.spine.type.MessageClass) ExternalMessageEnvelope(io.spine.server.integration.ExternalMessageEnvelope) ExternalMessageDispatcher(io.spine.server.integration.ExternalMessageDispatcher) RejectionClass(io.spine.core.RejectionClass) ExternalMessage(io.spine.server.integration.ExternalMessage)

Example 5 with RejectionClass

use of io.spine.core.RejectionClass in project core-java by SpineEventEngine.

the class UnhandledRejectionException method msgFormat.

private static String msgFormat(Message msg) {
    final RejectionClass cls = RejectionClass.of(msg);
    final String typeName = TypeName.of(msg).value();
    final String result = format("There is no registered handler for the rejection class: `%s`. Protobuf type: `%s`", cls, typeName);
    return result;
}
Also used : RejectionClass(io.spine.core.RejectionClass) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString)

Aggregations

RejectionClass (io.spine.core.RejectionClass)9 Test (org.junit.Test)5 BareDispatcher (io.spine.server.rejection.given.BareDispatcher)3 InvalidProjectNameSubscriber (io.spine.server.rejection.given.InvalidProjectNameSubscriber)3 ExternalMessageClass (io.spine.server.integration.ExternalMessageClass)2 MultipleRejectionSubscriber (io.spine.server.rejection.given.MultipleRejectionSubscriber)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SPI (io.spine.annotation.SPI)1 CommandClass (io.spine.core.CommandClass)1 EventClass (io.spine.core.EventClass)1 Rejection (io.spine.core.Rejection)1 BoundedContext (io.spine.server.BoundedContext)1 ExternalMessage (io.spine.server.integration.ExternalMessage)1 ExternalMessageDispatcher (io.spine.server.integration.ExternalMessageDispatcher)1 ExternalMessageEnvelope (io.spine.server.integration.ExternalMessageEnvelope)1 MessageClass (io.spine.type.MessageClass)1