org.axonframework.commandhandling.annotation
Class AnnotationCommandHandlerAdapter

java.lang.Object
  extended by org.axonframework.util.AbstractHandlerInspector
      extended by org.axonframework.util.AbstractHandlerInvoker
          extended by org.axonframework.commandhandling.annotation.AnnotationCommandHandlerAdapter
All Implemented Interfaces:
CommandHandler<Object>, Subscribable

public class AnnotationCommandHandlerAdapter
extends AbstractHandlerInvoker
implements CommandHandler<Object>, Subscribable

Adapter that turns any @CommandHandler annotated bean into a CommandHandler implementation. Each annotated method is subscribed as a CommandHandler at the CommandBus for the command type specified by the parameter of that method.

Since:
0.5
Author:
Allard Buijze
See Also:
CommandHandler

Constructor Summary
AnnotationCommandHandlerAdapter(Object target, CommandBus commandBus)
          Initialize the command handler adapter for the given target which is to be subscribed with the given commandBus.
 
Method Summary
 Method findCommandHandlerMethodFor(Object command)
          Returns the method that will be called to handle the given command.
 Object handle(Object command, UnitOfWork unitOfWork)
          Invokes the @CommandHandler annotated method that accepts the given command.
protected  Object onNoMethodFound(Class<?> parameterType)
          Indicates what needs to happen when no handler is found for a given parameter.
 void subscribe()
          Subscribe the command handlers to the command bus assigned during the initialization.
 void unsubscribe()
          Unsubscribe the command handlers from the command bus assigned during the initialization.
 
Methods inherited from class org.axonframework.util.AbstractHandlerInvoker
getTarget, invokeHandlerMethod, invokeHandlerMethod
 
Methods inherited from class org.axonframework.util.AbstractHandlerInspector
findHandlerMethod
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnnotationCommandHandlerAdapter

public AnnotationCommandHandlerAdapter(Object target,
                                       CommandBus commandBus)
Initialize the command handler adapter for the given target which is to be subscribed with the given commandBus.

Note that you need to call subscribe() to actually subscribe the command handlers to the command bus.

Parameters:
target - The object containing the @CommandHandler annotated methods
commandBus - The command bus to which the handlers must be subscribed
Method Detail

handle

public Object handle(Object command,
                     UnitOfWork unitOfWork)
              throws Throwable
Invokes the @CommandHandler annotated method that accepts the given command.

Specified by:
handle in interface CommandHandler<Object>
Parameters:
command - The command to handle
unitOfWork - The UnitOfWork the command is processed in
Returns:
the result of the command handling. Is Void.TYPE when the annotated handler has a void return value.
Throws:
NoHandlerForCommandException - when no handler is found for given command.
Throwable - any exception occurring while handling the command

subscribe

@PostConstruct
public void subscribe()
Subscribe the command handlers to the command bus assigned during the initialization. A subscription is made with the command bus for each accepted type of command.

Specified by:
subscribe in interface Subscribable

unsubscribe

@PreDestroy
public void unsubscribe()
Unsubscribe the command handlers from the command bus assigned during the initialization.

Specified by:
unsubscribe in interface Subscribable

onNoMethodFound

protected Object onNoMethodFound(Class<?> parameterType)
Description copied from class: AbstractHandlerInvoker
Indicates what needs to happen when no handler is found for a given parameter. The default behavior is to return Void.TYPE.

Overrides:
onNoMethodFound in class AbstractHandlerInvoker
Parameters:
parameterType - The type of parameter for which no handler could be found
Returns:
the value to return when no handler method is found. Defaults to Void.TYPE.

findCommandHandlerMethodFor

public Method findCommandHandlerMethodFor(Object command)
Returns the method that will be called to handle the given command. Returns null is no such method is found.

Parameters:
command - The command to find the handler method for
Returns:
the command handler method for the given command


Copyright © 2011. All Rights Reserved.