org.axonframework.commandhandling.gateway
Class GatewayProxyFactory

java.lang.Object
  extended by org.axonframework.commandhandling.gateway.GatewayProxyFactory

public class GatewayProxyFactory
extends Object

Factory that creates Gateway 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:

Effect of return values

Effect of declared exceptions

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:

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
static interface GatewayProxyFactory.InvocationHandler<R>
          Interface towards the mechanism that handles a method call on a gateway interface method.
 
Constructor Summary
GatewayProxyFactory(CommandBus commandBus, CommandDispatchInterceptor... dispatchInterceptors)
          Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors.
GatewayProxyFactory(CommandBus commandBus, RetryScheduler retryScheduler, CommandDispatchInterceptor... commandDispatchInterceptors)
          Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors.
GatewayProxyFactory(CommandBus commandBus, RetryScheduler retryScheduler, List<CommandDispatchInterceptor> commandDispatchInterceptors)
          Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors.
 
Method Summary
<T> T
createGateway(Class<T> gatewayInterface)
          Creates a gateway instance for the given gatewayInterface.
<R> GatewayProxyFactory
registerCommandCallback(CommandCallback<R> callback)
          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.
 GatewayProxyFactory registerDispatchInterceptor(CommandDispatchInterceptor dispatchInterceptor)
          Registers the given dispatchInterceptor which is invoked for each Command dispatched through the Command Gateways created by this factory.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToFireAndForget(GatewayProxyFactory.InvocationHandler<Future<R>> delegate)
          Wrap the given delegate in an InvocationHandler that returns immediately after invoking the delegate.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToReturnNullOnInterrupted(GatewayProxyFactory.InvocationHandler<R> delegate)
          Wrap the given delegate in an InvocationHandler that returns null when the delegate throws an InterruptedException.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToReturnNullOnTimeout(GatewayProxyFactory.InvocationHandler<R> delegate)
          Wrap the given delegate in an InvocationHandler that returns null when the delegate throws a TimeoutException.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToReturnWithFixedTimeout(GatewayProxyFactory.InvocationHandler<Future<R>> delegate, long timeout, TimeUnit timeUnit)
          Wraps the given delegate and waits for the result in the Future to become available, with given timeout and timeUnit.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToReturnWithTimeoutInArguments(GatewayProxyFactory.InvocationHandler<Future<R>> delegate, int timeoutIndex, int timeUnitIndex)
          Wraps the given delegate and waits for the result in the Future to become available using given indices to resolve the parameters that provide the timeout to use.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapToWaitForResult(GatewayProxyFactory.InvocationHandler<Future<R>> delegate)
          Wraps the given delegate and waits for the result in the Future to become available.
protected
<R> GatewayProxyFactory.InvocationHandler<R>
wrapUndeclaredExceptions(GatewayProxyFactory.InvocationHandler<R> delegate, Class<?>[] declaredExceptions)
          Wraps the given delegate in an InvocationHandler that wraps exceptions not declared on the method in a CommandExecutionException.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GatewayProxyFactory

public GatewayProxyFactory(CommandBus commandBus,
                           CommandDispatchInterceptor... dispatchInterceptors)
Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors.

Note that the given dispatchInterceptors are applied only on commands sent through gateways that have been created using this factory.

Parameters:
commandBus - The CommandBus on which to dispatch the Command Messages
dispatchInterceptors - The interceptors to invoke before dispatching commands to the Command Bus

GatewayProxyFactory

public GatewayProxyFactory(CommandBus commandBus,
                           RetryScheduler retryScheduler,
                           CommandDispatchInterceptor... commandDispatchInterceptors)
Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors. The given retryScheduler will reschedule commands for dispatching if a previous attempt resulted in an exception.

Note that the given dispatchInterceptors are applied only on commands sent through gateways that have been created using this factory.

Parameters:
commandBus - The CommandBus on which to dispatch the Command Messages
retryScheduler - The scheduler that will decide whether to reschedule commands, may be null to report failures without rescheduling
commandDispatchInterceptors - The interceptors to invoke before dispatching commands to the Command Bus

GatewayProxyFactory

public GatewayProxyFactory(CommandBus commandBus,
                           RetryScheduler retryScheduler,
                           List<CommandDispatchInterceptor> commandDispatchInterceptors)
Initialize the factory sending Commands to the given commandBus, optionally intercepting them with given dispatchInterceptors. The given retryScheduler will reschedule commands for dispatching if a previous attempt resulted in an exception.

Note that the given dispatchInterceptors are applied only on commands sent through gateways that have been created using this factory.

Parameters:
commandBus - The CommandBus on which to dispatch the Command Messages
retryScheduler - The scheduler that will decide whether to reschedule commands, may be null to report failures without rescheduling
commandDispatchInterceptors - The interceptors to invoke before dispatching commands to the Command Bus
Method Detail

createGateway

public <T> T createGateway(Class<T> gatewayInterface)
Creates a gateway instance for the given gatewayInterface. 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> GatewayProxyFactory.InvocationHandler<R> wrapUndeclaredExceptions(GatewayProxyFactory.InvocationHandler<R> delegate,
                                                                                Class<?>[] declaredExceptions)
Wraps the given delegate in an InvocationHandler that wraps exceptions not declared on the method in a CommandExecutionException.

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 InvocationHandler that wraps undeclared exceptions in a CommandExecutionException

wrapToReturnNullOnInterrupted

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToReturnNullOnInterrupted(GatewayProxyFactory.InvocationHandler<R> delegate)
Wrap the given delegate in an InvocationHandler that returns null when the delegate throws an InterruptedException.

Type Parameters:
R - The response type of the command handler
Parameters:
delegate - The delegate to invoke, potentially throwing an InterruptedException when invoked
Returns:
an InvocationHandler that wraps returns null when an InterruptedException is thrown

wrapToReturnNullOnTimeout

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToReturnNullOnTimeout(GatewayProxyFactory.InvocationHandler<R> delegate)
Wrap the given delegate in an InvocationHandler that returns null when the delegate throws a TimeoutException.

Type Parameters:
R - The response type of the command handler
Parameters:
delegate - The delegate to invoke, potentially throwing a TimeoutException when invoked
Returns:
an InvocationHandler that wraps returns null when a TimeoutException is thrown

wrapToFireAndForget

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToFireAndForget(GatewayProxyFactory.InvocationHandler<Future<R>> delegate)
Wrap the given delegate in an InvocationHandler that returns immediately after invoking the delegate.

Type Parameters:
R - The response type of the command handler
Parameters:
delegate - The delegate to invoke, potentially throwing an InterruptedException when invoked
Returns:
an InvocationHandler that wraps returns immediately after invoking the delegate

wrapToWaitForResult

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToWaitForResult(GatewayProxyFactory.InvocationHandler<Future<R>> delegate)
Wraps the given delegate and waits for the result in the Future to 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 a Future
Returns:
the result of the Future, either a return value or an exception

wrapToReturnWithFixedTimeout

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToReturnWithFixedTimeout(GatewayProxyFactory.InvocationHandler<Future<R>> delegate,
                                                                                    long timeout,
                                                                                    TimeUnit timeUnit)
Wraps the given delegate and waits for the result in the Future to become available, with given timeout and timeUnit.

Type Parameters:
R - The result of the command handler
Parameters:
delegate - The delegate to invoke, returning a Future
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 Future, either a return value or an exception

wrapToReturnWithTimeoutInArguments

protected <R> GatewayProxyFactory.InvocationHandler<R> wrapToReturnWithTimeoutInArguments(GatewayProxyFactory.InvocationHandler<Future<R>> delegate,
                                                                                          int timeoutIndex,
                                                                                          int timeUnitIndex)
Wraps the given delegate and waits for the result in the Future to 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 a Future
timeoutIndex - The index of the argument providing the timeout
timeUnitIndex - The index of the argument providing the time unit
Returns:
the result of the Future, either a return value or an exception

registerCommandCallback

public <R> GatewayProxyFactory registerCommandCallback(CommandCallback<R> callback)
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.

Axon will check the signature of the onSuccess() 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.

Type Parameters:
R - The type of return value the callback is interested in
Parameters:
callback - The callback to register
Returns:
this instance for further configuration

registerDispatchInterceptor

public GatewayProxyFactory registerDispatchInterceptor(CommandDispatchInterceptor dispatchInterceptor)
Registers the given dispatchInterceptor which 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


Copyright © 2010-2016. All Rights Reserved.