Interface MqttClientReconnector
- All Known Subinterfaces:
Mqtt3ClientReconnector,Mqtt5ClientReconnector
MqttClientDisconnectedContext and can be used for reconnecting.
The client will reconnect only if at least one of the methods reconnect(boolean) or reconnectWhen(CompletableFuture, BiConsumer) is called.
All methods must only be called in MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).
Some methods can also be called in the callback supplied to reconnectWhen(CompletableFuture, BiConsumer).
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault delay in milliseconds the client will wait for before trying to reconnect.static final booleanIf reconnect is enabled by default.static final booleanIf republish when the session expired before the client reconnected successfully is enabled by default.static final booleanIf resubscribe when the session expired before the client reconnected successfully is enabled by default.static final booleanIf resubscribe when the session is present when the client reconnected successfully is enabled by default. -
Method Summary
Modifier and TypeMethodDescriptionSets a delay the client will wait for before trying to reconnect.intlongReturns the currently set delay the client will wait for before trying to reconnect.Returns the currently set transport configuration the client will try to reconnect with.booleanbooleanbooleanbooleanreconnect(boolean reconnect) Instructs the client to reconnect or not.reconnectWhen(@NotNull CompletableFuture<T> future, @Nullable BiConsumer<? super T, ? super Throwable> callback) Instructs the client to reconnect after a future completes.republishIfSessionExpired(boolean republish) Instructs the client to queue pending Publish messages and automatically publish them even if the session expired before reconnected successfully.resubscribeIfSessionExpired(boolean resubscribe) Instructs the client to automatically restore its subscriptions when the session expired before it reconnected successfully.resubscribeIfSessionPresent(boolean resubscribeIfSessionPresent) Instructs the client to automatically restore its subscriptions when reconnected successfully and the session is still present.Fluent counterpart oftransportConfig(MqttClientTransportConfig).transportConfig(@NotNull MqttClientTransportConfig transportConfig) Sets a different transport configuration the client will try to reconnect with.
-
Field Details
-
DEFAULT_RECONNECT
static final boolean DEFAULT_RECONNECTIf reconnect is enabled by default.- Since:
- 1.2
- See Also:
-
DEFAULT_RESUBSCRIBE_IF_SESSION_PRESENT
static final boolean DEFAULT_RESUBSCRIBE_IF_SESSION_PRESENTIf resubscribe when the session is present when the client reconnected successfully is enabled by default.- Since:
- 1.3.7
- See Also:
-
DEFAULT_RESUBSCRIBE_IF_SESSION_EXPIRED
static final boolean DEFAULT_RESUBSCRIBE_IF_SESSION_EXPIREDIf resubscribe when the session expired before the client reconnected successfully is enabled by default.- Since:
- 1.2
- See Also:
-
DEFAULT_REPUBLISH_IF_SESSION_EXPIRED
static final boolean DEFAULT_REPUBLISH_IF_SESSION_EXPIREDIf republish when the session expired before the client reconnected successfully is enabled by default.- Since:
- 1.2
- See Also:
-
DEFAULT_DELAY_MS
static final long DEFAULT_DELAY_MSDefault delay in milliseconds the client will wait for before trying to reconnect.- Since:
- 1.2
- See Also:
-
-
Method Details
-
getAttempts
int getAttempts()- Returns:
- the number of failed connection attempts.
-
reconnect
Instructs the client to reconnect or not.- Parameters:
reconnect- whether to reconnect.- Returns:
- this reconnector.
-
reconnectWhen
@NotNull <T> @NotNull MqttClientReconnector reconnectWhen(@NotNull @NotNull CompletableFuture<T> future, @Nullable @Nullable BiConsumer<? super T, ? super Throwable> callback) Instructs the client to reconnect after a future completes.If additionally a
delayis supplied, the client will reconnect after both are complete.This method must only be called in
MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext)and not in the supplied callback.- Type Parameters:
T- the result type of the future.- Parameters:
future- the client will reconnect only after the future completes.callback- the callback that will be called after the future completes and before the client will reconnect. It can be used to set new connect properties (e.g. credentials).- Returns:
- this reconnector.
- Throws:
UnsupportedOperationException- if called outside ofMqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).
-
isReconnect
boolean isReconnect()- Returns:
- whether the client will reconnect.
-
resubscribeIfSessionPresent
@NotNull @NotNull MqttClientReconnector resubscribeIfSessionPresent(boolean resubscribeIfSessionPresent) Instructs the client to automatically restore its subscriptions when reconnected successfully and the session is still present.When the client reconnected successfully and its session is still present, the server still knows its subscriptions, so resubscribing is optional.
This setting only has effect if the client will reconnect (at least one of the methods
reconnect(boolean)orreconnectWhen(CompletableFuture, BiConsumer)is called).This method must only be called in
MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext)and not in the callback supplied toreconnectWhen(CompletableFuture, BiConsumer).- Parameters:
resubscribeIfSessionPresent- whether to resubscribe if the session is present when the client reconnected successfully.- Returns:
- this reconnector.
- Throws:
UnsupportedOperationException- if called outside ofMqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).- Since:
- 1.3.7
-
isResubscribeIfSessionPresent
boolean isResubscribeIfSessionPresent()- Returns:
- whether the client will resubscribe if the session is present when it reconnects successfully.
- Since:
- 1.3.7
-
resubscribeIfSessionExpired
Instructs the client to automatically restore its subscriptions when the session expired before it reconnected successfully.When the client reconnected successfully and its session is still present, the server still knows its subscriptions and they do not need to be restored.
This setting only has effect if the client will reconnect (at least one of the methods
reconnect(boolean)orreconnectWhen(CompletableFuture, BiConsumer)is called).This method must only be called in
MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext)and not in the callback supplied toreconnectWhen(CompletableFuture, BiConsumer).- Parameters:
resubscribe- whether to resubscribe when the session expired before the client reconnected successfully.- Returns:
- this reconnector.
- Throws:
UnsupportedOperationException- if called outside ofMqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).- Since:
- 1.2
-
isResubscribeIfSessionExpired
boolean isResubscribeIfSessionExpired()- Returns:
- whether the client will resubscribe when the session expired before it reconnected successfully.
- Since:
- 1.2
-
republishIfSessionExpired
Instructs the client to queue pending Publish messages and automatically publish them even if the session expired before reconnected successfully.When the client reconnected successfully and its session is still present, the client will always queue pending Publish messages and automatically publish them to ensure the QoS guarantees.
This setting only has effect if the client will reconnect (at least one of the methods
reconnect(boolean)orreconnectWhen(CompletableFuture, BiConsumer)is called).This method must only be called in
MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext)and not in the callback supplied toreconnectWhen(CompletableFuture, BiConsumer).- Parameters:
republish- whether to republish when the session expired before the client reconnected successfully.- Returns:
- this reconnector.
- Throws:
UnsupportedOperationException- if called outside ofMqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).- Since:
- 1.2
-
isRepublishIfSessionExpired
boolean isRepublishIfSessionExpired()- Returns:
- whether the client will republish when the session expired before it reconnected successfully.
- Since:
- 1.2
-
delay
Sets a delay the client will wait for before trying to reconnect.This setting only has effect if the client will reconnect (at least one of the methods
reconnect(boolean)orreconnectWhen(CompletableFuture, BiConsumer)is called).If additionally a
futureis supplied, the client will reconnect after both are complete.This method must only be called in
MqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext)and not in the callback supplied toreconnectWhen(CompletableFuture, BiConsumer).- Parameters:
delay- delay which the client will wait before trying to reconnect.timeUnit- the time unit of the delay.- Returns:
- this reconnector.
- Throws:
UnsupportedOperationException- if called outside ofMqttClientDisconnectedListener.onDisconnected(MqttClientDisconnectedContext).
-
getDelay
Returns the currently set delay the client will wait for before trying to reconnect.If the
delay(long, TimeUnit)method has not been called before (including previousMqttClientDisconnectedListeners) it will beDEFAULT_DELAY_MS.- Parameters:
timeUnit- the time unit of the returned delay.- Returns:
- the delay in the given time unit.
-
transportConfig
@NotNull @NotNull MqttClientReconnector transportConfig(@NotNull @NotNull MqttClientTransportConfig transportConfig) Sets a different transport configuration the client will try to reconnect with.- Parameters:
transportConfig- the transport configuration the client will try to reconnect with.- Returns:
- this reconnector.
-
transportConfig
@CheckReturnValue @NotNull MqttClientTransportConfigBuilder.Nested<? extends MqttClientReconnector> transportConfig()Fluent counterpart oftransportConfig(MqttClientTransportConfig).Calling
MqttClientTransportConfigBuilder.Nested.applyTransportConfig()on the returned builder has the effect of extending the current transport configuration.- Returns:
- the fluent builder for the transport configuration.
- See Also:
-
getTransportConfig
Returns the currently set transport configuration the client will try to reconnect with.If the
transportConfig(MqttClientTransportConfig)method has not been called before (including previousMqttClientDisconnectedListeners) it will be the transport configuration the client was connected with or thedefault transport configurationif it has not been connected yet.- Returns:
- the transport configuration.
-