Java 类com.google.common.util.concurrent.TimeLimiter 实例源码

项目:fault-tolerance-talk    文件:AnyOperationTimeoutTest.java   
@Test
public void shouldUseCallWithTimeout() throws Exception {
    TimeLimiter timeLimiter = new SimpleTimeLimiter();
    long start = System.nanoTime();
    String result = timeLimiter.callWithTimeout(
            () -> doSomeHeavyWeightOperation(), ENOUGH_MS, MILLISECONDS, true);
    assertEquals("done", result);
    TimeOutAssertion.assertTheCallTookBetween(start, DELAY_MS, ENOUGH_MS);
}
项目:benayn    文件:Retryer.java   
/**
 * Timing out after the specified time limit with {@link TimeLimiter}
 *
 * @param timeLimiter
 * @param duration
 * @param timeUnit
 * @return
 */
public Retryer<R> timeout(final TimeLimiter timeLimiter, final long duration, final TimeUnit timeUnit) {
    return withTimelimiter(new AttemptTimelimit<R>() {

        @Override public R call(Callable<R> callable) throws Exception {
            return checkNotNull(timeLimiter, "TimeLimiter cannot be null")
                    .callWithTimeout(callable, duration, checkNotNull(timeUnit), true);
        }
    });
}
项目:fault-tolerance-talk    文件:AnyOperationTimeoutTest.java   
@Test
public void shouldUseProxy() throws Exception {
    TimeLimiter timeLimiter = new SimpleTimeLimiter();
    long start = System.nanoTime();
    HeavyOperation target = () -> doSomeHeavyWeightOperation();
    HeavyOperation proxy = timeLimiter.newProxy(target, HeavyOperation.class, ENOUGH_MS, MILLISECONDS);
    assertEquals("done", proxy.doHeavyWeightOperation());
    TimeOutAssertion.assertTheCallTookBetween(start, DELAY_MS, ENOUGH_MS);
}
项目:java-extended    文件:Try.java   
public static <T> T tryWith(long timeoutDuration, TimeUnit timeoutUnit, Callable<T> callable) {
    try {
        TimeLimiter limiter = new SimpleTimeLimiter();
        //noinspection unchecked
        Callable<T> proxy = limiter.newProxy(callable, Callable.class, timeoutDuration, timeoutUnit);
        return proxy.call();
    } catch (Exception e) {
        throw uncheckedException().apply(e);
    }
}
项目:java-extended    文件:Try.java   
public static void tryWith(long timeoutDuration, TimeUnit timeoutUnit, Effect effect) {
    try {
        TimeLimiter limiter = new SimpleTimeLimiter();
        //noinspection unchecked
        Effect proxy = limiter.newProxy(effect, Effect.class, timeoutDuration, timeoutUnit);
        proxy.cause();
    } catch (Exception e) {
        throw uncheckedException().apply(e);
    }
}
项目:nomulus    文件:AppEngineTimeLimiter.java   
public static TimeLimiter create() {
  return SimpleTimeLimiter.create(new NewRequestThreadExecutorService());
}
项目:presto    文件:TimeoutBackupStore.java   
private static <T> T timeLimited(T target, Class<T> clazz, Duration timeout, ExecutorService executor)
{
    TimeLimiter limiter = new SimpleTimeLimiter(executor);
    return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS);
}
项目:AsyncUtils    文件:DecoratedCallableFunction.java   
DecoratedCallableFunction(TimeLimiter limiter) {
  this.limiter = limiter;
}
项目:AsyncUtils    文件:DecoratedCallableBuilder.java   
@VisibleForTesting
DecoratedCallableBuilder(TimeLimiter limiter) {
  this.limiter = limiter;
}