Java Code Examples for io.netty.util.HashedWheelTimer#newTimeout()

The following examples show how to use io.netty.util.HashedWheelTimer#newTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompletableFutures.java    From Singularity with Apache License 2.0 6 votes vote down vote up
public static <T> CompletableFuture<T> enforceTimeout(
  CompletionStage<T> underlyingFuture,
  HashedWheelTimer timer,
  long timeout,
  TimeUnit timeUnit,
  Supplier<Exception> exceptionSupplier
) {
  // We don't want to muck with the underlying future passed in, so
  // chaining a .thenApply(x -> x) forces a new future to be created with its own
  // completion tracking. In this way, the original future is left alone and can
  // time out on its own schedule.
  CompletableFuture<T> future = underlyingFuture
    .thenApply(x -> x)
    .toCompletableFuture();
  Timeout hwtTimeout = timer.newTimeout(
    ignored -> future.completeExceptionally(exceptionSupplier.get()),
    timeout,
    timeUnit
  );
  future.whenComplete((result, throwable) -> hwtTimeout.cancel());
  return future;
}
 
Example 2
Source File: HashedWheelTimerTest.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
	HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(1000, TimeUnit.MILLISECONDS, 16);
	hashedWheelTimer.newTimeout(new TimerTask() {

		@Override
		public void run(Timeout timeout) throws Exception {
			System.out.println(System.currentTimeMillis() + "  === executed");
		}
	}, 1, TimeUnit.SECONDS);
}
 
Example 3
Source File: CompletableFutures.java    From Singularity with Apache License 2.0 5 votes vote down vote up
/**
 * Return a future that completes with a timeout after a delay.
 */
public static CompletableFuture<Timeout> timeoutFuture(
  HashedWheelTimer hwt,
  long delay,
  TimeUnit timeUnit
) {
  try {
    CompletableFuture<Timeout> future = new CompletableFuture<>();
    hwt.newTimeout(future::complete, delay, timeUnit);
    return future;
  } catch (Throwable t) {
    return exceptionalFuture(t);
  }
}
 
Example 4
Source File: CarreraRequest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public void registerTimeout(HashedWheelTimer timeoutChecker, long timeout) {
    if (timeout > 0) {
        timeoutHandle = timeoutChecker.newTimeout(this::checkTimeout, timeout, TimeUnit.MILLISECONDS);
    }
}
 
Example 5
Source File: CarreraRequest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public void registerTimeout(HashedWheelTimer timeoutChecker, long timeout) {
    if (timeout > 0) {
        timeoutHandle = timeoutChecker.newTimeout(this::checkTimeout, timeout, TimeUnit.MILLISECONDS);
    }
}