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);
}
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);
}
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);
}
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);
}
}
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;
}
Aggregations