11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.eventbus; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.ConcurrentLinkedQueue; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.Executor; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * An {@link EventBus} that takes the Executor of your choice and uses it to 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * dispatch events, allowing dispatch to occur asynchronously. 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Cliff Biffle 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@Beta 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class AsyncEventBus extends EventBus { 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Executor executor; 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** the queue of events is shared across all threads */ 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final ConcurrentLinkedQueue<EventWithHandler> eventsToDispatch = 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new ConcurrentLinkedQueue<EventWithHandler>(); 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates a new AsyncEventBus that will use {@code executor} to dispatch 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * events. Assigns {@code identifier} as the bus's name for logging purposes. 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param identifier short name for the bus, for logging purposes. 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param executor Executor to use to dispatch events. It is the caller's 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * responsibility to shut down the executor after the last event has 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * been posted to this event bus. 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public AsyncEventBus(String identifier, Executor executor) { 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super(identifier); 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.executor = executor; 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates a new AsyncEventBus that will use {@code executor} to dispatch 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * events. 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param executor Executor to use to dispatch events. It is the caller's 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * responsibility to shut down the executor after the last event has 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * been posted to this event bus. 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public AsyncEventBus(Executor executor) { 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.executor = executor; 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected void enqueueEvent(Object event, EventHandler handler) { 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert eventsToDispatch.offer(new EventWithHandler(event, handler)); 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Dispatch {@code events} in the order they were posted, regardless of 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the posting thread. 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected void dispatchQueuedEvents() { 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert while (true) { 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert EventWithHandler eventWithHandler = eventsToDispatch.poll(); 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (eventWithHandler == null) { 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert break; 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert dispatch(eventWithHandler.event, eventWithHandler.handler); 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Calls the {@link #executor} to dispatch {@code event} to {@code handler}. 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected void dispatch(final Object event, final EventHandler handler) { 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert executor.execute(new Runnable() { 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @SuppressWarnings("synthetic-access") 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void run() { 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert AsyncEventBus.super.dispatch(event, handler); 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }); 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 100