Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2010, Adam Taft
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the project owner nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.intelligentsia.eventbus;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* A simple Event Bus implementation which receives events or messages from
* various sources and distributes them to all subscribers of the event type.
* This is highly useful for programs which are event driven. Swing applications
* in particular can benefit from an event bus architecture, as opposed to the
* traditional event listener architecture it employs.
*
* The BasicEventBus class is thread safe and uses a background thread to notify
* the subscribers of the event. The subscribers are notified in a serial
* fashion, and only one event will be published at a time. Though, the
* {@link #publish(Object)} method is done in a non-blocking way.
*
* Subscribers subscribe to the EventBus using the {@link #subscribe(Object)}
* method. A specific subscriber type is not required, but the subscriber will
* be reflected to find all methods annotated with the {@link EventHandler}
* annotations. These methods will be invoked as needed by the event bus based
* on the type of the first parameter to the annotated method.
*
* An event handler can indicate that it can veto events by setting the
* {@link EventHandler#canVeto()} value to true. This will inform the EventBus
* of the subscriber's desire to veto the event. A vetoed event will not be sent
* to the regular subscribers.
*
* During publication of an event, all veto EventHandler methods will be
* notified first and allowed to throw a {@link VetoException} indicating that
* the event has been vetoed and should not be published to the remaining event
* handlers. If no vetoes have been made, the regular subscriber handlers will
* be notified of the event.
*
* Subscribers are stored using a {@link WeakReference} such that a memory leak
* can be avoided if the client fails to unsubscribe at the end of the use.
* However, calling the {@link #unsubscribe(Object)} method is highly
* recommended none-the-less.
*
* @author Adam Taft
* @author Jerome Guibert
*/
public final class BasicEventBus implements EventBus {
private final List handlers = new CopyOnWriteArrayList();
private final BlockingQueue