Example usage for io.netty.util TimerTask TimerTask

List of usage examples for io.netty.util TimerTask TimerTask

Introduction

In this page you can find the example usage for io.netty.util TimerTask TimerTask.

Prototype

TimerTask

Source Link

Usage

From source file:com.mpush.client.connect.ConnClientChannelHandler.java

License:Apache License

private void startHeartBeat(final int heartbeat) throws Exception {
    HASHED_WHEEL_TIMER.newTimeout(new TimerTask() {
        @Override/*from  w  w  w.j  av  a2 s  .  c o  m*/
        public void run(Timeout timeout) throws Exception {
            if (connection.isConnected() && healthCheck()) {
                HASHED_WHEEL_TIMER.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS);
            }
        }
    }, heartbeat, TimeUnit.MILLISECONDS);
}

From source file:com.spotify.ffwd.protocol.RetryingProtocolConnection.java

License:Apache License

private void trySetup(final int attempt) {
    log.info("Attempt {}", action);

    final ChannelFuture connect = action.setup();

    connect.addListener(new ChannelFutureListener() {
        @Override/*from   www . java  2  s.c  o  m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                log.info("Successful {}", action);
                setChannel(future.channel());
                return;
            }

            final long delay = policy.delay(attempt);

            log.warn("Failed {} (attempt: {}), retrying in {}s: {}", action, attempt + 1,
                    TimeUnit.SECONDS.convert(delay, TimeUnit.MILLISECONDS), future.cause().getMessage());

            timer.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    if (stopped.get()) {
                        return;
                    }

                    trySetup(attempt + 1);
                }
            }, delay, TimeUnit.MILLISECONDS);
        }
    });
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private <R> AsyncFuture<R> sendRequest(final Class<R> expected, final NativeRpcRequest request) {
    final ResolvableFuture<R> future = async.future();
    final AtomicReference<Timeout> heartbeatTimeout = new AtomicReference<>();

    final Bootstrap b = new Bootstrap();
    b.channel(NioSocketChannel.class);
    b.group(group);// w w w  .j  a va  2s.co m
    b.handler(new NativeRpcClientSession<R>(mapper, timer, heartbeatInterval, maxFrameSize, address,
            heartbeatTimeout, future, expected));

    // timeout for how long we are allowed to spend attempting to send a request.
    final Timeout sendTimeout = timer.newTimeout(new TimerTask() {
        @Override
        public void run(Timeout timeout) throws Exception {
            future.fail(new Exception("sending of request timed out"));
        }
    }, this.sendTimeout, TimeUnit.MILLISECONDS);

    b.connect(address).addListener(handleConnect(request, future, heartbeatTimeout, sendTimeout));

    return future;
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private TimerTask heartbeatTimeout(final Channel ch, final ResolvableFuture<?> future) {
    return new TimerTask() {
        @Override/*from  ww w.j  a  v  a2 s .c  o  m*/
        public void run(Timeout timeout) throws Exception {
            future.fail(new Exception("missing heartbeat, request timed out"));
            ch.close();
        }
    };
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClientSession.java

License:Apache License

private TimerTask heartbeatTimeout(final Channel ch, final ResolvableFuture<?> future) {
    return new TimerTask() {
        @Override/*from   w  w w.  j a v a 2s.  c o  m*/
        public void run(Timeout timeout) throws Exception {
            future.fail(new Exception("request timed out (missing heartbeat)"));
            ch.close();
        }
    };
}

From source file:com.uber.tchannel.handlers.ResponseRouter.java

License:Open Source License

protected void setTimer(final OutRequest outRequest) {
    final long start = System.currentTimeMillis();
    Timeout timeout = timer.newTimeout(new TimerTask() {
        @Override/* w  w  w .j  a va2 s.  com*/
        public void run(Timeout timeout) throws Exception {
            // prevent ByteBuf refCnt leak
            outRequest.flushWrite();
            if (timeouts.incrementAndGet() >= resetOnTimeoutLimit) {
                // reset on continuous timeouts
                peerManager.handleConnectionErrors(ctx.channel(), new TChannelConnectionReset(
                        String.format("Connection reset due to continuous %d timeouts", resetOnTimeoutLimit)));
                return;
            }

            handleResponse(new ErrorResponse(outRequest.getRequest().getId(), ErrorType.Timeout,
                    String.format("Request timeout after %dms", System.currentTimeMillis() - start)));
        }
    }, outRequest.getRequest().getTimeout(), TimeUnit.MILLISECONDS);

    outRequest.setTimeout(timeout);
}

From source file:com.yahoo.pulsar.client.impl.UnAckedMessageTracker.java

License:Apache License

public void start(PulsarClientImpl client, ConsumerBase consumerBase, long ackTimeoutMillis) {
    this.stop();//  w ww  . j  av a 2  s  . c  o m
    timeout = client.timer().newTimeout(new TimerTask() {
        @Override
        public void run(Timeout t) throws Exception {
            if (isAckTimeout()) {
                log.warn("[{}] {} messages have timed-out", consumerBase, oldOpenSet.size());
                Set<MessageIdImpl> messageIds = new HashSet<>();
                oldOpenSet.forEach(messageIds::add);
                oldOpenSet.clear();
                consumerBase.redeliverUnacknowledgedMessages(messageIds);
            }
            toggle();
            timeout = client.timer().newTimeout(this, ackTimeoutMillis, TimeUnit.MILLISECONDS);
        }
    }, ackTimeoutMillis, TimeUnit.MILLISECONDS);
}

From source file:in.voidma.lemming.traffic.TrafficLatencyHandler.java

License:Open Source License

@Override
public void read(ChannelHandlerContext ctx) throws Exception {
    if (inBound) {
        timer.newTimeout(new TimerTask() {
            @Override/*from ww  w.  j  av  a  2 s.c  o  m*/
            public void run(Timeout timeout) throws Exception {
                ctx.read();
            }
        }, TimeLagMS, TimeUnit.MILLISECONDS);
    }
}

From source file:in.voidma.lemming.traffic.TrafficLatencyHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (outBound) {
        timer.newTimeout(new TimerTask() {
            @Override/*ww w.  ja v a2  s  .com*/
            public void run(Timeout timeout) throws Exception {
                ctx.write(msg, promise);
            }
        }, TimeLagMS, TimeUnit.MILLISECONDS);
    }
}

From source file:in.voidma.lemming.traffic.TrafficLatencyHandler.java

License:Open Source License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (outBound) {
        timer.newTimeout(new TimerTask() {
            @Override// w w  w  .  j  a  v a  2  s . c om
            public void run(Timeout timeout) throws Exception {
                ctx.flush();
            }
        }, TimeLagMS, TimeUnit.MILLISECONDS);
    }
}