Search in sources :

Example 1 with BackOffExecution

use of org.springframework.util.backoff.BackOffExecution in project pinpoint by naver.

the class OneByteFuzzyRowKeyFactory method allocateSlot.

private long[] allocateSlot() {
    ExponentialBackOff exponentialBackOff = new ExponentialBackOff(100, 2);
    exponentialBackOff.setMaxInterval(TimeUnit.MINUTES.toMillis(MAX_SLOT_TIME * 2));
    BackOffExecution backOffExecution = exponentialBackOff.start();
    final List<Long> backOffTimeList = new ArrayList<>();
    for (int i = 0; i < MAX_SLOT; i++) {
        final long nextBackOff = backOffExecution.nextBackOff();
        backOffTimeList.add(nextBackOff);
        if (MAX_SLOT_TIME <= nextBackOff) {
            break;
        }
    }
    return toLongArray(backOffTimeList);
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) ArrayList(java.util.ArrayList) ExponentialBackOff(org.springframework.util.backoff.ExponentialBackOff)

Example 2 with BackOffExecution

use of org.springframework.util.backoff.BackOffExecution in project spring-framework by spring-projects.

the class DefaultMessageListenerContainer method refreshConnectionUntilSuccessful.

/**
 * Refresh the underlying Connection, not returning before an attempt has been
 * successful. Called in case of a shared Connection as well as without shared
 * Connection, so either needs to operate on the shared Connection or on a
 * temporary Connection that just gets established for validation purposes.
 * <p>The default implementation retries until it successfully established a
 * Connection, for as long as this message listener container is running.
 * Applies the specified recovery interval between retries.
 * @see #setRecoveryInterval
 * @see #start()
 * @see #stop()
 */
protected void refreshConnectionUntilSuccessful() {
    BackOffExecution execution = this.backOff.start();
    while (isRunning()) {
        try {
            if (sharedConnectionEnabled()) {
                refreshSharedConnection();
            } else {
                Connection con = createConnection();
                JmsUtils.closeConnection(con);
            }
            logger.debug("Successfully refreshed JMS Connection");
            break;
        } catch (Exception ex) {
            if (ex instanceof JMSException) {
                invokeExceptionListener((JMSException) ex);
            }
            StringBuilder msg = new StringBuilder();
            msg.append("Could not refresh JMS Connection for destination '");
            msg.append(getDestinationDescription()).append("' - retrying using ");
            msg.append(execution).append(". Cause: ");
            msg.append(ex instanceof JMSException ? JmsUtils.buildExceptionMessage((JMSException) ex) : ex.getMessage());
            if (logger.isDebugEnabled()) {
                logger.error(msg, ex);
            } else {
                logger.error(msg);
            }
        }
        if (!applyBackOffTime(execution)) {
            logger.error("Stopping container for destination '" + getDestinationDescription() + "': back-off policy does not allow for further attempts.");
            stop();
        }
    }
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) JmsException(org.springframework.jms.JmsException) JMSException(jakarta.jms.JMSException)

Example 3 with BackOffExecution

use of org.springframework.util.backoff.BackOffExecution in project spring-framework by spring-projects.

the class ExponentialBackOffTests method maxAttemptsReached.

@Test
void maxAttemptsReached() {
    ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
    backOff.setMaxElapsedTime(4000L);
    BackOffExecution execution = backOff.start();
    assertThat(execution.nextBackOff()).isEqualTo(2000L);
    assertThat(execution.nextBackOff()).isEqualTo(4000L);
    // > 4 sec wait in total
    assertThat(execution.nextBackOff()).isEqualTo(BackOffExecution.STOP);
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) ExponentialBackOff(org.springframework.util.backoff.ExponentialBackOff) Test(org.junit.jupiter.api.Test)

Example 4 with BackOffExecution

use of org.springframework.util.backoff.BackOffExecution in project spring-framework by spring-projects.

the class ExponentialBackOffTests method defaultInstance.

@Test
void defaultInstance() {
    ExponentialBackOff backOff = new ExponentialBackOff();
    BackOffExecution execution = backOff.start();
    assertThat(execution.nextBackOff()).isEqualTo(2000L);
    assertThat(execution.nextBackOff()).isEqualTo(3000L);
    assertThat(execution.nextBackOff()).isEqualTo(4500L);
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) ExponentialBackOff(org.springframework.util.backoff.ExponentialBackOff) Test(org.junit.jupiter.api.Test)

Example 5 with BackOffExecution

use of org.springframework.util.backoff.BackOffExecution in project spring-framework by spring-projects.

the class ExponentialBackOffTests method maxIntervalReached.

@Test
void maxIntervalReached() {
    ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
    backOff.setMaxInterval(4000L);
    BackOffExecution execution = backOff.start();
    assertThat(execution.nextBackOff()).isEqualTo(2000L);
    assertThat(execution.nextBackOff()).isEqualTo(4000L);
    // max reached
    assertThat(execution.nextBackOff()).isEqualTo(4000L);
    assertThat(execution.nextBackOff()).isEqualTo(4000L);
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) ExponentialBackOff(org.springframework.util.backoff.ExponentialBackOff) Test(org.junit.jupiter.api.Test)

Aggregations

BackOffExecution (org.springframework.util.backoff.BackOffExecution)19 Test (org.junit.jupiter.api.Test)17 ExponentialBackOff (org.springframework.util.backoff.ExponentialBackOff)9 FixedBackOff (org.springframework.util.backoff.FixedBackOff)6 BackOff (org.springframework.util.backoff.BackOff)3 Connection (jakarta.jms.Connection)1 JMSException (jakarta.jms.JMSException)1 ArrayList (java.util.ArrayList)1 JmsException (org.springframework.jms.JmsException)1