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

项目:nomulus    文件:UrlChecker.java   
/** Probes {@code url} until it becomes available. */
static void waitUntilAvailable(final URL url, int timeoutMs) {
  try {
    Void unusedReturnValue = SimpleTimeLimiter.create(newCachedThreadPool())
        .callWithTimeout(
            () -> {
              int exponentialBackoffMs = 1;
              while (true) {
                if (isAvailable(url)) {
                  return null;
                }
                Thread.sleep(exponentialBackoffMs *= 2);
              }
            },
            timeoutMs,
            TimeUnit.MILLISECONDS);
  } catch (Exception e) {
    throwIfUnchecked(e);
    throw new RuntimeException(e);
  }
}
项目:nomulus    文件:TestServer.java   
/** Stops the HTTP server. */
public void stop() {
  try {
    Void unusedReturnValue = SimpleTimeLimiter.create(newCachedThreadPool())
        .callWithTimeout(
            () -> {
              server.stop();
              return null;
            },
            SHUTDOWN_TIMEOUT_MS,
            TimeUnit.MILLISECONDS);
  } catch (Exception e) {
    throwIfUnchecked(e);
    throw new RuntimeException(e);
  }
}
项目:graylog-plugin-dnsresolver    文件:DnsResolverFilter.java   
@Inject
public DnsResolverFilter(@Named("dns_resolver_timeout") Period resolverTimeout,
                         @Named("dns_resolver_run_before_extractors") boolean shouldRunBeforeExtractors,
                         @Named("dns_resolver_enabled") boolean enabled,
                         MetricRegistry metricRegistry) {
    this.shouldRunBeforeExtractors = shouldRunBeforeExtractors;
    this.enabled = enabled;
    timeout = resolverTimeout.toStandardDuration().getMillis();
    timeLimiter = SimpleTimeLimiter.create(
            Executors.newSingleThreadExecutor(
                    new ThreadFactoryBuilder()
                            .setDaemon(true)
                            .setNameFormat("dns-resolver-thread-%d")
                            .build()
            )
    );
    this.resolveTime = metricRegistry.timer(name(DnsResolverFilter.class, "resolveTime"));
    this.resolveTimeouts = metricRegistry.meter(name(DnsResolverFilter.class, "resolveTimeouts"));
}
项目: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);
}
项目:UnknownPandaServer    文件:UpsConsoleThread.java   
public UpsConsoleThread(UpsConsole nanoConsole) {
    super.setName("UnknownPandaServer|Console Thread");
    super.setDaemon(true);

    this.console = nanoConsole;
    this.limiter = new SimpleTimeLimiter();
}
项目: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);
}
项目:SWEN-Fuzzer    文件:VVector.java   
public boolean testWithTimeout(int timeout) {
    SimpleTimeLimiter limiter = new SimpleTimeLimiter();
    boolean result = false;
    try {
        limiter.callWithTimeout(new Callable<Boolean>() {
            public Boolean call() {
                return test();
            }
        }, timeout, TimeUnit.MILLISECONDS, false);
    } catch (Exception e) {
        result = true;
        loadDescription("Timed out with " + Integer.toString(timeout) + " ms.");
    }
    return result;
}
项目: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);
}
项目:fault-tolerance-talk    文件:AnyOperationTimeoutTest.java   
@Test(expected = UncheckedTimeoutException.class)
public void shouldTriggerTimeout() throws Exception {
    new SimpleTimeLimiter().callWithTimeout(
            () -> doSomeHeavyWeightOperation(), NOT_ENOUGH_MS, MILLISECONDS, true);
}
项目:AsyncUtils    文件:DecoratedCallableBuilder.java   
DecoratedCallableBuilder() {
  this(new SimpleTimeLimiter());
}
项目:benayn    文件:Retryer.java   
/**
 * Timing out after the specified time limit with {@link ExecutorService}
 *
 * @param duration
 * @param timeUnit
 * @param executor
 * @return
 */
public Retryer<R> timeout(long duration, TimeUnit timeUnit, ExecutorService executor) {
    return timeout(null == executor
            ? new SimpleTimeLimiter() : new SimpleTimeLimiter(executor), duration, timeUnit);
}