public class CommandGatewayFactory extends Object
CommandGateway
implementations from custom interface definitions. The behavior of the
method is defined by the parameters, declared exceptions and return type of the method.
Supported parameter types:
Message
implementation itself,
a new Message
is created using the payload and MetaData
of the Message
passed as parameter.
MetaDataValue
will cause the parameter values to be added as MetaData
values to the outgoing Message
.
long
and TimeUnit
, they are considered to represent the timeout for the command.
The method will block for as long as the command requires to execute, or until the timeout expires.
void
return types are always allowed.
Unless another parameter makes the method blocking, void
methods are non-blocking by default.
Future
, CompletableFuture
, or CompletionStage
return type will result in a non-blocking operation, given that the configured CommandBus
is asynchronous.
For example, when the provided CommandBus
is a SimpleCommandBus
, the returned Future
is still blocking.
The returned Future
allows you to retrieve the execution's result at your own convenience.
Note that declared exceptions and timeouts are ignored.
TimeoutException
will throw that exception when a configured timeout expires.
If no such exception is declared, but a timeout is configured, the method will return null
.
InterruptedException
will throw that exception when a thread blocked while waiting for a response is interrupted.
Not declaring the exception will have the method return null
when a blocked thread is interrupted.
Note that when no InterruptedException
is declared, the interrupt flag is set back on the interrupted thread.
@Timeout
annotation can be used to define a timeout on a method. This will always cause
a method invocation to block until a response is available, or the timeout expires.
Any method will be blocking if:
In other cases, the method is non-blocking and will return immediately after dispatching a command.
This factory is thread safe once configured, and so are the gateways it creates.Modifier and Type | Class and Description |
---|---|
static class |
CommandGatewayFactory.Builder
Builder class to instantiate a
CommandGatewayFactory . |
static interface |
CommandGatewayFactory.InvocationHandler<R>
Interface towards the mechanism that handles a method call on a gateway interface method.
|
Modifier | Constructor and Description |
---|---|
protected |
CommandGatewayFactory(CommandGatewayFactory.Builder builder)
Instantiate a
CommandGatewayFactory based on the fields contained in the CommandGatewayFactory.Builder . |
Modifier and Type | Method and Description |
---|---|
static CommandGatewayFactory.Builder |
builder()
Instantiate a Builder to be able to create a
CommandGatewayFactory . |
<T> T |
createGateway(Class<T> gatewayInterface)
Creates a gateway instance for the given
gatewayInterface . |
<C,R> CommandGatewayFactory |
registerCommandCallback(CommandCallback<C,R> callback,
ResponseType<R> responseType)
Registers the
callback , which is invoked for each sent command, unless Axon is able to detect that the
result of the command does not match the type accepted by the callback. |
CommandGatewayFactory |
registerDispatchInterceptor(MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor)
Registers the given
dispatchInterceptor which is invoked for each Command dispatched through the Command
Gateways created by this factory. |
protected <R> CommandGatewayFactory.InvocationHandler<R> |
wrapToFireAndForget(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate)
Wrap the given
delegate in an CommandGatewayFactory.InvocationHandler that returns immediately after invoking the
delegate . |
protected <R> CommandGatewayFactory.InvocationHandler<R> |
wrapToReturnWithFixedTimeout(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate,
long timeout,
TimeUnit timeUnit)
Wraps the given
delegate and waits for the result in the CompletableFuture to become available,
with given timeout and timeUnit . |
protected <R> CommandGatewayFactory.InvocationHandler<R> |
wrapToReturnWithTimeoutInArguments(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate,
int timeoutIndex,
int timeUnitIndex)
Wraps the given
delegate and waits for the result in the CompletableFuture to become available
using given indices to resolve the parameters that provide the timeout to use. |
protected <R> CommandGatewayFactory.InvocationHandler<R> |
wrapToWaitForResult(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate)
Wraps the given
delegate and waits for the result in the CompletableFuture to become available. |
protected <R> CommandGatewayFactory.InvocationHandler<R> |
wrapUndeclaredExceptions(CommandGatewayFactory.InvocationHandler<R> delegate,
Class<?>[] declaredExceptions)
Wraps the given
delegate in an CommandGatewayFactory.InvocationHandler that wraps exceptions not declared on the method
in a CommandExecutionException . |
protected CommandGatewayFactory(CommandGatewayFactory.Builder builder)
CommandGatewayFactory
based on the fields contained in the CommandGatewayFactory.Builder
.
Will assert that the CommandBus
is not null
, and will throw an AxonConfigurationException
if it is null
.
builder
- The CommandGatewayFactory.Builder
used to instantiate a CommandGatewayFactory
instance.public static CommandGatewayFactory.Builder builder()
CommandGatewayFactory
.
The CommandBus
is a hard requirements and as such should be provided.
CommandGatewayFactory
.public <T> T createGateway(Class<T> gatewayInterface)
gatewayInterface
. The returned instance is a Proxy that
implements that interface.T
- The interface declaring the gateway methods.gatewayInterface
- The interface declaring the gateway methods.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapUndeclaredExceptions(CommandGatewayFactory.InvocationHandler<R> delegate, Class<?>[] declaredExceptions)
delegate
in an CommandGatewayFactory.InvocationHandler
that wraps exceptions not declared on the method
in a CommandExecutionException
.R
- The response type of the command handler.delegate
- The delegate to invoke that potentially throws exceptions.declaredExceptions
- The exceptions declared on the method signature.CommandGatewayFactory.InvocationHandler
that wraps undeclared exceptions in a
CommandExecutionException
.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToFireAndForget(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate)
delegate
in an CommandGatewayFactory.InvocationHandler
that returns immediately after invoking the
delegate
.R
- The response type of the command handler.delegate
- The delegate to invoke, potentially throwing an InterruptedException when invoked.CommandGatewayFactory.InvocationHandler
that wraps returns immediately after invoking the delegate.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToWaitForResult(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate)
delegate
and waits for the result in the CompletableFuture
to become available.
No explicit timeout is provided for the waiting.R
- The result of the command handler.delegate
- The delegate to invoke, returning a CompletableFuture
CompletableFuture
, either a return value or an exception.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithFixedTimeout(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, long timeout, TimeUnit timeUnit)
delegate
and waits for the result in the CompletableFuture
to become available,
with given timeout
and timeUnit
.R
- The result of the command handler.delegate
- The delegate to invoke, returning a CompletableFuture
.timeout
- The amount of time to wait for the result to become available.timeUnit
- The unit of time to wait.CompletableFuture
, either a return value or an exception.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithTimeoutInArguments(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, int timeoutIndex, int timeUnitIndex)
delegate
and waits for the result in the CompletableFuture
to become available
using given indices to resolve the parameters that provide the timeout to use.R
- The result of the command handler.delegate
- The delegate to invoke, returning a CompletableFuture
.timeoutIndex
- The index of the argument providing the timeout.timeUnitIndex
- The index of the argument providing the time unit.CompletableFuture
, either a return value or an exception.public <C,R> CommandGatewayFactory registerCommandCallback(CommandCallback<C,R> callback, ResponseType<R> responseType)
callback
, which is invoked for each sent command, unless Axon is able to detect that the
result of the command does not match the type accepted by the callback.
Axon will check the signature of the CommandCallback.onResult(CommandMessage, CommandResultMessage)
method and only invoke the callback if the actual result of the command is an instance of that type. If Axon is
unable to detect the type, the callback is always invoked, potentially causing
ClassCastException
.R
- The type of return value the callback is interested in.callback
- The callback to register.responseType
- The actual response type of the command.public CommandGatewayFactory registerDispatchInterceptor(MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor)
dispatchInterceptor
which is invoked for each Command dispatched through the Command
Gateways created by this factory.dispatchInterceptor
- The interceptor to register.Copyright © 2010–2024. All rights reserved.