org.axonframework.commandhandling.annotation
Class AnnotationCommandHandlerAdapter

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

public class AnnotationCommandHandlerAdapter
extends Object
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
 Object handle(CommandMessage<Object> command, UnitOfWork unitOfWork)
          Invokes the @CommandHandler annotated method that accepts the given command.
 void subscribe()
          Subscribe the command handlers to the command bus assigned during the initialization.
static AnnotationCommandHandlerAdapter subscribe(Object annotatedCommandHandler, CommandBus commandBus)
          Subscribe the annotated command handler to the given command bus.
 void unsubscribe()
          Unsubscribe the command handlers from the command bus assigned during the initialization.
 
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

subscribe

public static AnnotationCommandHandlerAdapter subscribe(Object annotatedCommandHandler,
                                                        CommandBus commandBus)
Subscribe the annotated command handler to the given command bus.

Parameters:
annotatedCommandHandler - The annotated command handler that is to be subscribed to the command bus
commandBus - The command bus that gets the handler's subscription
Returns:
the Adapter created for the command handler target. Can be used to unsubscribe.

handle

public Object handle(CommandMessage<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 null 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


Copyright © 2010-2012. All Rights Reserved.