Example usage for java.util.concurrent RejectedExecutionException getCause

List of usage examples for java.util.concurrent RejectedExecutionException getCause

Introduction

In this page you can find the example usage for java.util.concurrent RejectedExecutionException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:io.kahu.hawaii.util.call.dispatch.RequestDispatcher.java

/**
 * Blocking (synchronous) execute of the request.
 *
 * @param request/*from   w  w  w  .  j  a v a  2  s  . co  m*/
 * @param <T>
 * @return
 * @throws ServerException
 */
public <T> Response<T> execute(AbortableRequest<T> request) throws ServerException {
    Response<T> response = request.getResponse();

    try {
        HawaiiExecutor executor = executorServiceRepository.getExecutor(request);

        notifyListeners(request, executor);

        FutureTask<T> task = executor.execute(request, response);

        /*
         * Block until data is retrieved, but with a time out.
         */
        TimeOut timeOut = request.getTimeOut();
        task.get(timeOut.getDuration(), timeOut.getUnit());

    } catch (RejectedExecutionException e) {
        // Executor is too busy (no threads available nor is there a place in the queue).
        request.reject();
    } catch (TimeoutException e) {
        // The task.get( ... ) is timed out. The execution takes too long.
        request.abort();
    } catch (InterruptedException e) {
        // ..
        response.setStatus(ResponseStatus.INTERNAL_FAILURE, "Interrupted", e);
    } catch (ExecutionException e) {
        // Catches all exceptions from within the executor
        response.setStatus(ResponseStatus.INTERNAL_FAILURE, "Execution exception", e.getCause());
    } catch (Throwable t) {
        // Catches all exceptions outside the executor
        response.setStatus(ResponseStatus.INTERNAL_FAILURE, "Unexpected exception", t);
    } finally {
        request.finish();
    }

    return response;
}

From source file:org.apache.hadoop.hbase.procedure.ProcedureMember.java

/**
 * Submit an subprocedure for execution.  This starts the local acquire phase.
 * @param subproc the subprocedure to execute.
 * @return <tt>true</tt> if the subprocedure was started correctly, <tt>false</tt> if it
 *         could not be started. In the latter case, the subprocedure holds a reference to
 *         the exception that caused the failure.
 */// w  w  w.j  a v a 2 s . c o  m
public boolean submitSubprocedure(Subprocedure subproc) {
    // if the submitted subprocedure was null, bail.
    if (subproc == null) {
        LOG.warn("Submitted null subprocedure, nothing to run here.");
        return false;
    }

    String procName = subproc.getName();
    if (procName == null || procName.length() == 0) {
        LOG.error("Subproc name cannot be null or the empty string");
        return false;
    }

    // make sure we aren't already running an subprocedure of that name
    Subprocedure rsub;
    synchronized (subprocs) {
        rsub = subprocs.get(procName);
    }
    if (rsub != null) {
        if (!rsub.isComplete()) {
            LOG.error("Subproc '" + procName + "' is already running. Bailing out");
            return false;
        }
        LOG.warn("A completed old subproc " + procName + " is still present, removing");
        subprocs.remove(procName);
    }

    LOG.debug("Submitting new Subprocedure:" + procName);

    // kick off the subprocedure
    Future<Void> future = null;
    try {
        synchronized (subprocs) {
            subprocs.put(procName, subproc);
        }
        future = this.pool.submit(subproc);
        return true;
    } catch (RejectedExecutionException e) {
        synchronized (subprocs) {
            subprocs.remove(procName);
        }
        // the thread pool is full and we can't run the subprocedure
        String msg = "Subprocedure pool is full!";
        subproc.cancel(msg, e.getCause());

        // cancel all subprocedures proactively
        if (future != null) {
            future.cancel(true);
        }
    }

    LOG.error("Failed to start subprocedure '" + procName + "'");
    return false;
}

From source file:org.codice.ddf.catalog.sourcepoller.Poller.java

private Commitable load(final K key, final Callable<V> loader, final long timeout,
        final TimeUnit timeoutTimeUnit) {
    final Future<V> loaderFuture;
    try {//w  w  w. ja v a  2s.  c  o  m
        loaderFuture = pollThreadPool.submit(loader);
    } catch (final RejectedExecutionException e) {
        // the loader {@link Callable} could not be scheduled for execution
        return () -> {
            throw e;
        };
    }

    try {
        final V newValue = loaderFuture.get(timeout, timeoutTimeUnit);
        if (newValue == null) {
            return () -> {
                throw new IllegalArgumentException(
                        "Poller values may not be null, but the loaded value for " + key + " was null.");
            };
        }

        return () -> cacheNewValue(key, newValue);
    } catch (TimeoutException e) {
        LOGGER.debug("The loader for {} did not complete within {} {}. Cancelling the loader task", key,
                timeout, timeoutTimeUnit);
        loaderFuture.cancel(true);
        return () -> handleTimeout(key);
    } catch (ExecutionException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            final RuntimeException runtimeException = (RuntimeException) cause;
            return () -> handleException(key, runtimeException);
        } else {
            return () -> {
                throw cause;
            };
        }
    } catch (CancellationException e) {
        return () -> {
            throw e;
        };
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return () -> {
            throw e;
        };
    }
}

From source file:org.nuxeo.ecm.core.event.impl.PostCommitEventExecutor.java

public void run(List<EventListenerDescriptor> listeners, EventBundle bundle, long timeoutMillis, boolean bulk) {
    // check that there's at list one listener interested
    boolean some = false;
    for (EventListenerDescriptor listener : listeners) {
        if (listener.acceptBundle(bundle)) {
            some = true;/*from   w ww  .j av a2  s.c  o m*/
            break;
        }
    }
    if (!some) {
        if (log.isDebugEnabled()) {
            log.debug("Events postcommit execution has nothing to do");
        }
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug(String.format("Events postcommit execution starting with timeout %sms%s",
                Long.valueOf(timeoutMillis), bulk ? " in bulk mode" : ""));
    }

    Callable<Boolean> callable = !bulk ? new EventBundleRunner(listeners, bundle)
            : new EventBundleBulkRunner(listeners, bundle);
    FutureTask<Boolean> futureTask = new FutureTask<Boolean>(callable);
    try {
        executor.execute(futureTask);
    } catch (RejectedExecutionException e) {
        log.error("Events postcommit execution rejected", e);
        return;
    }
    try {
        // wait for runner to be finished, with timeout
        Boolean ok = futureTask.get(timeoutMillis, TimeUnit.MILLISECONDS);
        if (Boolean.FALSE.equals(ok)) {
            log.error("Events postcommit bulk execution aborted due to previous error");
        }
    } catch (InterruptedException e) {
        // restore interrupted status
        Thread.currentThread().interrupt();
        // interrupt thread
        futureTask.cancel(true); // mayInterruptIfRunning=true
    } catch (TimeoutException e) {
        if (!bulk) {
            log.warn(String.format(
                    "Events postcommit execution exceeded timeout of %sms, leaving thread running",
                    Long.valueOf(timeoutMillis)));
            // don't cancel task, let it run
        } else {
            log.error(String.format(
                    "Events postcommit bulk execution exceeded timeout of %sms, interrupting thread",
                    Long.valueOf(timeoutMillis)));
            futureTask.cancel(true); // mayInterruptIfRunning=true
        }
    } catch (ExecutionException e) {
        log.error("Events postcommit execution encountered unexpected exception", e.getCause());
    }

    if (log.isDebugEnabled()) {
        log.debug("Events postcommit execution finished");
    }
}