public final class CommandRequest extends ClientRequest implements io.spine.logging.Logging
Usage example:
Subscription loginSubscription =
client.asGuest()
.command(logInUser)
.observe(UserLoggedIn.class, (event, context) -> { ... })
.observe(UserAlreadyLoggedIn.class, (rejection, context) -> { ... })
.onStreamingError((throwable) -> { ... })
.post();
The subscription obtained from the CommandRequest.post()
should be cancelled
to preserve both client-side and server-side resources. The moment of cancelling
the subscriptions depends on the nature of the posted command and the outcome
expected by the client application.
Modifier and Type | Method and Description |
---|---|
<E extends io.spine.base.EventMessage> |
observe(java.lang.Class<E> type,
java.util.function.Consumer<E> consumer)
Adds the passed consumer to the subscribers of the event of the passed type.
|
<E extends io.spine.base.EventMessage> |
observe(java.lang.Class<E> type,
EventConsumer<E> consumer)
Adds the passed event consumer to the subscribers of the event of the passed type.
|
CommandRequest |
onConsumingError(ConsumerErrorHandler<io.spine.base.EventMessage> handler)
Assigns a handler for errors occurred in consumers of events.
|
CommandRequest |
onServerError(ServerErrorHandler handler)
Assigns a handler for an error occurred on the server-side (such as validation error)
in response to posting a command.
|
CommandRequest |
onStreamingError(ErrorHandler handler)
Assigns a handler for errors occurred when delivering events.
|
com.google.common.collect.ImmutableSet<io.spine.client.Subscription> |
post()
Subscribes the consumers to events to receive events resulting from the command as
they happen, then sends the command to the server.
|
void |
postAndForget()
Posts the command without subscribing to events that may be generated during
the command handling.
|
client, command, select, subscribeTo, subscribeToEvent, user
@CanIgnoreReturnValue public <E extends io.spine.base.EventMessage> CommandRequest observe(java.lang.Class<E> type, java.util.function.Consumer<E> consumer)
E
- the type of the eventtype
- the type of the event message to be received by the consumerconsumer
- the consumer@CanIgnoreReturnValue public <E extends io.spine.base.EventMessage> CommandRequest observe(java.lang.Class<E> type, EventConsumer<E> consumer)
E
- the type of the eventtype
- the type of the event message to be received by the consumerconsumer
- the consumer of the event message and its context@CanIgnoreReturnValue public CommandRequest onStreamingError(ErrorHandler handler)
If such an error occurs, no more events resulting from the posted command will be delivered to the consumers.
onStreamingError
in class ClientRequest
@CanIgnoreReturnValue public CommandRequest onConsumingError(ConsumerErrorHandler<io.spine.base.EventMessage> handler)
After the passed handler is called, remaining event consumers will get the messages as usually. If not specified, the default implementation simply logs the error.
@OverridingMethodsMustInvokeSuper @CanIgnoreReturnValue public CommandRequest onServerError(ServerErrorHandler handler)
onServerError
in class ClientRequest
public com.google.common.collect.ImmutableSet<io.spine.client.Subscription> post()
The returned Subscription
instances should be
canceled after the requesting code receives
expected events, or after a reasonable timeout.
The method returns subscriptions to events that the handling of the command may produce. A command may not be accepted for processing by the server, e.g. because of a validation error. In such a case, the method would report the error to the configured error handler, and return an empty set.
CommandRequest.postAndForget()
public void postAndForget() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if CommandRequest.observe(Class, EventConsumer)
or CommandRequest.observe(Class, Consumer)
were
called in the command request configuration chain before calling this methodCommandRequest.post()