001/* 002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining 005 * a copy of this software and associated documentation files (the 006 * "Software"), to deal in the Software without restriction, including 007 * without limitation the rights to use, copy, modify, merge, publish, 008 * distribute, sublicense, and/or sell copies of the Software, and to 009 * permit persons to whom the Software is furnished to do so, subject to 010 * the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be 013 * included in all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 016 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 018 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 019 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 020 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 021 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 022 */ 023 024package co.aikar.taskchain; 025 026import java.util.HashMap; 027import java.util.Map; 028import java.util.Queue; 029import java.util.concurrent.TimeUnit; 030import java.util.function.BiConsumer; 031 032@SuppressWarnings({"WeakerAccess", "unused"}) 033public class TaskChainFactory { 034 private final GameInterface impl; 035 private final AsyncQueue asyncQueue; 036 private final Map<String, Queue<SharedTaskChain>> sharedChains = new HashMap<>(); 037 volatile private BiConsumer<Exception, TaskChainTasks.Task<?, ?>> defaultErrorHandler; 038 volatile boolean shutdown = false; 039 040 @SuppressWarnings("WeakerAccess") 041 public TaskChainFactory(GameInterface impl) { 042 this.impl = impl; 043 this.asyncQueue = impl.getAsyncQueue(); 044 impl.registerShutdownHandler(this); 045 } 046 047 GameInterface getImplementation() { 048 return impl; 049 } 050 051 public Map<String, Queue<SharedTaskChain>> getSharedChains() { 052 return sharedChains; 053 } 054 055 /** 056 * Creates a new chain. 057 */ 058 public <T> TaskChain<T> newChain() { 059 return new TaskChain<>(this); 060 } 061 062 /** 063 * Allows re-use of a Chain by giving it a name. This lets you keep adding Tasks to 064 * an already executing chain. This allows you to assure a sequence of events to only 065 * execute one at a time, but may be registered and executed from multiple execution points 066 * or threads. 067 * 068 * Task Data is not shared between chains of the same name. The only thing that is shared 069 * is execution order, in that 2 sequences of events can not run at the same time. 070 * 071 * If 2 chains are created at same time under same name, the first chain will execute fully before the 2nd chain will start, no matter how long 072 * 073 * @param name Name of the shared chain. Case sensitive 074 */ 075 public synchronized <T> TaskChain<T> newSharedChain(String name) { 076 //noinspection unchecked 077 return new SharedTaskChain<>(name, this); 078 } 079 080 /** 081 * Returns the default error handler that will be used by all chains created by this factory, 082 * if they do not suspply their own error handler. 083 * @return The current default error handler 084 */ 085 public BiConsumer<Exception, TaskChainTasks.Task<?, ?>> getDefaultErrorHandler() { 086 return defaultErrorHandler; 087 } 088 089 /** 090 * Sets the default error handler used for all chains created by this factory, 091 * if they do not supply their own error handler. 092 * @param errorHandler The error handler 093 */ 094 public void setDefaultErrorHandler(BiConsumer<Exception, TaskChainTasks.Task<?, ?>> errorHandler) { 095 this.defaultErrorHandler = errorHandler; 096 } 097 098 /** 099 * Shuts down the TaskChain system, forcing all tasks to run on current threads and finish. 100 * @param duration How long in the supplied units to wait before giving up the shutdown. 101 * @param units The units for how long to wait before giving up the shutdown 102 */ 103 public void shutdown(int duration, TimeUnit units) { 104 shutdown = true; 105 asyncQueue.shutdown(duration, units); 106 } 107}