Class CommandGatewayFactory

java.lang.Object
org.axonframework.commandhandling.gateway.CommandGatewayFactory

public class CommandGatewayFactory extends Object
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 Message implementation itself, a new Message is created using the payload and MetaData of the Message passed as parameter.
  • Parameters that are annotated with MetaDataValue will cause the parameter values to be added as MetaData values to the outgoing Message.
  • If the last two parameters are of type 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.

Effect of return values:

  • void return types are always allowed. Unless another parameter makes the method blocking, void methods are non-blocking by default.
  • Declaring a 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.
  • Any other return type will cause the dispatch to block (optionally with timeout) until a result is available.

Effect of declared exceptions:

  • 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 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.
  • Declaring an 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.

Effect of unchecked exceptions:

  • 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.

Finally, the @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:

  • It declares a return type other than void or Future, or
  • It declares an exception, or
  • the last two parameters are of type TimeUnit and long, or
  • The method is annotated with @Timeout.
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