Example usage for com.google.common.util.concurrent Futures withTimeout

List of usage examples for com.google.common.util.concurrent Futures withTimeout

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures withTimeout.

Prototype

@GwtIncompatible("java.util.concurrent.ScheduledExecutorService")
@CheckReturnValue
public static <V> ListenableFuture<V> withTimeout(ListenableFuture<V> delegate, long time, TimeUnit unit,
        ScheduledExecutorService scheduledExecutor) 

Source Link

Document

Returns a future that delegates to another but will finish early (via a TimeoutException wrapped in an ExecutionException ) if the specified duration expires.

Usage

From source file:org.thingsboard.server.kafka.AsyncCallbackTemplate.java

public static <T> void withCallbackAndTimeout(ListenableFuture<T> future, Consumer<T> onSuccess,
        Consumer<Throwable> onFailure, long timeoutInMs, ScheduledExecutorService timeoutExecutor,
        Executor callbackExecutor) {
    future = Futures.withTimeout(future, timeoutInMs, TimeUnit.MILLISECONDS, timeoutExecutor);
    withCallback(future, onSuccess, onFailure, callbackExecutor);
}

From source file:org.thingsboard.server.dao.util.AbstractBufferedRateExecutor.java

private void dispatch() {
    log.info("Buffered rate executor thread started");
    while (!Thread.interrupted()) {
        int curLvl = concurrencyLevel.get();
        AsyncTaskContext<T, V> taskCtx = null;
        try {/*from   w w w . j a v  a  2 s  .  c  om*/
            if (curLvl <= concurrencyLimit) {
                taskCtx = queue.take();
                final AsyncTaskContext<T, V> finalTaskCtx = taskCtx;
                logTask("Processing", finalTaskCtx);
                concurrencyLevel.incrementAndGet();
                long timeout = finalTaskCtx.getCreateTime() + maxWaitTime - System.currentTimeMillis();
                if (timeout > 0) {
                    totalLaunched.incrementAndGet();
                    ListenableFuture<V> result = execute(finalTaskCtx);
                    result = Futures.withTimeout(result, timeout, TimeUnit.MILLISECONDS, timeoutExecutor);
                    Futures.addCallback(result, new FutureCallback<V>() {
                        @Override
                        public void onSuccess(@Nullable V result) {
                            logTask("Releasing", finalTaskCtx);
                            totalReleased.incrementAndGet();
                            concurrencyLevel.decrementAndGet();
                            finalTaskCtx.getFuture().set(result);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            if (t instanceof TimeoutException) {
                                logTask("Expired During Execution", finalTaskCtx);
                            } else {
                                logTask("Failed", finalTaskCtx);
                            }
                            totalFailed.incrementAndGet();
                            concurrencyLevel.decrementAndGet();
                            finalTaskCtx.getFuture().setException(t);
                            log.debug("[{}] Failed to execute task: {}", finalTaskCtx.getId(),
                                    finalTaskCtx.getTask(), t);
                        }
                    }, callbackExecutor);
                } else {
                    logTask("Expired Before Execution", finalTaskCtx);
                    totalExpired.incrementAndGet();
                    concurrencyLevel.decrementAndGet();
                    taskCtx.getFuture().setException(new TimeoutException());
                }
            } else {
                Thread.sleep(pollMs);
            }
        } catch (InterruptedException e) {
            break;
        } catch (Throwable e) {
            if (taskCtx != null) {
                log.debug("[{}] Failed to execute task: {}", taskCtx.getId(), taskCtx, e);
                totalFailed.incrementAndGet();
                concurrencyLevel.decrementAndGet();
            } else {
                log.debug("Failed to queue task:", e);
            }
        }
    }
    log.info("Buffered rate executor thread stopped");
}