Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * 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 com.google.common.eventbus;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
An EventBus that takes the Executor of your choice and uses it to dispatch events, allowing dispatch to occur asynchronously.

Author(s):
Cliff Biffle
Since:
10.0
 
 public class AsyncEventBus extends EventBus {
   private final Executor executor;

  
the queue of events is shared across all threads
 
       new ConcurrentLinkedQueue<EventWithSubscriber>();

  
Creates a new AsyncEventBus that will use executor to dispatch events. Assigns identifier as the bus's name for logging purposes.

Parameters:
identifier short name for the bus, for logging purposes.
executor Executor to use to dispatch events. It is the caller's responsibility to shut down the executor after the last event has been posted to this event bus.
 
   public AsyncEventBus(String identifierExecutor executor) {
     super(identifier);
     this. = checkNotNull(executor);
   }

  
Creates a new AsyncEventBus that will use executor to dispatch events.

Parameters:
executor Executor to use to dispatch events. It is the caller's responsibility to shut down the executor after the last event has been posted to this event bus.
subscriberExceptionHandler Handler used to handle exceptions thrown from subscribers. See SubscriberExceptionHandler for more information.
Since:
16.0
 
   public AsyncEventBus(Executor executorSubscriberExceptionHandler subscriberExceptionHandler) {
     super(subscriberExceptionHandler);
     this. = checkNotNull(executor);
   }

  
Creates a new AsyncEventBus that will use executor to dispatch events.

Parameters:
executor Executor to use to dispatch events. It is the caller's responsibility to shut down the executor after the last event has been posted to this event bus.
 
   public AsyncEventBus(Executor executor) {
     super("default");
     this. = checkNotNull(executor);
   }
 
   @Override
   void enqueueEvent(Object eventEventSubscriber subscriber) {
     .offer(new EventWithSubscriber(eventsubscriber));
   }

  
Dispatch events in the order they were posted, regardless of the posting thread.
 
   @SuppressWarnings("deprecation"// only deprecated for external subclasses
   @Override
   protected void dispatchQueuedEvents() {
     while (true) {
       EventWithSubscriber eventWithSubscriber = .poll();
       if (eventWithSubscriber == null) {
         break;
      }
      dispatch(eventWithSubscriber.eventeventWithSubscriber.subscriber);
    }
  }

  
Calls the executor to dispatch event to subscriber.
  void dispatch(final Object eventfinal EventSubscriber subscriber) {
    checkNotNull(event);
    checkNotNull(subscriber);
        new Runnable() {
          @Override
          public void run() {
            AsyncEventBus.super.dispatch(eventsubscriber);
          }
        });
  }
New to GrepCode? Check out our FAQ X