List of usage examples for io.netty.util TimerTask TimerTask
TimerTask
From source file:org.opendaylight.protocol.pcep.pcc.mock.PCCTunnelManagerImpl.java
License:Open Source License
private void startStateTimeout(final PCCTunnel tunnel, final PlspId plspId) { if (this.stateTimeout > -1) { final Timeout newStateTimeout = this.timer.newTimeout(new TimerTask() { @Override/* w w w. j a v a 2 s. c om*/ public void run(final Timeout timeout) throws Exception { if (tunnel.getType() == LspType.PCE_LSP) { PCCTunnelManagerImpl.this.tunnels.remove(plspId); //report tunnel removal to all sendToAll(tunnel, plspId, Collections.<Subobject>emptyList(), createSrp(0), new PathBuilder().build(), createLsp(plspId.getValue(), false, Optional.<Tlvs>absent(), false, true)); } } }, this.stateTimeout, TimeUnit.SECONDS); tunnel.setStateTimeout(newStateTimeout); } }
From source file:org.opendaylight.protocol.pcep.pcc.mock.PCCTunnelManagerImpl.java
License:Open Source License
private void startRedelegationTimer(final PCCTunnel tunnel, final PlspId plspId, final PCCSession session) { final Timeout newRedelegationTimeout = this.timer.newTimeout(new TimerTask() { @Override// w w w.j a v a 2 s.com public void run(final Timeout timeout) throws Exception { //remove delegation PCCTunnelManagerImpl.this.setDelegation(plspId, null); //delegate to another PCE int index = session.getId(); for (int i = 1; i < PCCTunnelManagerImpl.this.sessions.size(); i++) { index++; if (index == PCCTunnelManagerImpl.this.sessions.size()) { index = 0; } final PCCSession nextSession = PCCTunnelManagerImpl.this.sessions.get(index); if (nextSession != null) { tunnel.cancelTimeouts(); final Tlvs tlvs = buildTlvs(tunnel, plspId.getValue(), Optional.<List<Subobject>>absent()); nextSession.sendReport(createPcRtpMessage( createLsp(plspId.getValue(), true, Optional.fromNullable(tlvs), true, false), NO_SRP, tunnel.getLspState())); tunnel.setDelegationHolder(nextSession.getId()); break; } } } }, this.redelegationTimeout, TimeUnit.SECONDS); tunnel.setRedelegationTimeout(newRedelegationTimeout); }
From source file:org.redisson.client.handler.ConnectionWatchdog.java
License:Apache License
private void reconnect(final RedisConnection connection, final int attempts) { int timeout = 2 << attempts; if (bootstrap.group().isShuttingDown()) { return;// w ww . j av a 2s . c o m } timer.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { tryReconnect(connection, Math.min(BACKOFF_CAP, attempts + 1)); } }, timeout, TimeUnit.MILLISECONDS); }
From source file:org.redisson.client.handler.PingConnectionHandler.java
License:Apache License
protected void sendPing(final ChannelHandlerContext ctx) { final RedisConnection connection = RedisConnection.getFrom(ctx.channel()); final RFuture<String> future = connection.async(StringCodec.INSTANCE, RedisCommands.PING); config.getTimer().newTimeout(new TimerTask() { @Override//w w w . j a va2 s. c o m public void run(Timeout timeout) throws Exception { CommandData<?, ?> commandData = connection.getCurrentCommand(); if ((commandData == null || !commandData.isBlockingCommand()) && (future.cancel(false) || !future.isSuccess())) { ctx.channel().close(); log.debug("channel: {} closed due to PING response timeout set in {} ms", ctx.channel(), config.getPingConnectionInterval()); } else { sendPing(ctx); } } }, config.getPingConnectionInterval(), TimeUnit.MILLISECONDS); }
From source file:org.redisson.command.CommandAsyncService.java
License:Apache License
protected <V, R> void async(final boolean readOnlyMode, final NodeSource source, final Codec codec, final RedisCommand<V> command, final Object[] params, final RPromise<R> mainPromise, final int attempt) { if (mainPromise.isCancelled()) { return;//from ww w. ja v a2s . com } if (!connectionManager.getShutdownLatch().acquire()) { mainPromise.tryFailure(new RedissonShutdownException("Redisson is shutdown")); return; } final AsyncDetails<V, R> details = AsyncDetails.acquire(); if (isRedissonReferenceSupportEnabled()) { try { for (int i = 0; i < params.length; i++) { RedissonReference reference = redisson != null ? RedissonObjectFactory.toReference(redisson, params[i]) : RedissonObjectFactory.toReference(redissonReactive, params[i]); params[i] = reference == null ? params[i] : reference; } } catch (Exception e) { connectionManager.getShutdownLatch().release(); mainPromise.tryFailure(e); return; } } final RFuture<RedisConnection> connectionFuture; if (readOnlyMode) { connectionFuture = connectionManager.connectionReadOp(source, command); } else { connectionFuture = connectionManager.connectionWriteOp(source, command); } final RPromise<R> attemptPromise = connectionManager.newPromise(); details.init(connectionFuture, attemptPromise, readOnlyMode, source, codec, command, params, mainPromise, attempt); final TimerTask retryTimerTask = new TimerTask() { @Override public void run(Timeout t) throws Exception { if (details.getAttemptPromise().isDone()) { return; } if (details.getConnectionFuture().cancel(false)) { connectionManager.getShutdownLatch().release(); } else { if (details.getConnectionFuture().isSuccess()) { ChannelFuture writeFuture = details.getWriteFuture(); if (writeFuture != null && !writeFuture.cancel(false) && writeFuture.isSuccess()) { return; } } } if (details.getMainPromise().isCancelled()) { if (details.getAttemptPromise().cancel(false)) { AsyncDetails.release(details); } return; } if (details.getAttempt() == connectionManager.getConfig().getRetryAttempts()) { if (details.getException() == null) { details.setException(new RedisTimeoutException("Command execution timeout for command: " + command + " with params: " + LogHelper.toString(details.getParams()))); } details.getAttemptPromise().tryFailure(details.getException()); return; } if (!details.getAttemptPromise().cancel(false)) { return; } int count = details.getAttempt() + 1; if (log.isDebugEnabled()) { log.debug("attempt {} for command {} and params {}", count, details.getCommand(), Arrays.toString(details.getParams())); } async(details.isReadOnlyMode(), details.getSource(), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), count); AsyncDetails.release(details); } }; Timeout timeout = connectionManager.newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS); details.setTimeout(timeout); connectionFuture.addListener(new FutureListener<RedisConnection>() { @Override public void operationComplete(Future<RedisConnection> connFuture) throws Exception { if (connFuture.isCancelled()) { return; } if (!connFuture.isSuccess()) { connectionManager.getShutdownLatch().release(); details.setException(convertException(connectionFuture)); return; } if (details.getAttemptPromise().isDone() || details.getMainPromise().isDone()) { releaseConnection(source, connectionFuture, details.isReadOnlyMode(), details.getAttemptPromise(), details); return; } final RedisConnection connection = connFuture.getNow(); if (details.getSource().getRedirect() == Redirect.ASK) { List<CommandData<?, ?>> list = new ArrayList<CommandData<?, ?>>(2); RPromise<Void> promise = connectionManager.newPromise(); list.add(new CommandData<Void, Void>(promise, details.getCodec(), RedisCommands.ASKING, new Object[] {})); list.add(new CommandData<V, R>(details.getAttemptPromise(), details.getCodec(), details.getCommand(), details.getParams())); RPromise<Void> main = connectionManager.newPromise(); ChannelFuture future = connection.send(new CommandsData(main, list)); details.setWriteFuture(future); } else { if (log.isDebugEnabled()) { log.debug("aquired connection for command {} and params {} from slot {} using node {}", details.getCommand(), Arrays.toString(details.getParams()), details.getSource(), connection.getRedisClient().getAddr()); } ChannelFuture future = connection.send(new CommandData<V, R>(details.getAttemptPromise(), details.getCodec(), details.getCommand(), details.getParams())); details.setWriteFuture(future); } details.getWriteFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { checkWriteFuture(details, connection); } }); releaseConnection(source, connectionFuture, details.isReadOnlyMode(), details.getAttemptPromise(), details); } }); attemptPromise.addListener(new FutureListener<R>() { @Override public void operationComplete(Future<R> future) throws Exception { checkAttemptFuture(source, details, future); } }); }
From source file:org.redisson.command.CommandAsyncService.java
License:Apache License
private <V, R> void checkWriteFuture(final AsyncDetails<V, R> details, final RedisConnection connection) { ChannelFuture future = details.getWriteFuture(); if (details.getAttemptPromise().isDone() || future.isCancelled()) { return;//from w w w. j a v a2 s .co m } if (!future.isSuccess()) { details.setException(new WriteRedisConnectionException( "Can't write command: " + details.getCommand() + ", params: " + LogHelper.toString(details.getParams()) + " to channel: " + future.channel(), future.cause())); return; } details.getTimeout().cancel(); long timeoutTime = connectionManager.getConfig().getTimeout(); if (RedisCommands.BLOCKING_COMMANDS.contains(details.getCommand().getName())) { Long popTimeout = Long.valueOf(details.getParams()[details.getParams().length - 1].toString()); handleBlockingOperations(details, connection, popTimeout); if (popTimeout == 0) { return; } timeoutTime += popTimeout * 1000; // add 1 second due to issue https://github.com/antirez/redis/issues/874 timeoutTime += 1000; } final long timeoutAmount = timeoutTime; TimerTask timeoutTask = new TimerTask() { @Override public void run(Timeout timeout) throws Exception { details.getAttemptPromise().tryFailure(new RedisTimeoutException("Redis server response timeout (" + timeoutAmount + " ms) occured for command: " + details.getCommand() + " with params: " + LogHelper.toString(details.getParams()) + " channel: " + connection.getChannel())); } }; Timeout timeout = connectionManager.newTimeout(timeoutTask, timeoutTime, TimeUnit.MILLISECONDS); details.setTimeout(timeout); }
From source file:org.redisson.command.CommandAsyncService.java
License:Apache License
private <R, V> void handleBlockingOperations(final AsyncDetails<V, R> details, final RedisConnection connection, Long popTimeout) {/* w w w .jav a2 s.c o m*/ final FutureListener<Boolean> listener = new FutureListener<Boolean>() { @Override public void operationComplete(Future<Boolean> future) throws Exception { details.getMainPromise().tryFailure(new RedissonShutdownException("Redisson is shutdown")); } }; final AtomicBoolean canceledByScheduler = new AtomicBoolean(); final Timeout scheduledFuture; if (popTimeout != 0) { // to handle cases when connection has been lost final Channel orignalChannel = connection.getChannel(); scheduledFuture = connectionManager.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { // re-connection wasn't made // and connection is still active if (orignalChannel == connection.getChannel() && connection.isActive()) { return; } canceledByScheduler.set(true); details.getAttemptPromise().trySuccess(null); } }, popTimeout, TimeUnit.SECONDS); } else { scheduledFuture = null; } details.getMainPromise().addListener(new FutureListener<R>() { @Override public void operationComplete(Future<R> future) throws Exception { if (scheduledFuture != null) { scheduledFuture.cancel(); } synchronized (listener) { connectionManager.getShutdownPromise().removeListener(listener); } // handling cancel operation for commands from skipTimeout collection if ((future.isCancelled() && details.getAttemptPromise().cancel(true)) || canceledByScheduler.get()) { connection.forceFastReconnectAsync(); return; } if (future.cause() instanceof RedissonShutdownException) { details.getAttemptPromise().tryFailure(future.cause()); } } }); synchronized (listener) { if (!details.getMainPromise().isDone()) { connectionManager.getShutdownPromise().addListener(listener); } } }
From source file:org.redisson.command.CommandAsyncService.java
License:Apache License
private <R, V> void checkAttemptFuture(final NodeSource source, final AsyncDetails<V, R> details, Future<R> future) { details.getTimeout().cancel();//w w w.jav a2s.c o m if (future.isCancelled()) { return; } if (future.cause() instanceof RedisMovedException) { RedisMovedException ex = (RedisMovedException) future.cause(); async(details.isReadOnlyMode(), new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.MOVED), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt()); AsyncDetails.release(details); return; } if (future.cause() instanceof RedisAskException) { RedisAskException ex = (RedisAskException) future.cause(); async(details.isReadOnlyMode(), new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.ASK), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt()); AsyncDetails.release(details); return; } if (future.cause() instanceof RedisLoadingException) { async(details.isReadOnlyMode(), source, details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt()); AsyncDetails.release(details); return; } if (future.cause() instanceof RedisTryAgainException) { connectionManager.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { async(details.isReadOnlyMode(), source, details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt()); } }, 1, TimeUnit.SECONDS); AsyncDetails.release(details); return; } if (future.isSuccess()) { R res = future.getNow(); if (res instanceof RedisClientResult) { InetSocketAddress addr = source.getAddr(); if (addr == null) { addr = details.getConnectionFuture().getNow().getRedisClient().getAddr(); } ((RedisClientResult) res).setRedisClient(addr); } if (isRedissonReferenceSupportEnabled()) { handleReference(details.getMainPromise(), res); } else { details.getMainPromise().trySuccess(res); } } else { details.getMainPromise().tryFailure(future.cause()); } AsyncDetails.release(details); }
From source file:org.redisson.command.CommandBatchService.java
License:Apache License
private void execute(final Entry entry, final NodeSource source, final RPromise<Void> mainPromise, final AtomicInteger slots, final int attempt, final boolean noResult) { if (mainPromise.isCancelled()) { return;/* ww w . j a v a 2s . c o m*/ } if (!connectionManager.getShutdownLatch().acquire()) { mainPromise.tryFailure(new IllegalStateException("Redisson is shutdown")); return; } final RPromise<Void> attemptPromise = connectionManager.newPromise(); final AsyncDetails details = new AsyncDetails(); final RFuture<RedisConnection> connectionFuture; if (entry.isReadOnlyMode()) { connectionFuture = connectionManager.connectionReadOp(source, null); } else { connectionFuture = connectionManager.connectionWriteOp(source, null); } final TimerTask retryTimerTask = new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (attemptPromise.isDone()) { return; } if (connectionFuture.cancel(false)) { connectionManager.getShutdownLatch().release(); } else { if (connectionFuture.isSuccess()) { ChannelFuture writeFuture = details.getWriteFuture(); if (writeFuture != null && !writeFuture.cancel(false) && writeFuture.isSuccess()) { return; } } } if (mainPromise.isCancelled()) { attemptPromise.cancel(false); return; } if (attempt == connectionManager.getConfig().getRetryAttempts()) { if (details.getException() == null) { details.setException(new RedisTimeoutException("Batch command execution timeout")); } attemptPromise.tryFailure(details.getException()); return; } if (!attemptPromise.cancel(false)) { return; } int count = attempt + 1; execute(entry, source, mainPromise, slots, count, noResult); } }; Timeout timeout = connectionManager.newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS); details.setTimeout(timeout); connectionFuture.addListener(new FutureListener<RedisConnection>() { @Override public void operationComplete(Future<RedisConnection> connFuture) throws Exception { checkConnectionFuture(entry, source, mainPromise, attemptPromise, details, connectionFuture, noResult); } }); attemptPromise.addListener(new FutureListener<Void>() { @Override public void operationComplete(Future<Void> future) throws Exception { details.getTimeout().cancel(); if (future.isCancelled()) { return; } if (future.cause() instanceof RedisMovedException) { RedisMovedException ex = (RedisMovedException) future.cause(); entry.clearErrors(); execute(entry, new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.MOVED), mainPromise, slots, attempt, noResult); return; } if (future.cause() instanceof RedisAskException) { RedisAskException ex = (RedisAskException) future.cause(); entry.clearErrors(); execute(entry, new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.ASK), mainPromise, slots, attempt, noResult); return; } if (future.cause() instanceof RedisLoadingException) { entry.clearErrors(); execute(entry, source, mainPromise, slots, attempt, noResult); return; } if (future.cause() instanceof RedisTryAgainException) { entry.clearErrors(); connectionManager.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { execute(entry, source, mainPromise, slots, attempt, noResult); } }, 1, TimeUnit.SECONDS); return; } if (future.isSuccess()) { if (slots.decrementAndGet() == 0) { mainPromise.trySuccess(future.getNow()); } } else { mainPromise.tryFailure(future.cause()); } } }); }
From source file:org.redisson.command.CommandBatchService.java
License:Apache License
private void checkWriteFuture(final RPromise<Void> attemptPromise, AsyncDetails details, final RedisConnection connection, ChannelFuture future, boolean noResult) { if (attemptPromise.isDone() || future.isCancelled()) { return;/*from w ww . ja v a 2 s.c om*/ } if (!future.isSuccess()) { details.setException(new WriteRedisConnectionException( "Can't write command batch to channel: " + future.channel(), future.cause())); } else { details.getTimeout().cancel(); TimerTask timeoutTask = new TimerTask() { @Override public void run(Timeout timeout) throws Exception { attemptPromise.tryFailure(new RedisTimeoutException( "Redis server response timeout during command batch execution. Channel: " + connection.getChannel())); } }; Timeout timeout = connectionManager.newTimeout(timeoutTask, connectionManager.getConfig().getTimeout(), TimeUnit.MILLISECONDS); details.setTimeout(timeout); } }