io.netty.channel.EventLoop.schedule()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(238)

本文整理了Java中io.netty.channel.EventLoop.schedule()方法的一些代码示例,展示了EventLoop.schedule()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EventLoop.schedule()方法的具体详情如下:
包路径:io.netty.channel.EventLoop
类名称:EventLoop
方法名:schedule

EventLoop.schedule介绍

暂无

代码示例

代码示例来源:origin: normanmaurer/netty-in-action

/**
 * Listing 7.3 Scheduling a task with EventLoop
 * */
public static void scheduleViaEventLoop() {
  Channel ch = CHANNEL_FROM_SOMEWHERE; // get reference from somewhere
  ScheduledFuture<?> future = ch.eventLoop().schedule(
    new Runnable() {
    @Override
    public void run() {
      System.out.println("60 seconds later");
    }
  }, 60, TimeUnit.SECONDS);
}

代码示例来源:origin: eclipse-vertx/vert.x

InternalTimerHandler(long timerID, Handler<Long> runnable, boolean periodic, long delay, ContextImpl context) {
 this.context = context;
 this.timerID = timerID;
 this.handler = runnable;
 this.periodic = periodic;
 this.cancelled = new AtomicBoolean();
 EventLoop el = context.nettyEventLoop();
 Runnable toRun = () -> context.runOnContext(this);
 if (periodic) {
  future = el.scheduleAtFixedRate(toRun, delay, delay, TimeUnit.MILLISECONDS);
 } else {
  future = el.schedule(toRun, delay, TimeUnit.MILLISECONDS);
 }
}

代码示例来源:origin: line/armeria

void scheduleTimeout(EventLoop eventLoop) {
  if (responseTimeoutFuture != null || responseTimeoutMillis <= 0 || !isOpen()) {
    // No need to schedule a response timeout if:
    // - the timeout has been scheduled already,
    // - the timeout has been disabled or
    // - the response stream has been closed already.
    return;
  }
  responseTimeoutFuture = eventLoop.schedule(
      this, responseTimeoutMillis, TimeUnit.MILLISECONDS);
}

代码示例来源:origin: ReactiveX/RxNetty

@Override
public ScheduledFuture<?> schedule(Runnable command, long delay,
                  TimeUnit unit) {
  return next().schedule(command, delay, unit);
}

代码示例来源:origin: ReactiveX/RxNetty

@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                    long delay, TimeUnit unit) {
  return next().schedule(callable, delay, unit);
}

代码示例来源:origin: netty/netty

@Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    final ChannelConfig config = ctx.channel().config();
    if (config.isAutoRead()) {
      // stop accept new connections for 1 second to allow the channel to recover
      // See https://github.com/netty/netty/issues/1328
      config.setAutoRead(false);
      ctx.channel().eventLoop().schedule(enableAutoReadTask, 1, TimeUnit.SECONDS);
    }
    // still let the exceptionCaught event flow through the pipeline to give the user
    // a chance to do something with it
    ctx.fireExceptionCaught(cause);
  }
}

代码示例来源:origin: redisson/redisson

private void onQueryWriteCompletion(ChannelFuture writeFuture) {
  if (!writeFuture.isSuccess()) {
    setFailure("failed to send a query", writeFuture.cause());
    return;
  }
  // Schedule a query timeout task if necessary.
  final long queryTimeoutMillis = parent.queryTimeoutMillis();
  if (queryTimeoutMillis > 0) {
    timeoutFuture = parent.ch.eventLoop().schedule(new Runnable() {
      @Override
      public void run() {
        if (promise.isDone()) {
          // Received a response before the query times out.
          return;
        }
        setFailure("query timed out after " + queryTimeoutMillis + " milliseconds", null);
      }
    }, queryTimeoutMillis, TimeUnit.MILLISECONDS);
  }
}

代码示例来源:origin: redisson/redisson

@Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    final ChannelConfig config = ctx.channel().config();
    if (config.isAutoRead()) {
      // stop accept new connections for 1 second to allow the channel to recover
      // See https://github.com/netty/netty/issues/1328
      config.setAutoRead(false);
      ctx.channel().eventLoop().schedule(enableAutoReadTask, 1, TimeUnit.SECONDS);
    }
    // still let the exceptionCaught event flow through the pipeline to give the user
    // a chance to do something with it
    ctx.fireExceptionCaught(cause);
  }
}

代码示例来源:origin: line/armeria

@Override
public void onRequestTimeoutChange(long newRequestTimeoutMillis) {
  // Cancel the previously scheduled timeout, if exists.
  cancelTimeout();
  if (newRequestTimeoutMillis > 0 && state != State.DONE) {
    // Calculate the amount of time passed since the creation of this subscriber.
    final long passedTimeMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTimeNanos);
    if (passedTimeMillis < newRequestTimeoutMillis) {
      timeoutFuture = ctx.channel().eventLoop().schedule(
          this::onTimeout,
          newRequestTimeoutMillis - passedTimeMillis, TimeUnit.MILLISECONDS);
    } else {
      // We went past the dead line set by the new timeout already.
      onTimeout();
    }
  }
}

代码示例来源:origin: line/armeria

private void initSession(SessionProtocol desiredProtocol, ChannelFuture connectFuture,
             Promise<Channel> sessionPromise) {
  assert connectFuture.isSuccess();
  final Channel ch = connectFuture.channel();
  final EventLoop eventLoop = ch.eventLoop();
  assert eventLoop.inEventLoop();
  final ScheduledFuture<?> timeoutFuture = eventLoop.schedule(() -> {
    if (sessionPromise.tryFailure(new SessionProtocolNegotiationException(
        desiredProtocol, "connection established, but session creation timed out: " + ch))) {
      ch.close();
    }
  }, connectTimeoutMillis, TimeUnit.MILLISECONDS);
  ch.pipeline().addLast(new HttpSessionHandler(this, ch, sessionPromise, timeoutFuture));
}

代码示例来源:origin: wildfly/wildfly

@Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    final ChannelConfig config = ctx.channel().config();
    if (config.isAutoRead()) {
      // stop accept new connections for 1 second to allow the channel to recover
      // See https://github.com/netty/netty/issues/1328
      config.setAutoRead(false);
      ctx.channel().eventLoop().schedule(enableAutoReadTask, 1, TimeUnit.SECONDS);
    }
    // still let the exceptionCaught event flow through the pipeline to give the user
    // a chance to do something with it
    ctx.fireExceptionCaught(cause);
  }
}

代码示例来源:origin: wildfly/wildfly

private void onQueryWriteCompletion(ChannelFuture writeFuture) {
  if (!writeFuture.isSuccess()) {
    setFailure("failed to send a query", writeFuture.cause());
    return;
  }
  // Schedule a query timeout task if necessary.
  final long queryTimeoutMillis = parent.queryTimeoutMillis();
  if (queryTimeoutMillis > 0) {
    timeoutFuture = parent.ch.eventLoop().schedule(new Runnable() {
      @Override
      public void run() {
        if (promise.isDone()) {
          // Received a response before the query times out.
          return;
        }
        setFailure("query timed out after " + queryTimeoutMillis + " milliseconds", null);
      }
    }, queryTimeoutMillis, TimeUnit.MILLISECONDS);
  }
}

代码示例来源:origin: line/armeria

@Override
public void onSubscribe(Subscription subscription) {
  assert this.subscription == null;
  this.subscription = subscription;
  final EventLoop eventLoop = ch.eventLoop();
  if (timeoutMillis > 0) {
    // The timer would be executed if the first message has not been sent out within the timeout.
    timeoutFuture = eventLoop.schedule(
        () -> failAndRespond(WriteTimeoutException.get()),
        timeoutMillis, TimeUnit.MILLISECONDS);
  }
  // NB: This must be invoked at the end of this method because otherwise the callback methods in this
  //     class can be called before the member fields (subscription and timeoutFuture) are initialized.
  //     It is because the successful write of the first headers will trigger subscription.request(1).
  writeFirstHeader();
}

代码示例来源:origin: line/armeria

@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
  return delegate().schedule(context().makeContextAware(command), delay, unit);
}

代码示例来源:origin: line/armeria

@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
  return delegate().schedule(context().makeContextAware(callable), delay, unit);
}

代码示例来源:origin: line/armeria

private O limitedExecute(ClientRequestContext ctx, I req) throws Exception {
  final Deferred<O> deferred = defer(ctx, req);
  final PendingTask currentTask = new PendingTask(ctx, req, deferred);
  pendingRequests.add(currentTask);
  drain();
  if (!currentTask.isRun() && timeoutMillis != 0) {
    // Current request was not delegated. Schedule a timeout.
    final ScheduledFuture<?> timeoutFuture = ctx.eventLoop().schedule(
        () -> deferred.close(ResponseTimeoutException.get()),
        timeoutMillis, TimeUnit.MILLISECONDS);
    currentTask.set(timeoutFuture);
  }
  return deferred.response();
}

代码示例来源:origin: netty/netty

eventLoop().schedule(new Runnable() {
  @Override
  public void run() {

代码示例来源:origin: line/armeria

@Override
  protected void configure(ServerBuilder sb) throws Exception {
    sb.gracefulShutdownTimeout(1000L, 2000L);
    sb.defaultRequestTimeoutMillis(0); // Disable RequestTimeoutException.
    sb.service("/sleep", THttpService.of(
        (AsyncIface) (milliseconds, resultHandler) ->
            RequestContext.current().eventLoop().schedule(
                () -> resultHandler.onComplete(milliseconds), milliseconds, MILLISECONDS)));
  }
};

代码示例来源:origin: redisson/redisson

connectTimeoutFuture = eventLoop().schedule(new Runnable() {
  @Override
  public void run() {

代码示例来源:origin: redisson/redisson

connectTimeoutFuture = eventLoop().schedule(new Runnable() {
  @Override
  public void run() {

相关文章