@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.Modifier and Type | Method and Description |
---|---|
static ReconnectionStrategy |
alwaysAfter(java.time.Duration duration)
Reconnects always after a fix duration, e.g. after 10 seconds.
|
static ReconnectionStrategy |
alwaysRandomlyAfter(java.time.Duration min,
java.time.Duration max)
Reconnects always after a random duration which lies between the given min and max duration, e.g. after 10-20 seconds.
|
java.time.Duration |
getNextReconnectionAttempt(int attempt,
java.lang.Throwable cause)
Gets the duration after which the next reconnection is attempted.
|
default boolean |
mayReconnect(int attempt,
java.lang.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.
|
default boolean mayReconnect(int attempt, java.lang.Throwable e)
java.time.Duration getNextReconnectionAttempt(int attempt, java.lang.Throwable cause)
Gets the duration after which the next reconnection is attempted.
attempt
- The current reconnection attempt. The first attempt is 0, the second attempt is 1, etc…cause
- The cause for the disconnection.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
, where n
is the number of reconnection attempt and s
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.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).static ReconnectionStrategy alwaysAfter(java.time.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.
duration
- The fix duration after which a reconnection is attempted.static ReconnectionStrategy alwaysRandomlyAfter(java.time.Duration min, java.time.Duration max)
Reconnects always after a random duration which lies between the given min and max duration, e.g. after 10-20 seconds.
min
- The min duration after which a reconnection is attempted.max
- The max duration.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.
first
- The first strategy.second
- The second strategy.static ReconnectionStrategy none()
Reconnection won’t happen automatically, i.e. it’s disabled.
Copyright © 2014–2019 XMPP.rocks. All rights reserved.