Example usage for java.util.concurrent RejectedExecutionException RejectedExecutionException

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

Introduction

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

Prototype

public RejectedExecutionException(Throwable cause) 

Source Link

Document

Constructs a RejectedExecutionException with the specified cause.

Usage

From source file:amqp.spring.camel.component.SpringAMQPProducer.java

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    if (!isRunAllowed()) {
        if (exchange.getException() == null)
            exchange.setException(new RejectedExecutionException("SpringAMQPProducer not started yet!"));
        callback.done(true);// w w w  .j  a  va 2 s.c o m
        return true;
    }

    if (this.threadPool == null) {
        if (exchange.getException() == null)
            exchange.setException(new RejectedExecutionException("SpringAMQPProducer is not yet initialized!"));
        callback.done(true);
        return true;
    }

    this.threadPool.submit(new AMQPProducerTask(exchange, callback));
    return false;
}

From source file:org.springframework.integration.util.CallerBlocksPolicy.java

@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    if (!executor.isShutdown()) {
        try {//w  w w.j  av a2 s.com
            BlockingQueue<Runnable> queue = executor.getQueue();
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to queue task execution for " + this.maxWait + " milliseconds");
            }
            if (!queue.offer(r, this.maxWait, TimeUnit.MILLISECONDS)) {
                throw new RejectedExecutionException("Max wait time expired to queue task");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Task execution queued");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RejectedExecutionException("Interrupted", e);
        }
    } else {
        throw new RejectedExecutionException("Executor has been shut down");
    }
}

From source file:amqp.spring.camel.component.SpringAMQPProducer.java

@Override
public void process(Exchange exchange) throws Exception {
    if (!isRunAllowed() && exchange.getException() == null) {
        exchange.setException(new RejectedExecutionException("SpringAMQPProducer not started yet!"));
    }//from  w ww  .j a v  a2s  .com

    //This is an intentional synchronous invocation of run(), don't mock me
    new AMQPProducerTask(exchange).run();
}

From source file:com.metamx.emitter.core.LoggingEmitter.java

@Override
public void emit(Event event) {
    synchronized (started) {
        if (!started.get()) {
            throw new RejectedExecutionException("Service not started.");
        }/* w w w . j a  v  a2s .  co  m*/
    }
    try {
        final String message = "Event [%s]";
        switch (level) {
        case TRACE:
            if (log.isTraceEnabled()) {
                log.trace(message, jsonMapper.writeValueAsString(event));
            }
            break;
        case DEBUG:
            if (log.isDebugEnabled()) {
                log.debug(message, jsonMapper.writeValueAsString(event));
            }
            break;
        case INFO:
            if (log.isInfoEnabled()) {
                log.info(message, jsonMapper.writeValueAsString(event));
            }
            break;
        case WARN:
            log.warn(message, jsonMapper.writeValueAsString(event));
            break;
        case ERROR:
            log.error(message, jsonMapper.writeValueAsString(event));
            break;
        }
    } catch (Exception e) {
        log.warn("Failed to generate json", e);
    }
}

From source file:org.apache.camel.processor.DelayProcessorSupport.java

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    if (!isRunAllowed()) {
        exchange.setException(new RejectedExecutionException("Run is not allowed"));
        callback.done(true);/*from www  . j a  v a2 s . c  om*/
        return true;
    }

    // calculate delay and wait
    long delay = calculateDelay(exchange);
    if (delay <= 0) {
        // no delay then continue routing
        return super.process(exchange, callback);
    }

    if (!isAsyncDelayed() || exchange.isTransacted()) {
        // use synchronous delay (also required if using transactions)
        try {
            delay(delay, exchange);
            // then continue routing
            return super.process(exchange, callback);
        } catch (Exception e) {
            // exception occurred so we are done
            exchange.setException(e);
            callback.done(true);
            return true;
        }
    } else {
        // asynchronous delay so schedule a process call task
        ProcessCall call = new ProcessCall(exchange, callback);
        try {
            if (log.isTraceEnabled()) {
                log.trace("Scheduling delayed task to run in " + delay + " millis for exchangeId: "
                        + exchange.getExchangeId());
            }
            executorService.schedule(call, delay, TimeUnit.MILLISECONDS);
            // tell Camel routing engine we continue routing asynchronous
            return false;
        } catch (RejectedExecutionException e) {
            if (isCallerRunsWhenRejected()) {
                if (!isRunAllowed()) {
                    exchange.setException(new RejectedExecutionException());
                } else {
                    // let caller run by processing
                    try {
                        delay(delay, exchange);
                    } catch (InterruptedException ie) {
                        exchange.setException(ie);
                    }
                    // then continue routing
                    return super.process(exchange, callback);
                }
            } else {
                exchange.setException(e);
            }
            // caller don't run the task so we are done
            callback.done(true);
            return true;
        }
    }
}

From source file:oz.hadoop.yarn.api.core.DataProcessorImpl.java

/**
 * /* ww w .  j a  v  a2s.com*/
 */
@Override
public void process(ByteBuffer data, String ipRegexFilter) {
    if (this.active) {
        final int index = this.getIndexOfAvailableDelegate(ipRegexFilter);
        if (index >= 0) {
            final ContainerDelegate delegate = this.containerDelegates[index];
            if (logger.isDebugEnabled()) {
                logger.debug("Selected ContainerDelegate for process invocation: " + delegate);
            }

            ReplyPostProcessor replyPostProcessor = new ReplyPostProcessor() {
                @Override
                public void doProcess(ByteBuffer reply) {
                    /*
                     * This is release logic which will make ContainerDelegate available again.
                     */
                    if (!DataProcessorImpl.this.busyDelegatesFlags[index].compareAndSet(true, false)) {
                        logger.error(
                                "Failed to release 'busyDelegatesFlag'. Should never happen. Concurrency issue; if you see this message, REPORT!");
                        DataProcessorImpl.this.stop();
                        throw new IllegalStateException(
                                "Should never happen. Concurrency issue, if you see this message, REPORT!");
                    }
                }
            };
            if (logger.isDebugEnabled()) {
                logger.debug("Submitting data " + data + " to the Application Container");
            }
            delegate.process(data, replyPostProcessor);
            this.completedSinceStart.getAndIncrement();
        } else {
            logger.debug(
                    "Process awaiting available container delegate was discarded due to application termination.");
        }
    } else {
        logger.warn("Rejecting submission due to the shutdown. Completed processes: "
                + this.completedSinceStart.get());
        throw new RejectedExecutionException("Rejecting submission due to a termination");
    }
}

From source file:com.metamx.emitter.core.HttpPostEmitter.java

@Override
public void emit(Event event) {
    synchronized (started) {
        if (!started.get()) {
            throw new RejectedExecutionException("Service is closed.");
        }//from w  w w. j  a v a  2  s.  c  o m
    }

    final byte[] eventBytes;
    try {
        eventBytes = jsonMapper.writeValueAsBytes(event);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    if (eventBytes.length > MAX_EVENT_SIZE) {
        log.error("Event too large to emit (%,d > %,d): %s ...", eventBytes.length, MAX_EVENT_SIZE,
                new String(eventBytes, 0, 1024));
        return;
    }

    synchronized (eventsList) {
        if (bufferedSize.get() + eventBytes.length <= config.getMaxBufferSize()) {
            eventsList.get().add(eventBytes);
            bufferedSize.addAndGet(eventBytes.length);
            if (!event.isSafeToBuffer() || count.incrementAndGet() >= config.getFlushCount()) {
                exec.execute(new EmittingRunnable(version.get()));
            }
        } else {
            messagesDroppedSinceLastBufferFullWarning++;
        }

        final long now = System.currentTimeMillis();
        if (messagesDroppedSinceLastBufferFullWarning > 0
                && lastBufferFullWarning + BUFFER_FULL_WARNING_THROTTLE < now) {
            log.error("Buffer full: dropped %,d events!", messagesDroppedSinceLastBufferFullWarning);
            lastBufferFullWarning = now;
            messagesDroppedSinceLastBufferFullWarning = 0;
        }
    }
}

From source file:org.jcurl.core.ui.TaskExecutor.java

/**
 * Cast down to {@link Task} and delegate to
 * {@link #execute(org.jcurl.core.ui.TaskExecutor.Task)}.
 * /*  ww w  .ja  va  2  s.co m*/
 * @throws RejectedExecutionException
 *             if the downcast fails.
 */
@SuppressWarnings("unchecked")
public void execute(final Runnable command) {
    try {
        execute((Task<? extends Executor>) command);
    } catch (final ClassCastException e) {
        throw new RejectedExecutionException(e);
    }
}

From source file:org.apache.tomcat.util.threads.ThreadPoolExecutor.java

/**
 * Executes the given command at some time in the future.  The command
 * may execute in a new thread, in a pooled thread, or in the calling
 * thread, at the discretion of the <tt>Executor</tt> implementation.
 * If no threads are available, it will be added to the work queue.
 * If the work queue is full, the system will wait for the specified
 * time and it throw a RejectedExecutionException if the queue is still
 * full after that./*from   www .j a  v a  2  s. c om*/
 *
 * @param command the runnable task
 * @throws RejectedExecutionException if this task cannot be
 * accepted for execution - the queue is full
 * @throws NullPointerException if command or unit is null
 */
public void execute(Runnable command, long timeout, TimeUnit unit) {
    submittedCount.incrementAndGet();
    try {
        super.execute(command);
    } catch (RejectedExecutionException rx) {
        if (super.getQueue() instanceof TaskQueue) {
            final TaskQueue queue = (TaskQueue) super.getQueue();
            try {
                if (!queue.force(command, timeout, unit)) {
                    submittedCount.decrementAndGet();
                    throw new RejectedExecutionException("Queue capacity is full.");
                }
            } catch (InterruptedException x) {
                submittedCount.decrementAndGet();
                Thread.interrupted();
                throw new RejectedExecutionException(x);
            }
        } else {
            submittedCount.decrementAndGet();
            throw rx;
        }

    }
}

From source file:org.jcurl.core.ui.TaskExecutor.java

/**
 * Delegate to the {@link Executor#execute(Runnable)} of the type parameter.
 * /*  w  ww  .  ja v  a2 s.c  om*/
 * Keeps a {@link Map} class-&gt;instance.
 * 
 * @param msg
 * @param et
 *            executor to delegate to.
 * @throws RejectedExecutionException
 *             {@link Class#newInstance()} of the type parameter failed.
 */
public void execute(final Runnable msg, final Class<? extends Executor> et) {
    log.debug(et);
    Executor ex;
    try {
        synchronized (et) {
            ex = map.get(et);
            if (ex == null)
                map.put(et, ex = et.newInstance());
        }
    } catch (final InstantiationException e) {
        throw new RejectedExecutionException(e);
    } catch (final IllegalAccessException e) {
        throw new RejectedExecutionException(e);
    }
    ex.execute(msg);
}