Class KlientConfigBuilder
-
- All Implemented Interfaces:
public final class KlientConfigBuilderBuilder for NatsKlient configuration
-
-
Field Summary
Fields Modifier and Type Field Description private Stringnameprivate List<String>serversprivate DurationreconnectWaitprivate IntegermaxReconnectsprivate IntegerreconnectBufferSizeprivate DurationconnectionTimeoutprivate DurationpingIntervalprivate DurationrequestCleanupIntervalprivate BooleanreconnectOnConnectprivate IntegermaxMessagesInOutgoingQueueprivate BooleandiscardMessagesWhenOutgoingQueueFullprivate CoroutineDispatcherioDispatcherprivate StringcredentialsPathprivate CharArraytokenprivate Stringusernameprivate CharArraypasswordprivate SSLContexttlsContextprivate ConnectionListenerconnectionListenerprivate ErrorListenererrorListener
-
Constructor Summary
Constructors Constructor Description KlientConfigBuilder()
-
Method Summary
Modifier and Type Method Description final StringgetName()final UnitsetName(String value)final List<String>getServers()final UnitsetServers(List<String> value)final DurationgetReconnectWait()final UnitsetReconnectWait(Duration value)final IntegergetMaxReconnects()final UnitsetMaxReconnects(Integer value)final IntegergetReconnectBufferSize()final UnitsetReconnectBufferSize(Integer value)final DurationgetConnectionTimeout()final UnitsetConnectionTimeout(Duration value)final DurationgetPingInterval()final UnitsetPingInterval(Duration value)final DurationgetRequestCleanupInterval()How often the connection reaps unanswered request inboxesA JetStream publish future has no per-call deadline of its own: jnats hands it a null timeout ( NatsJetStream.java:171) and falls back to this interval (NatsConnection.java:1484-1486).final UnitsetRequestCleanupInterval(Duration value)How often the connection reaps unanswered request inboxesA JetStream publish future has no per-call deadline of its own: jnats hands it a null timeout ( NatsJetStream.java:171) and falls back to this interval (NatsConnection.java:1484-1486).final BooleangetReconnectOnConnect()Whether NatsKlient.connect waits for a server that is not up yetWith the default false, connecting is a single pass over servers: it either succeeds or throws, which is what a fail-fast startup wants.final UnitsetReconnectOnConnect(Boolean value)Whether NatsKlient.connect waits for a server that is not up yetWith the default false, connecting is a single pass over servers: it either succeeds or throws, which is what a fail-fast startup wants.final IntegergetMaxMessagesInOutgoingQueue()How many messages may wait in the connection's outgoing queue nullleaves jnats' default (5000).final UnitsetMaxMessagesInOutgoingQueue(Integer value)How many messages may wait in the connection's outgoing queue nullleaves jnats' default (5000).final BooleangetDiscardMessagesWhenOutgoingQueueFull()Whether a publish that finds the outgoing queue full drops the message instead of waitingThe queue being full means the connection cannot write as fast as the application publishes. final UnitsetDiscardMessagesWhenOutgoingQueueFull(Boolean value)Whether a publish that finds the outgoing queue full drops the message instead of waitingThe queue being full means the connection cannot write as fast as the application publishes. final CoroutineDispatchergetIoDispatcher()The dispatcher every blocking NATS call runs onjnats is a blocking client: management calls, key-value and object-store operations, flushes and fetches all park a thread, so natsy moves them off the caller's. final UnitsetIoDispatcher(CoroutineDispatcher value)The dispatcher every blocking NATS call runs onjnats is a blocking client: management calls, key-value and object-store operations, flushes and fetches all park a thread, so natsy moves them off the caller's. final StringgetCredentialsPath()Path to a NATS credentials file - the .credsholding a user JWT and its NKey seedA path, not the credential material itself: jnats reads the file on every connect and reconnect, so a rotated file is picked up without rebuilding the klient.final UnitsetCredentialsPath(String value)Path to a NATS credentials file - the .credsholding a user JWT and its NKey seedA path, not the credential material itself: jnats reads the file on every connect and reconnect, so a rotated file is picked up without rebuilding the klient.final CharArraygetToken()Authentication token, held the way password is CharArrayrather thanStringso the secret can be zeroed after connecting; jnats takeschar[]here too and has deprecated itsStringoverload (io/nats/client/Options.java:1952).final UnitsetToken(CharArray value)Authentication token, held the way password is CharArrayrather thanStringso the secret can be zeroed after connecting; jnats takeschar[]here too and has deprecated itsStringoverload (io/nats/client/Options.java:1952).final StringgetUsername()final UnitsetUsername(String value)final CharArraygetPassword()Password for username, held the way token is final UnitsetPassword(CharArray value)Password for username, held the way token is final SSLContextgetTlsContext()final UnitsetTlsContext(SSLContext value)final ConnectionListenergetConnectionListener()final UnitsetConnectionListener(ConnectionListener value)final ErrorListenergetErrorListener()final UnitsetErrorListener(ErrorListener value)final Unitcodecs(Function1<CodecRegistry.Builder, Unit> block)Override how a type is carried, or configure the shared kotlinx.serialization.json.JsonNothing has to be registered: any @Serializabletype is carried as JSON without appearing here.final Unitjetstream(Function1<JetStreamConfigBuilder, Unit> block)Configure JetStream settings final UnitjnatsOptions(Function1<Options.Builder, Unit> customize)Reach a jnats option this DSL does not model Options.Buildercarries around sixty settings; the properties above are the ones natsy has an opinion about.final KlientConfigbuild()Assemble the configurationPublic because the configuration and the client are built in different places: a Ktor plugin or a Koin module reads settings from a file, fills this builder, and hands the result to NatsKlient. -
-
Method Detail
-
getServers
final List<String> getServers()
-
setServers
final Unit setServers(List<String> value)
-
getReconnectWait
final Duration getReconnectWait()
-
setReconnectWait
final Unit setReconnectWait(Duration value)
-
getMaxReconnects
final Integer getMaxReconnects()
-
setMaxReconnects
final Unit setMaxReconnects(Integer value)
-
getReconnectBufferSize
final Integer getReconnectBufferSize()
-
setReconnectBufferSize
final Unit setReconnectBufferSize(Integer value)
-
getConnectionTimeout
final Duration getConnectionTimeout()
-
setConnectionTimeout
final Unit setConnectionTimeout(Duration value)
-
getPingInterval
final Duration getPingInterval()
-
setPingInterval
final Unit setPingInterval(Duration value)
-
getRequestCleanupInterval
final Duration getRequestCleanupInterval()
How often the connection reaps unanswered request inboxes
A JetStream publish future has no per-call deadline of its own: jnats hands it a null timeout (
NatsJetStream.java:171) and falls back to this interval (NatsConnection.java:1484-1486). natsy therefore raises whatever is set here to at least connectionTimeout and the JetStreamrequestTimeout, so a publish can keep the deadline those knobs report. A jnatsOptions customizer still wins if it sets a shorter interval; the publish deadline is then shortened to match.
-
setRequestCleanupInterval
final Unit setRequestCleanupInterval(Duration value)
How often the connection reaps unanswered request inboxes
A JetStream publish future has no per-call deadline of its own: jnats hands it a null timeout (
NatsJetStream.java:171) and falls back to this interval (NatsConnection.java:1484-1486). natsy therefore raises whatever is set here to at least connectionTimeout and the JetStreamrequestTimeout, so a publish can keep the deadline those knobs report. A jnatsOptions customizer still wins if it sets a shorter interval; the publish deadline is then shortened to match.
-
getReconnectOnConnect
final Boolean getReconnectOnConnect()
Whether NatsKlient.connect waits for a server that is not up yet
With the default
false, connecting is a single pass over servers: it either succeeds or throws, which is what a fail-fast startup wants.With
truenatsy uses jnats'connectReconnectOnConnect, which treats the initial connection like any other and hands it to the reconnect loop. That loop runs on the calling thread, sleeping reconnectWait between rounds until it connects or runs out of attempts - and the attempt budget is maxReconnects, whose default of-1means unlimited. So with the defaults this turns NatsKlient.connect from "connected or throws" into "connected or blocks forever". Set a finite maxReconnects alongside it if startup must not hang.Worth it when the broker legitimately starts after the application - compose, or a pod that comes up before the NATS service.
-
setReconnectOnConnect
final Unit setReconnectOnConnect(Boolean value)
Whether NatsKlient.connect waits for a server that is not up yet
With the default
false, connecting is a single pass over servers: it either succeeds or throws, which is what a fail-fast startup wants.With
truenatsy uses jnats'connectReconnectOnConnect, which treats the initial connection like any other and hands it to the reconnect loop. That loop runs on the calling thread, sleeping reconnectWait between rounds until it connects or runs out of attempts - and the attempt budget is maxReconnects, whose default of-1means unlimited. So with the defaults this turns NatsKlient.connect from "connected or throws" into "connected or blocks forever". Set a finite maxReconnects alongside it if startup must not hang.Worth it when the broker legitimately starts after the application - compose, or a pod that comes up before the NATS service.
-
getMaxMessagesInOutgoingQueue
final Integer getMaxMessagesInOutgoingQueue()
How many messages may wait in the connection's outgoing queue
nullleaves jnats' default (5000). The queue is what makes publishing look instantaneous: a publish hands the message over and returns. When it is full, publishing blocks until space appears, and then fails - see discardMessagesWhenOutgoingQueueFull for the other way out of that.
-
setMaxMessagesInOutgoingQueue
final Unit setMaxMessagesInOutgoingQueue(Integer value)
How many messages may wait in the connection's outgoing queue
nullleaves jnats' default (5000). The queue is what makes publishing look instantaneous: a publish hands the message over and returns. When it is full, publishing blocks until space appears, and then fails - see discardMessagesWhenOutgoingQueueFull for the other way out of that.
-
getDiscardMessagesWhenOutgoingQueueFull
final Boolean getDiscardMessagesWhenOutgoingQueueFull()
Whether a publish that finds the outgoing queue full drops the message instead of waiting
The queue being full means the connection cannot write as fast as the application publishes. By default jnats waits for room and then throws, which natsy reports as a eu.vstoyanov.natsy.exception.NatsyConnectionException - correct for a publisher that must not lose messages, but it means a publish can block the calling thread for seconds.
Set this for telemetry-shaped traffic, where dropping the newest sample beats stalling the caller. It is the only way to make publishing truly non-blocking.
-
setDiscardMessagesWhenOutgoingQueueFull
final Unit setDiscardMessagesWhenOutgoingQueueFull(Boolean value)
Whether a publish that finds the outgoing queue full drops the message instead of waiting
The queue being full means the connection cannot write as fast as the application publishes. By default jnats waits for room and then throws, which natsy reports as a eu.vstoyanov.natsy.exception.NatsyConnectionException - correct for a publisher that must not lose messages, but it means a publish can block the calling thread for seconds.
Set this for telemetry-shaped traffic, where dropping the newest sample beats stalling the caller. It is the only way to make publishing truly non-blocking.
-
getIoDispatcher
final CoroutineDispatcher getIoDispatcher()
The dispatcher every blocking NATS call runs on
jnats is a blocking client: management calls, key-value and object-store operations, flushes and fetches all park a thread, so natsy moves them off the caller's. Dispatchers.IO is the right default; replace it to isolate NATS traffic from the rest of the application (
Dispatchers.IO.limitedParallelism(n)), to run on virtual threads, or to make a test deterministic.
-
setIoDispatcher
final Unit setIoDispatcher(CoroutineDispatcher value)
The dispatcher every blocking NATS call runs on
jnats is a blocking client: management calls, key-value and object-store operations, flushes and fetches all park a thread, so natsy moves them off the caller's. Dispatchers.IO is the right default; replace it to isolate NATS traffic from the rest of the application (
Dispatchers.IO.limitedParallelism(n)), to run on virtual threads, or to make a test deterministic.
-
getCredentialsPath
final String getCredentialsPath()
Path to a NATS credentials file - the
.credsholding a user JWT and its NKey seedA path, not the credential material itself: jnats reads the file on every connect and reconnect, so a rotated file is picked up without rebuilding the klient.
-
setCredentialsPath
final Unit setCredentialsPath(String value)
Path to a NATS credentials file - the
.credsholding a user JWT and its NKey seedA path, not the credential material itself: jnats reads the file on every connect and reconnect, so a rotated file is picked up without rebuilding the klient.
-
getToken
final CharArray getToken()
Authentication token, held the way password is
CharArrayrather thanStringso the secret can be zeroed after connecting; jnats takeschar[]here too and has deprecated itsStringoverload (io/nats/client/Options.java:1952).
-
setToken
final Unit setToken(CharArray value)
Authentication token, held the way password is
CharArrayrather thanStringso the secret can be zeroed after connecting; jnats takeschar[]here too and has deprecated itsStringoverload (io/nats/client/Options.java:1952).
-
getUsername
final String getUsername()
-
setUsername
final Unit setUsername(String value)
-
getPassword
final CharArray getPassword()
-
setPassword
final Unit setPassword(CharArray value)
-
getTlsContext
final SSLContext getTlsContext()
-
setTlsContext
final Unit setTlsContext(SSLContext value)
-
getConnectionListener
final ConnectionListener getConnectionListener()
-
setConnectionListener
final Unit setConnectionListener(ConnectionListener value)
-
getErrorListener
final ErrorListener getErrorListener()
-
setErrorListener
final Unit setErrorListener(ErrorListener value)
-
codecs
final Unit codecs(Function1<CodecRegistry.Builder, Unit> block)
Override how a type is carried, or configure the shared kotlinx.serialization.json.Json
Nothing has to be registered: any
@Serializabletype is carried as JSON without appearing here. See CodecRegistry.Blocks accumulate rather than replace, so a second
codecs { }- from a shared configuration helper, say - adds to the first instead of silently discarding it. This matches CodecRegistry.Builder.configureJson, which composes for the same reason.
-
jetstream
final Unit jetstream(Function1<JetStreamConfigBuilder, Unit> block)
Configure JetStream settings
-
jnatsOptions
final Unit jnatsOptions(Function1<Options.Builder, Unit> customize)
Reach a jnats option this DSL does not model
Options.Buildercarries around sixty settings; the properties above are the ones natsy has an opinion about. Everything else -inboxPrefix, anAuthHandlerfor NKey or JWT,keystorePath,socketWriteTimeout,tlsFirst, a customExecutorService- is reachable here rather than being unreachable until natsy grows a property for it.Applied last, after everything else this builder sets, so it can also override those. Blocks accumulate, like codecs.
jnatsOptions { inboxPrefix("_INBOX.orders.") socketWriteTimeout(java.time.Duration.ofSeconds(5)) }
-
build
final KlientConfig build()
Assemble the configuration
Public because the configuration and the client are built in different places: a Ktor plugin or a Koin module reads settings from a file, fills this builder, and hands the result to NatsKlient. Construction validates, so a bad configuration fails here rather than at the first message.
-
-
-
-