List of usage examples for org.springframework.util.backoff BackOffExecution nextBackOff
long nextBackOff();
From source file:com.netflix.spinnaker.fiat.roles.UserRolesSyncer.java
public long syncAndReturn() { FixedBackOff backoff = new FixedBackOff(); backoff.setInterval(retryIntervalMs); BackOffExecution backOffExec = backoff.start(); if (!isServerHealthy()) { log.warn("Server is currently UNHEALTHY. User permission role synchronization and " + "resolution may not complete until this server becomes healthy again."); }//from ww w . ja v a 2 s . c o m while (true) { try { Map<String, UserPermission> combo = new HashMap<>(); Map<String, UserPermission> temp; if (!(temp = getUserPermissions()).isEmpty()) { combo.putAll(temp); } if (!(temp = getServiceAccountsAsMap()).isEmpty()) { combo.putAll(temp); } return updateUserPermissions(combo); } catch (ProviderException | PermissionResolutionException ex) { Status status = healthIndicator.health().getStatus(); long waitTime = backOffExec.nextBackOff(); if (waitTime == BackOffExecution.STOP) { log.error("Unable to resolve service account permissions.", ex); return 0; } String message = new StringBuilder("User permission sync failed. ").append("Server status is ") .append(status).append(". Trying again in ").append(waitTime).append(" ms. Cause:") .append(ex.getMessage()).toString(); if (log.isDebugEnabled()) { log.debug(message, ex); } else { log.warn(message); } try { Thread.sleep(waitTime); } catch (InterruptedException ignored) { } } finally { isServerHealthy(); } } }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java
protected void actualStart() throws Exception { this.aborted = false; this.hasStopped = false; super.doStart(); final String[] queueNames = getQueueNames(); checkMissingQueues(queueNames);/*from w ww . j a v a 2s.c o m*/ if (getTaskExecutor() == null) { afterPropertiesSet(); } long idleEventInterval = getIdleEventInterval(); if (this.taskScheduler == null) { afterPropertiesSet(); } if (idleEventInterval > 0 && this.monitorInterval > idleEventInterval) { this.monitorInterval = idleEventInterval / 2; } if (getFailedDeclarationRetryInterval() < this.monitorInterval) { this.monitorInterval = getFailedDeclarationRetryInterval(); } this.lastRestartAttempt = System.currentTimeMillis(); this.consumerMonitorTask = this.taskScheduler.scheduleAtFixedRate(() -> { long now = System.currentTimeMillis(); if (idleEventInterval > 0) { if (now - getLastReceive() > idleEventInterval && now - this.lastAlertAt > idleEventInterval) { publishIdleContainerEvent(now - getLastReceive()); this.lastAlertAt = now; } } this.consumers.stream().filter(c -> !c.getChannel().isOpen()).collect(Collectors.toList()) // needed to avoid ConcurrentModificationException in cancelConsumer() .forEach(c -> { this.logger.error("Consumer canceled - channel closed " + c); c.cancelConsumer("Consumer " + c + " channel closed"); }); if (this.lastRestartAttempt + getFailedDeclarationRetryInterval() < now) { synchronized (this.consumersMonitor) { List<SimpleConsumer> restartableConsumers = new ArrayList<>(this.consumersToRestart); this.consumersToRestart.clear(); if (this.started) { if (restartableConsumers.size() > 0) { redeclareElementsIfNecessary(); } for (SimpleConsumer consumer : restartableConsumers) { if (this.logger.isDebugEnabled() && restartableConsumers.size() > 0) { logger.debug("Attempting to restart consumer " + consumer); } doConsumeFromQueue(consumer.getQueue()); } this.lastRestartAttempt = now; } } } processMonitorTask(); }, this.monitorInterval); if (queueNames.length > 0) { try { redeclareElementsIfNecessary(); } catch (Exception e) { this.logger.error("Failed to redeclare elements", e); } getTaskExecutor().execute(() -> { synchronized (this.consumersMonitor) { if (this.hasStopped) { // container stopped before we got the lock if (this.logger.isDebugEnabled()) { this.logger.debug("Consumer start aborted - container stopping"); } } else { BackOffExecution backOffExecution = getRecoveryBackOff().start(); while (!DirectMessageListenerContainer.this.started && isRunning()) { this.cancellationLock.reset(); try { for (String queue : queueNames) { consumeFromQueue(queue); } } catch (AmqpConnectException e) { long nextBackOff = backOffExecution.nextBackOff(); if (nextBackOff < 0) { DirectMessageListenerContainer.this.aborted = true; shutdown(); this.logger.error("Failed to start container - backOffs exhausted", e); break; } this.logger.error("Error creating consumer; retrying in " + nextBackOff, e); doShutdown(); try { Thread.sleep(nextBackOff); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } continue; // initialization failed; try again having rested for backOff-interval } DirectMessageListenerContainer.this.started = true; DirectMessageListenerContainer.this.startedLatch.countDown(); } } } }); } else { this.started = true; this.startedLatch.countDown(); } if (logger.isInfoEnabled()) { this.logger.info("Container initialized for queues: " + Arrays.asList(queueNames)); } }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.java
/** * Wait for a period determined by the {@link #setRecoveryInterval(long) recoveryInterval} * or {@link #setRecoveryBackOff(BackOff)} to give the container a * chance to recover from consumer startup failure, e.g. if the broker is down. * @param backOffExecution the BackOffExecution to get the {@code recoveryInterval} * @throws Exception if the shared connection still can't be established *//*from w ww . j av a 2 s .co m*/ protected void handleStartupFailure(BackOffExecution backOffExecution) throws Exception { long recoveryInterval = backOffExecution.nextBackOff(); if (BackOffExecution.STOP == recoveryInterval) { synchronized (this) { if (isActive()) { logger.warn("stopping container - restart recovery attempts exhausted"); stop(); } } return; } try { if (logger.isDebugEnabled() && isActive()) { logger.debug("Recovering consumer in " + recoveryInterval + " ms."); } long timeout = System.currentTimeMillis() + recoveryInterval; while (isActive() && System.currentTimeMillis() < timeout) { Thread.sleep(200); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException("Unrecoverable interruption on consumer restart"); } }