Search in sources :

Example 96 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class Aggregate method applyMessages.

/**
     * Applies the passed event message or {@code Event} to the aggregate.
     *
     * @param eventMessages the event messages or events to apply
     * @param envelope      the envelope of the command which generated the events
     * @see #ensureEventMessage(Message)
     */
private void applyMessages(Iterable<? extends Message> eventMessages, CommandEnvelope envelope) {
    final List<? extends Message> messages = newArrayList(eventMessages);
    final EventFactory eventFactory = createEventFactory(envelope, messages.size());
    final List<Event> events = newArrayListWithCapacity(messages.size());
    Version projectedEventVersion = getVersion();
    for (Message eventOrMessage : messages) {
        // Applying each message would increment the entity version.
        // Therefore we should simulate this behaviour.
        projectedEventVersion = Versions.increment(projectedEventVersion);
        final Message eventMessage = ensureEventMessage(eventOrMessage);
        final Event event;
        if (eventOrMessage instanceof Event) {
            event = importEvent((Event) eventOrMessage, envelope.getCommandContext(), projectedEventVersion);
        } else {
            event = eventFactory.createEvent(eventMessage, projectedEventVersion);
        }
        events.add(event);
    }
    play(events);
    uncommittedEvents.addAll(events);
}
Also used : Events.getMessage(io.spine.base.Events.getMessage) EventApplierMethod.forEventMessage(io.spine.server.reflect.EventApplierMethod.forEventMessage) Message(com.google.protobuf.Message) Version(io.spine.base.Version) EventFactory(io.spine.server.command.EventFactory) Event(io.spine.base.Event)

Example 97 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class InvalidCommandException method onMissingTenantId.

/**
     * Creates an exception for a command with missing {@code tenant_id} attribute in
     * the {@code CommandContext}, which is required in a multitenant application.
     */
public static InvalidCommandException onMissingTenantId(Command command) {
    final CommandEnvelope envelope = CommandEnvelope.of(command);
    final Message commandMessage = envelope.getMessage();
    final String errMsg = format("The command (class: `%s`, type: `%s`, id: `%s`) is posted to " + "multitenant Command Bus, but has no `tenant_id` attribute in the context.", CommandClass.of(commandMessage).value().getName(), TypeName.of(commandMessage), idToString(envelope.getCommandId()));
    final Error error = unknownTenantError(commandMessage, errMsg);
    return new InvalidCommandException(errMsg, command, error);
}
Also used : Message(com.google.protobuf.Message) CommandEnvelope(io.spine.envelope.CommandEnvelope) CommandValidationError(io.spine.base.CommandValidationError) Error(io.spine.base.Error) Identifiers.idToString(io.spine.base.Identifiers.idToString)

Example 98 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class Rescheduler method onScheduledCommandExpired.

/**
     * Sets the status of the expired command to error.
     *
     * <p>We cannot post such a command because there is no handler or dispatcher registered yet.
     * Or, posting such a command may be undesirable from the business logic point of view.
     *
     * @param command the expired command
     * @see CommandExpiredException
     */
private void onScheduledCommandExpired(Command command) {
    final CommandEnvelope commandEnvelope = CommandEnvelope.of(command);
    final Message msg = commandEnvelope.getMessage();
    final CommandId id = commandEnvelope.getCommandId();
    final Error error = CommandExpiredException.commandExpiredError(msg);
    commandStore().setToError(commandEnvelope, error);
    log().errorExpiredCommand(msg, id);
}
Also used : Message(com.google.protobuf.Message) CommandEnvelope(io.spine.envelope.CommandEnvelope) Error(io.spine.base.Error) CommandId(io.spine.base.CommandId)

Example 99 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class BoundedContext method notify.

@SuppressWarnings("MethodDoesntCallSuperMethod")
/* We ignore method from super because the default implementation sets
           unimplemented status. */
@Override
public void notify(IntegrationEvent integrationEvent, StreamObserver<Response> responseObserver) {
    final Message eventMsg = unpack(integrationEvent.getMessage());
    final boolean isValid = eventBus.validate(eventMsg, responseObserver);
    if (isValid) {
        final Event event = EventFactory.toEvent(integrationEvent);
        eventBus.post(event);
    }
}
Also used : Message(com.google.protobuf.Message) IntegrationEvent(io.spine.server.integration.IntegrationEvent) Event(io.spine.base.Event)

Example 100 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class Identifiers method convert.

private static String convert(Message message) {
    final Collection<Object> values = message.getAllFields().values();
    final String result;
    if (values.isEmpty()) {
        result = EMPTY_ID;
    } else if (values.size() == 1) {
        final Object object = values.iterator().next();
        if (object instanceof Message) {
            result = idMessageToString((Message) object);
        } else {
            result = object.toString();
        }
    } else {
        result = messageWithMultipleFieldsToString(message);
    }
    return result;
}
Also used : Message(com.google.protobuf.Message) TextFormat.shortDebugString(com.google.protobuf.TextFormat.shortDebugString)

Aggregations

Message (com.google.protobuf.Message)145 Test (org.junit.Test)47 Any (com.google.protobuf.Any)25 Event (io.spine.base.Event)11 ByteString (com.google.protobuf.ByteString)9 Command (io.spine.base.Command)9 EntityRecord (io.spine.server.entity.EntityRecord)8 EntityFilters (io.spine.client.EntityFilters)7 Target (io.spine.client.Target)6 TypeUrl (io.spine.type.TypeUrl)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CommandId (io.spine.base.CommandId)5 ByteArray (voldemort.utils.ByteArray)5 FieldMask (com.google.protobuf.FieldMask)4 GeneratedMessage (com.google.protobuf.GeneratedMessage)4 Timestamp (com.google.protobuf.Timestamp)4 EventContext (io.spine.base.EventContext)4 EntityId (io.spine.client.EntityId)4 EnclosedMessageFieldValueWithCustomInvalidMessage (io.spine.test.validate.msg.EnclosedMessageFieldValueWithCustomInvalidMessage)4