org.axonframework.commandhandling
Class SimpleCommandBus

java.lang.Object
  extended by org.axonframework.commandhandling.SimpleCommandBus
All Implemented Interfaces:
CommandBus
Direct Known Subclasses:
AsynchronousCommandBus

public class SimpleCommandBus
extends Object
implements CommandBus

Implementation of the CommandBus that dispatches commands to the handlers subscribed to that specific type of command. Interceptors may be configured to add processing to commands regardless of their type, for example logging, security (authorization), sla monitoring, etc.

This class can be monitored as the implementation of the StatisticsProvider interface indicates.

Since:
0.5
Author:
Allard Buijze, Martin Tilma

Constructor Summary
SimpleCommandBus()
          Initializes the SimpleCommandBus.
 
Method Summary
 void dispatch(CommandMessage<?> command)
          Dispatch the given command to the CommandHandler subscribed to that type of command.
<R> void
dispatch(CommandMessage<?> command, CommandCallback<R> callback)
          Dispatch the given command to the CommandHandler subscribed to that type of command.
protected
<R> void
doDispatch(CommandMessage<?> command, CommandCallback<R> callback)
          Performs the actual dispatching logic.
protected  CommandMessage<?> intercept(CommandMessage<?> command)
          Invokes all the dispatch interceptors.
 void setDispatchInterceptors(List<? extends CommandDispatchInterceptor> dispatchInterceptors)
          Registers the given list of dispatch interceptors to the command bus.
 void setHandlerInterceptors(List<? extends CommandHandlerInterceptor> handlerInterceptors)
          Registers the given list of interceptors to the command bus.
 void setRollbackConfiguration(RollbackConfiguration rollbackConfiguration)
          Sets the RollbackConfiguration that allows you to change when the UnitOfWork is committed.
 void setSubscriptions(Map<?,?> handlers)
          Convenience method that allows you to register command handlers using a Dependency Injection framework.
 void setTransactionManager(TransactionManager transactionManager)
          Sets the transaction manager that manages the transaction around command handling.
 void setUnitOfWorkFactory(UnitOfWorkFactory unitOfWorkFactory)
          Sets the UnitOfWorkFactory that provides the UnitOfWork instances for handling incoming commands.
<T> void
subscribe(String commandName, CommandHandler<? super T> handler)
          Subscribe the given handler to commands of type commandType.
<T> boolean
unsubscribe(String commandName, CommandHandler<? super T> handler)
          Unsubscribe the given handler to commands of type commandType.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleCommandBus

public SimpleCommandBus()
Initializes the SimpleCommandBus.

Method Detail

dispatch

public void dispatch(CommandMessage<?> command)
Description copied from interface: CommandBus
Dispatch the given command to the CommandHandler subscribed to that type of command. No feedback is given about the status of the dispatching process. Implementations may return immediately after asserting a valid handler is registered for the given command.

Specified by:
dispatch in interface CommandBus
Parameters:
command - The Command to dispatch
See Also:
GenericCommandMessage.asCommandMessage(Object)

dispatch

public <R> void dispatch(CommandMessage<?> command,
                         CommandCallback<R> callback)
Description copied from interface: CommandBus
Dispatch the given command to the CommandHandler subscribed to that type of command. When the command is processed, on of the callback methods is called, depending on the result of the processing.

When the method returns, the only guarantee provided by the CommandBus implementation, is that the command has been successfully received. Implementations are highly recommended to perform basic validation of the command before returning from this method call.

Implementations must start a UnitOfWork when before dispatching the command, and either commit or rollback after a successful or failed execution, respectively.

Specified by:
dispatch in interface CommandBus
Type Parameters:
R - The type of the expected result
Parameters:
command - The Command to dispatch
callback - The callback to invoke when command processing is complete
See Also:
GenericCommandMessage.asCommandMessage(Object)

intercept

protected CommandMessage<?> intercept(CommandMessage<?> command)
Invokes all the dispatch interceptors.

Parameters:
command - The original command being dispatched
Returns:
The command to actually dispatch

doDispatch

protected <R> void doDispatch(CommandMessage<?> command,
                              CommandCallback<R> callback)
Performs the actual dispatching logic. The dispatch interceptors must have been invoked at this point.

Type Parameters:
R - The type of result expected from the command handler
Parameters:
command - The actual command to dispatch to the handler
callback - The callback to notify of the result

subscribe

public <T> void subscribe(String commandName,
                          CommandHandler<? super T> handler)
Subscribe the given handler to commands of type commandType. If a subscription already exists for the given type, then the new handler takes over the subscription.

Specified by:
subscribe in interface CommandBus
Type Parameters:
T - The Type of command
Parameters:
commandName - The type of command to subscribe the handler to
handler - The handler instance that handles the given type of command

unsubscribe

public <T> boolean unsubscribe(String commandName,
                               CommandHandler<? super T> handler)
Unsubscribe the given handler to commands of type commandType. If the handler is not currently assigned to that type of command, no action is taken.

Specified by:
unsubscribe in interface CommandBus
Type Parameters:
T - The Type of command
Parameters:
commandName - The name of the command the handler is subscribed to
handler - The handler instance to unsubscribe from the CommandBus
Returns:
true of this handler is successfully unsubscribed, false of the given handler was not the current handler for given commandType.

setHandlerInterceptors

public void setHandlerInterceptors(List<? extends CommandHandlerInterceptor> handlerInterceptors)
Registers the given list of interceptors to the command bus. All incoming commands will pass through the interceptors at the given order before the command is passed to the handler for processing.

Parameters:
handlerInterceptors - The interceptors to invoke when commands are handled

setDispatchInterceptors

public void setDispatchInterceptors(List<? extends CommandDispatchInterceptor> dispatchInterceptors)
Registers the given list of dispatch interceptors to the command bus. All incoming commands will pass through the interceptors at the given order before the command is dispatched toward the command handler.

Parameters:
dispatchInterceptors - The interceptors to invoke when commands are dispatched

setSubscriptions

public void setSubscriptions(Map<?,?> handlers)
Convenience method that allows you to register command handlers using a Dependency Injection framework. The parameter of this method is a Map<Object<T>, CommandHandler<? super T>>. The key represents the type (either as a String or Class) of command to register the handler for, the value is the actual handler.

Parameters:
handlers - The handlers to subscribe in the form of a Map of Class - CommandHandler entries.

setUnitOfWorkFactory

public void setUnitOfWorkFactory(UnitOfWorkFactory unitOfWorkFactory)
Sets the UnitOfWorkFactory that provides the UnitOfWork instances for handling incoming commands. Defaults to a DefaultUnitOfWorkFactory.

This method should not be used in combination with setTransactionManager(org.axonframework.unitofwork.TransactionManager). For transaction support, ensure the provided UnitOfWorkFactory implementation binds each UnitOfWork to a transaction.

Parameters:
unitOfWorkFactory - The UnitOfWorkFactory providing UoW instances for this Command Bus.

setTransactionManager

public void setTransactionManager(TransactionManager transactionManager)
Sets the transaction manager that manages the transaction around command handling. This should not be used in combination with setUnitOfWorkFactory(org.axonframework.unitofwork.UnitOfWorkFactory).

Parameters:
transactionManager - the transaction manager to use

setRollbackConfiguration

public void setRollbackConfiguration(RollbackConfiguration rollbackConfiguration)
Sets the RollbackConfiguration that allows you to change when the UnitOfWork is committed. If not set the RollbackOnUncheckedExceptionConfiguration will be used, which triggers a rollback on all unchecked exceptions.

Parameters:
rollbackConfiguration - The RollbackConfiguration.


Copyright © 2010-2016. All Rights Reserved.