Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2010-2011. Axon Framework
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.axonframework.commandhandling.annotation;
 import java.util.List;
Adapter that turns any @CommandHandler annotated bean into a org.axonframework.commandhandling.CommandHandler implementation. Each annotated method is subscribed as a CommandHandler at the org.axonframework.commandhandling.CommandBus for the command type specified by the parameter of that method.

Allard Buijze
See also:
     private final 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.

target The object containing the @CommandHandler annotated methods
commandBus The command bus to which the handlers must be subscribed
     public AnnotationCommandHandlerAdapter(Object targetCommandBus commandBus) {
         this. = commandBus;

Invokes the

command The command to handle
unitOfWork The UnitOfWork the command is processed in
the result of the command handling. Is java.lang.Void.TYPE when the annotated handler has a void return value.
org.axonframework.commandhandling.NoHandlerForCommandException when no handler is found for given command.
java.lang.Throwable any exception occurring while handling the command
annotated method that accepts the given command.
     public Object handle(Object commandUnitOfWork unitOfWorkthrows Throwable {
         try {
             return invokeHandlerMethod(commandunitOfWork);
         } catch (IllegalAccessException e) {
             throw new UnsupportedOperationException(String.format(
                     "An error occurred when handling a command of type [%s]",
                     command.getClass().getSimpleName()), e);
         } catch (InvocationTargetException e) {
             throw e.getCause();

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.
     public void subscribe() {
         List<Class<?>> acceptedCommands = findAcceptedHandlerParameters();
         for (Class<?> acceptedCommand : acceptedCommands) {

Unsubscribe the command handlers from the command bus assigned during the initialization.
    public void unsubscribe() {
        List<Class<?>> acceptedCommands = findAcceptedHandlerParameters();
        for (Class<?> acceptedCommand : acceptedCommands) {
    private <T> List<Class<? extends T>> findAcceptedHandlerParameters() {
        final List<Class<? extends T>> handlerParameters = new LinkedList<Class<? extends T>>();
        for (Method m : ReflectionUtils.methodsOf(getTargetType())) {
            if (m.isAnnotationPresent(CommandHandler.class)) {
                handlerParameters.add((Class<T>) m.getParameterTypes()[0]);
        return handlerParameters;
    protected Object onNoMethodFound(Class<?> parameterType) {
        throw new NoHandlerForCommandException(
                String.format("No Handler found for a command of type[%s]"parameterType.getSimpleName()));
New to GrepCode? Check out our FAQ X