Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
   *
   * This program is licensed to you under the Apache License Version 2.0,
   * and you may not use this file except in compliance with the Apache License Version 2.0.
   * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the Apache License Version 2.0 is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
  */
 
 package org.sonatype.sisu.goodies.eventbus.internal;
 
 
 
 
 
 import static com.google.common.base.Preconditions.checkNotNull;

org.sonatype.sisu.goodies.eventbus.EventBus implementation using guava event bus.

It differs from guava event bus by dispatching events as they appear (is re-entrant). Guava will queue up all event and dispatch them in the order they were posted, without re-entrance.

Since:
1.2
 
 public class DefaultEventBus
     implements EventBus
 {
 
   static final Logger LOG = LoggerFactory.getLogger(DefaultEventBus.class);
 
   private static final Marker REGISTRATION = MarkerFactory.getMarker("registration");
 
   private static final Marker EVENTS = MarkerFactory.getMarker("events");
 
   static final Marker DISPATCHING = MarkerFactory.getMarker("dispatching");
 
 
   private final BeanLocator beanLocator;
 
   private final AtomicBoolean handlersLoaded;
 
   @Inject
   public DefaultEventBus(final @Named("${guava.eventBus:-reentrant}")
                          org.sonatype.sisu.goodies.eventbus.internal.guava.EventBus eventBus,
                          final BeanLocator beanLocator)
   {
     this. = checkNotNull(beanLocator);
     this. = checkNotNull(eventBus);
      = new AtomicBoolean(false);
     .info("Using {}"eventBus);
   }
 
   @Override
   public EventBus register(final Object handler) {
     .register(handler);
     .debug("Registered handler '{}'"handler);
     return this;
   }
 
   @Override
   public EventBus unregister(final Object handler) {
     .unregister(handler);
     .debug("Unregistered handler '{}'"handler);
     return this;
   }
 
   @Override
   public EventBus post(final Object event) {
     if (.compareAndSet(falsetrue)) {
     }
     .debug("Event '{}' fired"event);
     .post(event);
     return this;
   }
 
   private void registerHandlers(final BeanLocator beanLocator) {
    .debug("Loading automatically registrable handlers");
    beanLocator.watch(
        Key.get(Object.class),
        new Mediator<AnnotationObjectDefaultEventBus>()
        {
          @Override
          public void add(final BeanEntry<AnnotationObjectentryfinal DefaultEventBus watcher)
              throws Exception
          {
            if (entry.getImplementationClass().isAnnotationPresent(EventBus.Managed.class)) {
              register(entry.getValue());
            }
          }
          @Override
          public void remove(final BeanEntry<AnnotationObjectentryfinal DefaultEventBus watcher)
              throws Exception
          {
            if (entry.getImplementationClass().isAnnotationPresent(EventBus.Managed.class)) {
              unregister(entry.getValue());
            }
          }
        },
        this
    );
  }
New to GrepCode? Check out our FAQ X