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

com.barchart.util.concurrent.FutureCallbackTask Maven / Gradle / Ivy

/**
 * Copyright (C) 2011-2012 Barchart, Inc. 
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */
package com.barchart.util.concurrent;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 

* Basic implementation of {@link FutureCallback} based on {@link FutureTask}. *

* * @author jeremy * @see FutureTask * @see FutureCallback * @param * The result type */ public class FutureCallbackTask extends FutureTask implements FutureCallback> { private final static Logger log = LoggerFactory .getLogger(FutureCallbackTask.class); private final List> listeners = new CopyOnWriteArrayList>(); private final Lock callbackLock = new ReentrantLock(); /** * Create a future result handler. */ public FutureCallbackTask(final Runnable r, final E value) { super(r, value); } /** * Create a future result handler. */ public FutureCallbackTask(final Callable c) { super(c); } @Override public FutureCallbackTask addResultListener( final FutureListener listener) { callbackLock.lock(); try { listeners.add(listener); if (isDone()) { try { listener.resultAvailable(this); } catch (final Exception ex) { log.warn("Unhandled exception in callback", ex); } } } finally { callbackLock.unlock(); } return this; } @Override public FutureCallbackTask succeed(final E result_) { super.set(result_); return this; } @Override public FutureCallbackTask fail(final Throwable error_) { super.setException(error_); return this; } @Override public E getUnchecked() { try { return get(); } catch (final Exception e) { return null; } } /** * Notify listeners that a result is available. */ @Override protected void done() { callbackLock.lock(); try { for (final FutureListener l : listeners) { try { l.resultAvailable(this); } catch (final Exception ex) { log.warn("Unhandled exception in callback", ex); } } } finally { callbackLock.unlock(); } } /** * Log an error if this FutureCallbackTask is garbage collected before it * has been called. */ @Override public void finalize() throws Throwable { try { if (!isDone()) { log.error("finalize() called on an incomplete FutureCallbackTask"); } } catch (final Exception e) { } finally { super.finalize(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy