Example usage for io.netty.util.concurrent ScheduledFuture cancel

List of usage examples for io.netty.util.concurrent ScheduledFuture cancel

Introduction

In this page you can find the example usage for io.netty.util.concurrent ScheduledFuture cancel.

Prototype

@Override
boolean cancel(boolean mayInterruptIfRunning);

Source Link

Document

If the cancellation was successful it will fail the future with a CancellationException .

Usage

From source file:com.cloudera.livy.rsc.driver.RSCDriver.java

License:Apache License

private void setupIdleTimeout() {
    if (clients.size() > 0) {
        return;/*from  ww  w  .j  a  v  a  2s .c  o m*/
    }

    Runnable timeoutTask = new Runnable() {
        @Override
        public void run() {
            LOG.warn("Shutting down RSC due to idle timeout ({}).", livyConf.get(SERVER_IDLE_TIMEOUT));
            shutdown();
        }
    };
    ScheduledFuture<?> timeout = server.getEventLoopGroup().schedule(timeoutTask,
            livyConf.getTimeAsMs(SERVER_IDLE_TIMEOUT), TimeUnit.MILLISECONDS);

    // If there's already an idle task registered, then cancel the new one.
    if (!this.idleTimeout.compareAndSet(null, timeout)) {
        LOG.debug("Timeout task already registered.");
        timeout.cancel(false);
    }

    // If a new client connected while the idle task was being set up, then stop the task.
    if (clients.size() > 0) {
        stopIdleTimeout();
    }
}

From source file:com.cloudera.livy.rsc.driver.RSCDriver.java

License:Apache License

private void stopIdleTimeout() {
    ScheduledFuture<?> idleTimeout = this.idleTimeout.getAndSet(null);
    if (idleTimeout != null) {
        LOG.debug("Cancelling idle timeout since new client connected.");
        idleTimeout.cancel(false);
    }/* w  ww. j  a v a  2  s.  c  o  m*/
}

From source file:com.lambdaworks.redis.cluster.RedisClusterClient.java

License:Apache License

/**
 * Shutdown this client and close all open connections. The client should be discarded after calling shutdown.
 *
 * @param quietPeriod the quiet period as described in the documentation
 * @param timeout the maximum amount of time to wait until the executor is shutdown regardless if a task was submitted
 *        during the quiet period/*from  w  w w  . j a  va 2  s .c  o m*/
 * @param timeUnit the unit of {@code quietPeriod} and {@code timeout}
 */
@Override
public void shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {

    if (clusterTopologyRefreshActivated.compareAndSet(true, false)) {

        ScheduledFuture<?> scheduledFuture = clusterTopologyRefreshFuture.get();

        try {
            scheduledFuture.cancel(false);
            clusterTopologyRefreshFuture.set(null);
        } catch (Exception e) {
            logger.debug("Could not unschedule Cluster topology refresh", e);
        }
    }

    super.shutdown(quietPeriod, timeout, timeUnit);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsEndpointGroup.java

License:Apache License

/**
 * Stops polling DNS servers for service updates.
 */// w  ww.ja va2  s.  co m
@Override
public final void close() {
    stopped = true;
    super.close();
    final ScheduledFuture<?> scheduledFuture = this.scheduledFuture;
    if (scheduledFuture != null) {
        scheduledFuture.cancel(true);
    }
}

From source file:io.pravega.client.netty.impl.ClientConnectionInboundHandler.java

License:Open Source License

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    super.channelRegistered(ctx);
    Channel c = ctx.channel();/*from   w w  w  . j a  v  a 2 s  . c om*/
    channel.set(c);
    c.write(new WireCommands.Hello(WireCommands.WIRE_VERSION, WireCommands.OLDEST_COMPATABLE_VERSION),
            c.voidPromise());
    ScheduledFuture<?> old = keepAliveFuture
            .getAndSet(c.eventLoop().scheduleWithFixedDelay(new KeepAliveTask(ctx), 20, 10, TimeUnit.SECONDS));
    if (old != null) {
        old.cancel(false);
    }
}

From source file:io.pravega.client.netty.impl.ClientConnectionInboundHandler.java

License:Open Source License

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    ScheduledFuture<?> future = keepAliveFuture.get();
    if (future != null) {
        future.cancel(false);
    }/* w ww .  java 2  s.  c o m*/
    channel.set(null);
    processor.connectionDropped();
    super.channelUnregistered(ctx);
}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

private void setSuccess(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
    parent.queryContextManager.remove(nameServerAddr(), id);

    // Cancel the timeout task.
    final ScheduledFuture<?> timeoutFuture = this.timeoutFuture;
    if (timeoutFuture != null) {
        timeoutFuture.cancel(false);
    }// w  ww.  j  av  a2 s . c o m

    Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise = this.promise;
    if (promise.setUncancellable()) {
        @SuppressWarnings("unchecked")
        AddressedEnvelope<DnsResponse, InetSocketAddress> castResponse = (AddressedEnvelope<DnsResponse, InetSocketAddress>) envelope
                .retain();
        promise.setSuccess(castResponse);
    }
}

From source file:nats.client.NatsImpl.java

License:Open Source License

@Override
public Registration publish(String subject, String body, String replyTo, long period, TimeUnit unit) {
    final ClientPublishFrame publishFrame = new ClientPublishFrame(subject, body, replyTo);
    final ScheduledFuture<?> scheduledFuture = eventLoopGroup.next().scheduleAtFixedRate(new Runnable() {
        @Override//w  ww  .j a  va  2  s.co m
        public void run() {
            if (isConnected()) {
                publish(publishFrame);
            }
        }
    }, 0l, period, unit);
    return new Registration() {
        @Override
        public void remove() {
            scheduledFuture.cancel(false);
        }
    };
}

From source file:org.apache.spark.sql.hive.thriftserver.rsc.RSCDriver.java

License:Apache License

private void setupIdleTimeout() {
    if (clients.size() > 0) {
        return;/*from  w  ww  .j a v  a2 s .c  o m*/
    }

    Runnable timeoutTask = new Runnable() {
        @Override
        public void run() {
            LOG.warn("Shutting down RSC due to idle timeout ({}).",
                    livyConf.get(RSCConf.Entry.SERVER_IDLE_TIMEOUT));
            shutdown();
        }
    };
    ScheduledFuture<?> timeout = server.getEventLoopGroup().schedule(timeoutTask,
            livyConf.getTimeAsMs(RSCConf.Entry.SERVER_IDLE_TIMEOUT), TimeUnit.MILLISECONDS);

    // If there's already an idle task registered, then cancel the new one.
    if (!this.idleTimeout.compareAndSet(null, timeout)) {
        LOG.debug("Timeout task already registered.");
        timeout.cancel(false);
    }

    // If a new client connected while the idle task was being set up, then stop the task.
    if (clients.size() > 0) {
        stopIdleTimeout();
    }
}

From source file:org.redisson.client.RedisConnection.java

License:Apache License

public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
    final RPromise<R> promise = new RedissonPromise<R>();
    if (timeout == -1) {
        timeout = redisClient.getCommandTimeout();
    }/*from   ww  w .  ja va  2  s .  c  o m*/

    if (redisClient.getBootstrap().group().isShuttingDown()) {
        RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
        return RedissonPromise.newFailedFuture(cause);
    }

    final ScheduledFuture<?> scheduledFuture = redisClient.getBootstrap().group().next()
            .schedule(new Runnable() {
                @Override
                public void run() {
                    RedisTimeoutException ex = new RedisTimeoutException(
                            "Command execution timeout for " + redisClient.getAddr());
                    promise.tryFailure(ex);
                }
            }, timeout, TimeUnit.MILLISECONDS);

    promise.addListener(new FutureListener<R>() {
        @Override
        public void operationComplete(Future<R> future) throws Exception {
            scheduledFuture.cancel(false);
        }
    });
    send(new CommandData<T, R>(promise, encoder, command, params));
    return promise;
}