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
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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.

Author(s):
Allard Buijze
Since:
0.5
See also:
CommandHandler
 
 
     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.

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

    
Invokes the

Parameters:
command The command to handle
unitOfWork The UnitOfWork the command is processed in
Returns:
the result of the command handling. Is java.lang.Void.TYPE when the annotated handler has a void return value.
Throws:
org.axonframework.commandhandling.NoHandlerForCommandException when no handler is found for given command.
java.lang.Throwable any exception occurring while handling the command
CommandHandler:
annotated method that accepts the given command.
 
     @Override
     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.
 
     @Override
     @PostConstruct
     public void subscribe() {
         List<Class<?>> acceptedCommands = findAcceptedHandlerParameters();
         for (Class<?> acceptedCommand : acceptedCommands) {
             .subscribe(acceptedCommandthis);
         }
     }

    
Unsubscribe the command handlers from the command bus assigned during the initialization.
    @Override
    @PreDestroy
    public void unsubscribe() {
        List<Class<?>> acceptedCommands = findAcceptedHandlerParameters();
        for (Class<?> acceptedCommand : acceptedCommands) {
            .unsubscribe(acceptedCommandthis);
        }
    }
    @SuppressWarnings({"unchecked"})
    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;
    }
    @Override
    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