All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.skuzzle.jeve.AsynchronousEventProvider Maven / Gradle / Ivy

package de.skuzzle.jeve;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;




/**
 * This EventProvider fires events asynchronously using an {@link ExecutorService} for
 * managing the creation of threads.
 * 
 * @author Simon Taddiken
 * @since 1.0.0
 */
class AsynchronousEventProvider extends AbstractEventProvider {
    
    /** Service which serves for creating and reusing threads.  */
    protected final ExecutorService dispatchPool;
    
    
    /**
     * Creates a new {@link AsynchronousEventProvider} which uses a single threaded
     * {@link ExecutorService}.
     */
    public AsynchronousEventProvider() {
        this(Executors.newFixedThreadPool(1));
    }
    
    
    
    /**
     * Creates a new {@link AsynchronousEventProvider} which uses the provided 
     * {@link ExecutorService} for event dispatching.
     * 
     * @param dispatcher ExecutorService to use.
     */
    public AsynchronousEventProvider(ExecutorService dispatcher) {
        if (dispatcher == null) {
            throw new IllegalArgumentException("dispatcher is null");
        }
        this.dispatchPool = dispatcher;
    }
    
    
    
    @Override
    public > void dispatch(
            Class listenerClass, E event, BiConsumer bc, ExceptionCallback ec) {
        
        this.checkDispatchArgs(listenerClass, event, bc, ec);
        if (this.canDispatch()) {
            this.dispatchPool.execute(() -> notifyListeners(listenerClass, event, bc, ec));
        }
        
    }
    
    
    
    @Override
    public boolean canDispatch() {
        return !this.dispatchPool.isShutdown() && !this.dispatchPool.isTerminated();
    }
    
    
    
    @Override
    public boolean isSequential() {
        return true;
    }
    
    
    
    @Override
    public void close() {
        super.close();
        this.dispatchPool.shutdownNow();
        try {
            this.dispatchPool.awaitTermination(2000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy