001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.eventbus;
018
019import com.google.common.annotations.Beta;
020
021import java.util.concurrent.Executor;
022
023/**
024 * An {@link EventBus} that takes the Executor of your choice and uses it to
025 * dispatch events, allowing dispatch to occur asynchronously.
026 *
027 * @author Cliff Biffle
028 * @since 10.0
029 */
030@Beta
031public class AsyncEventBus extends EventBus {
032
033  /**
034   * Creates a new AsyncEventBus that will use {@code executor} to dispatch
035   * events.  Assigns {@code identifier} as the bus's name for logging purposes.
036   *
037   * @param identifier short name for the bus, for logging purposes.
038   * @param executor   Executor to use to dispatch events. It is the caller's
039   *        responsibility to shut down the executor after the last event has
040   *        been posted to this event bus.
041   */
042  public AsyncEventBus(String identifier, Executor executor) {
043    super(identifier, executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE);
044  }
045
046  /**
047   * Creates a new AsyncEventBus that will use {@code executor} to dispatch
048   * events.
049   *
050   * @param executor Executor to use to dispatch events. It is the caller's
051   *        responsibility to shut down the executor after the last event has
052   *        been posted to this event bus.
053   * @param subscriberExceptionHandler Handler used to handle exceptions thrown from subscribers.
054   *    See {@link SubscriberExceptionHandler} for more information.
055   * @since 16.0
056   */
057  public AsyncEventBus(Executor executor, SubscriberExceptionHandler subscriberExceptionHandler) {
058    super("default", executor, Dispatcher.legacyAsync(), subscriberExceptionHandler);
059  }
060
061  /**
062   * Creates a new AsyncEventBus that will use {@code executor} to dispatch
063   * events.
064   *
065   * @param executor Executor to use to dispatch events. It is the caller's
066   *        responsibility to shut down the executor after the last event has
067   *        been posted to this event bus.
068   */
069  public AsyncEventBus(Executor executor) {
070    super("default", executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE);
071  }
072}