Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010. 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.eventhandling.annotation;
 
 
 import java.util.Map;
Spring Bean post processor that automatically generates an adapter for each bean containing EventHandler annotated methods.

Author(s):
Allard Buijze
Since:
0.3
 
 
     private Executor executor;
     private EventBus eventBus;
 
     @Override
     protected Class<?> getAdapterInterface() {
         return EventListener.class;
     }

    
Create an AnnotationEventListenerAdapter instance of the given bean. This adapter will receive all event handler calls to be handled by this bean.

Parameters:
bean The bean that the EventListenerAdapter has to adapt
Returns:
an event handler adapter for the given bean
 
     @Override
         adapter.subscribe();
         return adapter;
     }
 
     @Override
     protected boolean isPostProcessingCandidate(Class<?> targetClass) {
         return isNotAggregateRoot(targetClass)
                 && isNotEventHandlerSubclass(targetClass)
                 && hasEventHandlerMethod(targetClass);
     }

    
 
     @SuppressWarnings({"unchecked"})
     @Override
     public void afterPropertiesSet() throws Exception {
         // if no EventBus is set, find one in the application context
         if ( == null) {
             Map<StringEventBusbeans = getApplicationContext().getBeansOfType(EventBus.class);
             if (beans.size() != 1) {
                 throw new IllegalStateException("If no specific EventBus is provided, the application context must "
                         + "contain exactly one bean of type EventBus. The current application context has: "
                         + beans.size());
             }
             this. = beans.entrySet().iterator().next().getValue();
         }
     }
 
     private boolean isNotAggregateRoot(Class<?> targetClass) {
         return !AggregateRoot.class.isAssignableFrom(targetClass);
     }
 
     private boolean isNotEventHandlerSubclass(Class<?> beanClass) {
         return !EventListener.class.isAssignableFrom(beanClass);
     }
 
     private boolean hasEventHandlerMethod(Class<?> beanClass) {
         final AtomicBoolean result = new AtomicBoolean(false);
         ReflectionUtils.doWithMethods(beanClassnew HasEventHandlerAnnotationMethodCallback(result));
         return result.get();
     }

    
Sets the Executor to use when the AnnotationEventListenerBeanPostProcessor encounters event listeners with the AsynchronousEventListener annotation.

Parameters:
executor the Executor to use for asynchronous event listeners
    public void setExecutor(Executor executor) {
        this. = executor;
    }

    
Sets the event bus to which detected event listeners should be subscribed. If none is provided, the event bus will be automatically detected in the application context.

Parameters:
eventBus the event bus to subscribe detected event listeners to
    public void setEventBus(EventBus eventBus) {
        this. = eventBus;
    }
    private static final class HasEventHandlerAnnotationMethodCallback implements ReflectionUtils.MethodCallback {
        private final AtomicBoolean result;
        private HasEventHandlerAnnotationMethodCallback(AtomicBoolean result) {
            this. = result;
        }

        
        @Override
        public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
            if (method.isAnnotationPresent(EventHandler.class)) {
                .set(true);
            }
        }
    }
New to GrepCode? Check out our FAQ X