/** * 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); }
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); } }
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); }
@Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { return next().schedule(command, delay, unit); }
@Override public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { return next().schedule(callable, delay, unit); }
@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); } }
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); } }
@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); } }
@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(); } } }
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)); }
@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); } }
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); } }
@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(); }
@Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { return delegate().schedule(context().makeContextAware(command), delay, unit); }
@Override public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { return delegate().schedule(context().makeContextAware(callable), delay, unit); }
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(); }
eventLoop().schedule(new Runnable() { @Override public void run() {
@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))); } };
connectTimeoutFuture = eventLoop().schedule(new Runnable() { @Override public void run() {
connectTimeoutFuture = eventLoop().schedule(new Runnable() { @Override public void run() {