Class CommandGatewayFactory
java.lang.Object
org.axonframework.commandhandling.gateway.CommandGatewayFactory
Factory that creates
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:
-
The first parameter of the method is considered the payload of the message.
If the first parameter is a
Messageimplementation itself, a newMessageis created using the payload andMetaDataof theMessagepassed as parameter. -
Parameters that are annotated with
MetaDataValuewill cause the parameter values to be added asMetaDatavalues to the outgoingMessage. -
If the last two parameters are of type
longandTimeUnit, 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.
-
voidreturn types are always allowed. Unless another parameter makes the method blocking,voidmethods are non-blocking by default. -
Declaring a
Future,CompletableFuture, orCompletionStagereturn type will result in a non-blocking operation, given that the configuredCommandBusis asynchronous. For example, when the providedCommandBusis aSimpleCommandBus, the returnedFutureis still blocking. The returnedFutureallows you to retrieve the execution's result at your own convenience. Note that declared exceptions and timeouts are ignored. - Any other return type will cause the dispatch to block (optionally with timeout) until a result is available.
- Any checked exception declared on the method will cause it to block (optionally with timeout). If the command results in a declared checked exception, that exception is thrown from the method.
-
Declaring a
TimeoutExceptionwill throw that exception when a configured timeout expires. If no such exception is declared, but a timeout is configured, the method will returnnull. -
Declaring an
InterruptedExceptionwill throw that exception when a thread blocked while waiting for a response is interrupted. Not declaring the exception will have the method returnnullwhen a blocked thread is interrupted. Note that when noInterruptedExceptionis declared, the interrupt flag is set back on the interrupted thread.
- Any unchecked exception thrown during command handling will cause it to block. If the method is blocking (see below) the unchecked exception will be thrown from the method.
@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.- Since:
- 2.0
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aCommandGatewayFactory.static interfaceInterface towards the mechanism that handles a method call on a gateway interface method. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aCommandGatewayFactorybased on the fields contained in theCommandGatewayFactory.Builder. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Instantiate a Builder to be able to create aCommandGatewayFactory.<T> TcreateGateway(Class<T> gatewayInterface) Creates a gateway instance for the givengatewayInterface.<C,R> CommandGatewayFactory registerCommandCallback(CommandCallback<C, R> callback, ResponseType<R> responseType) Registers thecallback, 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.registerDispatchInterceptor(MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor) Registers the givendispatchInterceptorwhich is invoked for each Command dispatched through the Command Gateways created by this factory.protected <R> CommandGatewayFactory.InvocationHandler<R> Wrap the givendelegatein anCommandGatewayFactory.InvocationHandlerthat returns immediately after invoking thedelegate.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithFixedTimeout(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, long timeout, TimeUnit timeUnit) Wraps the givendelegateand waits for the result in theCompletableFutureto become available, with giventimeoutandtimeUnit.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithTimeoutInArguments(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, int timeoutIndex, int timeUnitIndex) Wraps the givendelegateand waits for the result in theCompletableFutureto become available using given indices to resolve the parameters that provide the timeout to use.protected <R> CommandGatewayFactory.InvocationHandler<R> Wraps the givendelegateand waits for the result in theCompletableFutureto become available.protected <R> CommandGatewayFactory.InvocationHandler<R> wrapUndeclaredExceptions(CommandGatewayFactory.InvocationHandler<R> delegate, Class<?>[] declaredExceptions) Wraps the givendelegatein anCommandGatewayFactory.InvocationHandlerthat wraps exceptions not declared on the method in aCommandExecutionException.
-
Constructor Details
-
CommandGatewayFactory
Instantiate aCommandGatewayFactorybased on the fields contained in theCommandGatewayFactory.Builder.Will assert that the
CommandBusis notnull, and will throw anAxonConfigurationExceptionif it isnull.- Parameters:
builder- TheCommandGatewayFactory.Builderused to instantiate aCommandGatewayFactoryinstance.
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aCommandGatewayFactory.The
CommandBusis a hard requirements and as such should be provided.- Returns:
- A Builder to be able to create a
CommandGatewayFactory.
-
createGateway
Creates a gateway instance for the givengatewayInterface. The returned instance is a Proxy that implements that interface.- Type Parameters:
T- The interface declaring the gateway methods.- Parameters:
gatewayInterface- The interface declaring the gateway methods.- Returns:
- A Proxy implementation implementing the given interface.
-
wrapUndeclaredExceptions
protected <R> CommandGatewayFactory.InvocationHandler<R> wrapUndeclaredExceptions(CommandGatewayFactory.InvocationHandler<R> delegate, Class<?>[] declaredExceptions) Wraps the givendelegatein anCommandGatewayFactory.InvocationHandlerthat wraps exceptions not declared on the method in aCommandExecutionException.- Type Parameters:
R- The response type of the command handler.- Parameters:
delegate- The delegate to invoke that potentially throws exceptions.declaredExceptions- The exceptions declared on the method signature.- Returns:
- An
CommandGatewayFactory.InvocationHandlerthat wraps undeclared exceptions in aCommandExecutionException.
-
wrapToFireAndForget
protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToFireAndForget(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate) Wrap the givendelegatein anCommandGatewayFactory.InvocationHandlerthat returns immediately after invoking thedelegate.- Type Parameters:
R- The response type of the command handler.- Parameters:
delegate- The delegate to invoke, potentially throwing an InterruptedException when invoked.- Returns:
- An
CommandGatewayFactory.InvocationHandlerthat wraps returns immediately after invoking the delegate.
-
wrapToWaitForResult
protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToWaitForResult(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate) Wraps the givendelegateand waits for the result in theCompletableFutureto become available. No explicit timeout is provided for the waiting.- Type Parameters:
R- The result of the command handler.- Parameters:
delegate- The delegate to invoke, returning aCompletableFuture- Returns:
- The result of the
CompletableFuture, either a return value or an exception.
-
wrapToReturnWithFixedTimeout
protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithFixedTimeout(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, long timeout, TimeUnit timeUnit) Wraps the givendelegateand waits for the result in theCompletableFutureto become available, with giventimeoutandtimeUnit.- Type Parameters:
R- The result of the command handler.- Parameters:
delegate- The delegate to invoke, returning aCompletableFuture.timeout- The amount of time to wait for the result to become available.timeUnit- The unit of time to wait.- Returns:
- The result of the
CompletableFuture, either a return value or an exception.
-
wrapToReturnWithTimeoutInArguments
protected <R> CommandGatewayFactory.InvocationHandler<R> wrapToReturnWithTimeoutInArguments(CommandGatewayFactory.InvocationHandler<CompletableFuture<R>> delegate, int timeoutIndex, int timeUnitIndex) Wraps the givendelegateand waits for the result in theCompletableFutureto become available using given indices to resolve the parameters that provide the timeout to use.- Type Parameters:
R- The result of the command handler.- Parameters:
delegate- The delegate to invoke, returning aCompletableFuture.timeoutIndex- The index of the argument providing the timeout.timeUnitIndex- The index of the argument providing the time unit.- Returns:
- The result of the
CompletableFuture, either a return value or an exception.
-
registerCommandCallback
public <C,R> CommandGatewayFactory registerCommandCallback(CommandCallback<C, R> callback, ResponseType<R> responseType) Registers thecallback, 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 theCommandCallback.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 causingClassCastException.- Type Parameters:
R- The type of return value the callback is interested in.- Parameters:
callback- The callback to register.responseType- The actual response type of the command.- Returns:
- This instance for further configuration.
-
registerDispatchInterceptor
public CommandGatewayFactory registerDispatchInterceptor(MessageDispatchInterceptor<CommandMessage<?>> dispatchInterceptor) Registers the givendispatchInterceptorwhich is invoked for each Command dispatched through the Command Gateways created by this factory.- Parameters:
dispatchInterceptor- The interceptor to register.- Returns:
- This instance for further configuration.
-