Interface ReconnectionStrategy
-
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface ReconnectionStrategy
A strategy for reconnection logic, i.e. when and in which interval reconnection attempts will happen. You can provide your own strategy by implementing this interface.Alternatively you can use some of the predefined strategies which you can retrieve by one of the static methods.
E.g.
alwaysAfter(Duration)
always tries to reconnect after a fix amount of time.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static ReconnectionStrategy
alwaysAfter(Duration duration)
Reconnects always after a fix duration, e.g. after 10 seconds.static ReconnectionStrategy
alwaysRandomlyAfter(Duration min, Duration max)
Reconnects always after a random duration which lies between the given min and max duration, e.g. after 10-20 seconds.Duration
getNextReconnectionAttempt(int attempt, Throwable cause)
Gets the duration after which the next reconnection is attempted.default boolean
mayReconnect(int attempt, Throwable e)
static ReconnectionStrategy
none()
Reconnection won't happen automatically, i.e. it's disabled.static ReconnectionStrategy
onSystemShutdownFirstOrElseSecond(ReconnectionStrategy first, ReconnectionStrategy second)
Uses a hybrid reconnection strategy, which uses the first one on system shutdown and the second one on every other disconnection cause.static ReconnectionStrategy
truncatedBinaryExponentialBackoffStrategy(int slotTime, int ceiling)
This is the default reconnection strategy.
-
-
-
Method Detail
-
mayReconnect
default boolean mayReconnect(int attempt, Throwable e)
-
getNextReconnectionAttempt
Duration getNextReconnectionAttempt(int attempt, Throwable cause)
Gets the duration after which the next reconnection is attempted.- Parameters:
attempt
- The current reconnection attempt. The first attempt is 0, the second attempt is 1, etc...cause
- The cause for the disconnection.- Returns:
- The duration after which the next reconnection is attempted.
-
truncatedBinaryExponentialBackoffStrategy
static ReconnectionStrategy truncatedBinaryExponentialBackoffStrategy(int slotTime, int ceiling)
This is the default reconnection strategy.It exponentially increases the time span from which a random value for the next reconnection attempt is chosen. The formula for doing this, is:
(2n - 1) * s
, wheren
is the number of reconnection attempt ands
is the slot time, which is 60 seconds by default.In practice this means, the first reconnection attempt occurs after a random period of time between 0 and 60 seconds.
The second attempt chooses a random number >= 0 and < 180 seconds.
The third attempt chooses a random number >= 0 and < 420 seconds.
The fourth attempt chooses a random number >= 0 and < 900 seconds.
The fifth attempt chooses a random number >= 0 and < 1860 seconds (= 31 minutes)The strategy is called "truncated", because it won't increase the time span after the nth iteration, which means in the example above, the sixth and any further attempt behaves equally to the fifth attempt.
This "truncated binary exponential backoff" is the recommended reconnection strategy by the XMPP specification.
- Parameters:
slotTime
- The slot time (in seconds), usually 60.ceiling
- The ceiling, i.e. when the time is truncated. E.g. if the ceiling is 4, the back off is truncated at the 5th reconnection attempt (it starts at zero).- Returns:
- The truncated binary exponential backoff strategy.
-
alwaysAfter
static ReconnectionStrategy alwaysAfter(Duration duration)
Reconnects always after a fix duration, e.g. after 10 seconds. When a disconnection is detected the first reconnection attempt is started after the given duration. If the attempt fails, the second one is started again after the same duration and so on.- Parameters:
duration
- The fix duration after which a reconnection is attempted.- Returns:
- The reconnection strategy.
-
alwaysRandomlyAfter
static ReconnectionStrategy alwaysRandomlyAfter(Duration min, Duration max)
Reconnects always after a random duration which lies between the given min and max duration, e.g. after 10-20 seconds.- Parameters:
min
- The min duration after which a reconnection is attempted.max
- The max duration.- Returns:
- The reconnection strategy.
-
onSystemShutdownFirstOrElseSecond
static ReconnectionStrategy onSystemShutdownFirstOrElseSecond(ReconnectionStrategy first, ReconnectionStrategy second)
Uses a hybrid reconnection strategy, which uses the first one on system shutdown and the second one on every other disconnection cause.- Parameters:
first
- The first strategy.second
- The second strategy.- Returns:
- The reconnection strategy.
-
none
static ReconnectionStrategy none()
Reconnection won't happen automatically, i.e. it's disabled.- Returns:
- The reconnection strategy.
-
-