public static class Options.Builder
extends java.lang.Object
A common usage for testing might be new Options.Builder().server(myserverurl).noReconnect.build()
Constructor and Description |
---|
Builder()
Constructs a new Builder with the default values.
|
Builder(Options o) |
Builder(java.util.Properties props)
Constructs a new
Builder from a Properties object. |
Builder(java.lang.String propertiesFilePath)
Constructs a new
Builder from a file that contains properties. |
Modifier and Type | Method and Description |
---|---|
Options.Builder |
authHandler(AuthHandler handler)
Set the
AuthHandler to sign the server nonce for authentication in
nonce-mode. |
Options.Builder |
bufferSize(int size)
Sets the initial size for buffers in the connection, primarily for testing.
|
Options |
build()
Build an Options object from this Builder.
|
Options.Builder |
clientSideLimitChecks(boolean checks)
Set client side limit checks.
|
Options.Builder |
connectionListener(ConnectionListener listener)
Set the
ConnectionListener to receive asynchronous notifications of disconnect
events. |
Options.Builder |
connectionName(java.lang.String name)
Set the connection's optional Name.
|
Options.Builder |
connectionTimeout(java.time.Duration connectionTimeout)
Set the timeout for connection attempts.
|
Options.Builder |
connectionTimeout(long connectionTimeoutMillis)
Set the timeout for connection attempts.
|
Options.Builder |
credentialPath(java.lang.String credentialPath) |
Options.Builder |
dataPortType(java.lang.String dataPortClassName)
The class to use for this connections data port.
|
Options.Builder |
discardMessagesWhenOutgoingQueueFull()
Enable discard messages when the outgoing queue full.
|
Options.Builder |
dispatcherFactory(io.nats.client.impl.DispatcherFactory dispatcherFactory)
Set the DispatcherFactory implementation for connections to use instead of the default implementation
|
Options.Builder |
errorListener(ErrorListener listener)
Set the
ErrorListener to receive asynchronous error events related to this
connection. |
Options.Builder |
executor(java.util.concurrent.ExecutorService executor)
Set the
ExecutorService used to run threaded tasks. |
Options.Builder |
httpRequestInterceptor(java.util.function.Consumer<HttpRequest> interceptor)
Add an HttpRequest interceptor which can be used to modify the HTTP request when using websockets
|
Options.Builder |
httpRequestInterceptors(java.util.Collection<? extends java.util.function.Consumer<HttpRequest>> interceptors)
Overwrite the list of HttpRequest interceptors which can be used to modify the HTTP request when using websockets
|
Options.Builder |
ignoreDiscoveredServers()
Turn off use of discovered servers when connecting / reconnecting.
|
Options.Builder |
inboxPrefix(java.lang.String prefix)
Set the connection's inbox prefix.
|
Options.Builder |
keystorePassword(char[] keystorePassword) |
Options.Builder |
keystorePath(java.lang.String keystore) |
Options.Builder |
maxControlLine(int bytes)
Set the maximum length of a control line sent by this connection.
|
Options.Builder |
maxMessagesInOutgoingQueue(int maxMessagesInOutgoingQueue)
Set the maximum number of messages in the outgoing queue.
|
Options.Builder |
maxPingsOut(int max)
Set the maximum number of pings the client can have in flight.
|
Options.Builder |
maxReconnects(int max)
Set the maximum number of reconnect attempts.
|
Options.Builder |
noEcho()
Turn off echo.
|
Options.Builder |
noHeaders()
Turn off header support.
|
Options.Builder |
noNoResponders()
Turn off noresponder support.
|
Options.Builder |
noRandomize()
For the default server list provider, turn off server pool randomization.
|
Options.Builder |
noReconnect()
Equivalent to calling maxReconnects with 0,
maxReconnects . |
Options.Builder |
noResolveHostnames()
For the default server list provider, whether to resolve hostnames when building server list.
|
Options.Builder |
oldRequestStyle()
Turn on the old request style that uses a new inbox and subscriber for each
request.
|
Options.Builder |
opentls()
Set the options to use an SSL context that accepts any server certificate and has no client certificates.
|
Options.Builder |
pedantic()
Turn on pedantic mode for the server, in relation to this connection.
|
Options.Builder |
pingInterval(java.time.Duration time)
Set the interval between attempts to pings the server.
|
Options.Builder |
properties(java.util.Properties props)
Add settings defined in the properties object
|
Options.Builder |
proxy(java.net.Proxy proxy)
Define a proxy to use when connecting.
|
Options.Builder |
reconnectBufferSize(long size)
Set the maximum number of bytes to buffer in the client when trying to
reconnect.
|
Options.Builder |
reconnectDelayHandler(ReconnectDelayHandler handler)
Set the
ReconnectDelayHandler for custom reconnect duration |
Options.Builder |
reconnectJitter(java.time.Duration time)
Set the jitter time to wait between reconnect attempts to the same server.
|
Options.Builder |
reconnectJitterTls(java.time.Duration time)
Set the jitter time for a tls/secure connection to wait between reconnect attempts to the same server.
|
Options.Builder |
reconnectWait(java.time.Duration time)
Set the time to wait between reconnect attempts to the same server.
|
Options.Builder |
reportNoResponders() |
Options.Builder |
requestCleanupInterval(java.time.Duration time)
Set the interval between cleaning passes on outstanding request futures that are cancelled or timeout
in the application code.
|
Options.Builder |
secure()
Sets the options to use the default SSL Context, if it exists.
|
Options.Builder |
server(java.lang.String serverURL)
Add a server to the list of known servers.
|
Options.Builder |
serverPool(ServerPool serverPool)
Set the ServerPool implementation for connections to use instead of the default implementation
|
Options.Builder |
servers(java.lang.String[] servers)
Add an array of servers to the list of known servers.
|
Options.Builder |
socketReadTimeoutMillis(int socketReadTimeoutMillis)
Set the timeout to use around socket reads
|
Options.Builder |
socketSoLinger(int socketSoLinger)
Set the value of the socket SO LINGER property in seconds.
|
Options.Builder |
socketWriteTimeout(java.time.Duration socketWriteTimeout)
Set the timeout to use around socket writes
|
Options.Builder |
socketWriteTimeout(long socketWriteTimeoutMillis)
Set the timeout to use around socket writes
|
Options.Builder |
sslContext(javax.net.ssl.SSLContext ctx)
Set the SSL context, requires that the server supports TLS connections and
the URI specifies TLS.
|
Options.Builder |
sslContextFactory(io.nats.client.impl.SSLContextFactory sslContextFactory)
Set the factory that provides the ssl context.
|
Options.Builder |
statisticsCollector(StatisticsCollector collector)
Set the
StatisticsCollector to collect connection metrics. |
Options.Builder |
supportUTF8Subjects()
The client protocol is not clear about the encoding for subject names.
|
Options.Builder |
timeTraceLogger(TimeTraceLogger logger)
Set the
TimeTraceLogger to receive trace events related to this connection. |
Options.Builder |
tlsAlgorithm(java.lang.String tlsAlgorithm) |
Options.Builder |
tlsFirst()
Set TLS Handshake First behavior on.
|
Options.Builder |
token(char[] token)
Set the token for token-based authentication.
|
Options.Builder |
token(java.lang.String token)
Deprecated.
use the char[] version instead for better security
|
Options.Builder |
traceConnection()
Enable connection trace messages.
|
Options.Builder |
truststorePassword(char[] truststorePassword) |
Options.Builder |
truststorePath(java.lang.String truststore) |
Options.Builder |
turnOnAdvancedStats()
Turn on advanced stats, primarily for test/benchmarks.
|
Options.Builder |
useDispatcherWithExecutor()
Instruct dispatchers to dispatch all messages as a task, instead of directly from dispatcher thread
|
Options.Builder |
userInfo(char[] userName,
char[] password)
Set the username and password for basic authentication.
|
Options.Builder |
userInfo(java.lang.String userName,
java.lang.String password)
Set the username and password for basic authentication.
|
Options.Builder |
useTimeoutException()
Throw
TimeoutException on timeout instead of CancellationException ? |
Options.Builder |
verbose()
Turn on verbose mode with the server.
|
public Builder()
When build()
is called on a default builder it will add the default url
to its list of servers if there were no servers defined.
public Builder(java.util.Properties props) throws java.lang.IllegalArgumentException
Builder
from a Properties
object.
Methods called on the builder after construction can override the properties.
props
- the Properties
objectjava.lang.IllegalArgumentException
public Builder(java.lang.String propertiesFilePath) throws java.io.IOException
Builder
from a file that contains properties.propertiesFilePath
- a resolvable path to a file from the location the application is running, either relative or absolutejava.io.IOException
- if the properties file cannot be found, opened or readpublic Builder(Options o)
public Options.Builder properties(java.util.Properties props)
props
- the properties objectjava.lang.IllegalArgumentException
- if the properties object is nullpublic Options.Builder server(java.lang.String serverURL)
serverURL
- the URL for the server to addjava.lang.IllegalArgumentException
- if the url is not formatted correctly.public Options.Builder servers(java.lang.String[] servers)
servers
- A list of server URIsjava.lang.IllegalArgumentException
- if any url is not formatted correctly.public Options.Builder oldRequestStyle()
public Options.Builder noRandomize()
public Options.Builder noResolveHostnames()
public Options.Builder reportNoResponders()
public Options.Builder noEcho()
public Options.Builder noHeaders()
public Options.Builder noNoResponders()
public Options.Builder clientSideLimitChecks(boolean checks)
checks
- the checks flagpublic Options.Builder supportUTF8Subjects()
public Options.Builder connectionName(java.lang.String name)
name
- the connections new name.public Options.Builder inboxPrefix(java.lang.String prefix)
prefix
- prefix to use.public Options.Builder verbose()
public Options.Builder pedantic()
public Options.Builder turnOnAdvancedStats()
Statistics
object.public Options.Builder traceConnection()
public Options.Builder secure() throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmException
- Not thrown, deferred to build() method, left in for backward compatibilitypublic Options.Builder opentls() throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmException
- Not thrown, deferred to build() method, left in for backward compatibilitypublic Options.Builder sslContext(javax.net.ssl.SSLContext ctx)
ctx
- the SSL Context to use for TLS connectionspublic Options.Builder sslContextFactory(io.nats.client.impl.SSLContextFactory sslContextFactory)
sslContextFactory
- the SSL Context for use to create a ssl contextpublic Options.Builder keystorePath(java.lang.String keystore)
keystore
- the path to the keystore filepublic Options.Builder keystorePassword(char[] keystorePassword)
keystorePassword
- the password for the keystorepublic Options.Builder truststorePath(java.lang.String truststore)
truststore
- the path to the trust store filepublic Options.Builder truststorePassword(char[] truststorePassword)
truststorePassword
- the password for the trust storepublic Options.Builder tlsAlgorithm(java.lang.String tlsAlgorithm)
tlsAlgorithm
- the tls algorithm. Default is public Options.Builder credentialPath(java.lang.String credentialPath)
credentialPath
- the path to the credentials file for creating an AuthHandler
public Options.Builder noReconnect()
maxReconnects
.public Options.Builder maxReconnects(int max)
The reconnect count is incremented on a per-server basis, so if the server list contains 5 servers but max reconnects is set to 3, only 3 of those servers will be tried.
This library has a slight difference from some NATS clients, if you set the maxReconnects to zero there will not be any reconnect attempts, regardless of the number of known servers.
The reconnect state is entered when the connection is connected and loses
that connection. During the initial connection attempt, the client will cycle over
its server list one time, regardless of what maxReconnects is set to. The only exception
to this is the experimental async connect method connectAsynchronously
.
max
- the maximum reconnect attemptspublic Options.Builder reconnectWait(java.time.Duration time)
time
- the time to waitpublic Options.Builder reconnectJitter(java.time.Duration time)
time
- the time to waitpublic Options.Builder reconnectJitterTls(java.time.Duration time)
time
- the time to waitpublic Options.Builder maxControlLine(int bytes)
bytes
- the max byte countpublic Options.Builder connectionTimeout(java.time.Duration connectionTimeout)
connectionTimeout
- the time to waitpublic Options.Builder connectionTimeout(long connectionTimeoutMillis)
connectionTimeoutMillis
- the time to wait in millisecondspublic Options.Builder socketReadTimeoutMillis(int socketReadTimeoutMillis)
socketReadTimeoutMillis
- the timeout millisecondspublic Options.Builder socketWriteTimeout(long socketWriteTimeoutMillis)
socketWriteTimeoutMillis
- the timeout millisecondspublic Options.Builder socketWriteTimeout(java.time.Duration socketWriteTimeout)
socketWriteTimeout
- the timeout durationpublic Options.Builder socketSoLinger(int socketSoLinger)
socketSoLinger
- the number of seconds to lingerpublic Options.Builder pingInterval(java.time.Duration time)
maxPingsOut()
. As of 2.4.4 the library
may wait up to 2 * time to send a ping. Incoming traffic from the server can postpone
the next ping to avoid pings taking up bandwidth during busy messaging.
Keep in mind that a ping requires a round trip to the server. Setting this value to a small
number can result in quick failures due to maxPingsOut being reached, these failures will
force a disconnect/reconnect which can result in messages being held back or failed. In general,
the ping interval should be set in seconds but this value is not enforced as it would result in
an API change from the 2.0 release.time
- the time between client to server pingspublic Options.Builder requestCleanupInterval(java.time.Duration time)
The default value is probably reasonable, but this interval is useful in a very noisy network situation where lots of requests are used.
time
- the cleaning intervalpublic Options.Builder maxPingsOut(int max)
max
- the max pingspublic Options.Builder bufferSize(int size)
size
- the size in bytes to make buffers for connections created with this optionspublic Options.Builder reconnectBufferSize(long size)
Statistics
.
A value of zero will disable the reconnect buffer, a value less than zero means unlimited. Caution
should be used for negative numbers as they can result in an unreliable network connection plus a
high message rate leading to an out of memory error.size
- the size in bytespublic Options.Builder userInfo(java.lang.String userName, java.lang.String password)
userName
- a non-empty userNamepassword
- the password, in plain textpublic Options.Builder userInfo(char[] userName, char[] password)
userName
- a non-empty userNamepassword
- the password, in plain text@Deprecated public Options.Builder token(java.lang.String token)
token
- The tokenpublic Options.Builder token(char[] token)
token
- The tokenpublic Options.Builder authHandler(AuthHandler handler)
AuthHandler
to sign the server nonce for authentication in
nonce-mode.handler
- The new AuthHandler for this connection.public Options.Builder reconnectDelayHandler(ReconnectDelayHandler handler)
ReconnectDelayHandler
for custom reconnect durationhandler
- The new ReconnectDelayHandler for this connection.public Options.Builder errorListener(ErrorListener listener)
ErrorListener
to receive asynchronous error events related to this
connection.listener
- The new ErrorListener for this connection.public Options.Builder timeTraceLogger(TimeTraceLogger logger)
TimeTraceLogger
to receive trace events related to this connection.logger
- The new TimeTraceLogger for this connection.public Options.Builder connectionListener(ConnectionListener listener)
ConnectionListener
to receive asynchronous notifications of disconnect
events.listener
- The new ConnectionListener for this type of event.public Options.Builder statisticsCollector(StatisticsCollector collector)
StatisticsCollector
to collect connection metrics.
If not set, then a default implementation will be used.
collector
- the new StatisticsCollector for this connection.public Options.Builder executor(java.util.concurrent.ExecutorService executor)
ExecutorService
used to run threaded tasks. The default is a
cached thread pool that names threads after the connection name (or a default). This executor
is used for reading and writing the underlying sockets as well as for each Dispatcher.
The default executor uses a short keepalive time, 500ms, to insure quick shutdowns. This is reasonable
since most threads from the executor are long-lived. If you customize, be sure to keep the shutdown
effect in mind, executors can block for their keepalive time. The default executor also marks threads
with priority normal and as non-daemon.executor
- The ExecutorService to use for connections built with these options.public Options.Builder httpRequestInterceptor(java.util.function.Consumer<HttpRequest> interceptor)
interceptor
- The interceptorpublic Options.Builder httpRequestInterceptors(java.util.Collection<? extends java.util.function.Consumer<HttpRequest>> interceptors)
interceptors
- The list of interceptorspublic Options.Builder proxy(java.net.Proxy proxy)
proxy
- is the HTTP or socks proxy to use.public Options.Builder dataPortType(java.lang.String dataPortClassName)
dataPortClassName
- a valid and accessible class namepublic Options.Builder maxMessagesInOutgoingQueue(int maxMessagesInOutgoingQueue)
maxMessagesInOutgoingQueue
- the maximum number of messages in the outgoing queuepublic Options.Builder discardMessagesWhenOutgoingQueueFull()
maxMessagesInOutgoingQueue
public Options.Builder ignoreDiscoveredServers()
public Options.Builder tlsFirst()
public Options.Builder useTimeoutException()
TimeoutException
on timeout instead of CancellationException
?public Options.Builder useDispatcherWithExecutor()
public Options.Builder serverPool(ServerPool serverPool)
serverPool
- the implementationpublic Options.Builder dispatcherFactory(io.nats.client.impl.DispatcherFactory dispatcherFactory)
dispatcherFactory
- the implementationpublic Options build() throws java.lang.IllegalStateException
If the Options builder was not provided with a server, a default one will be included
Options.DEFAULT_URL
. If only a single server URI is included, the builder
will try a few things to make connecting easier:
nats://user:password@server:port
, they will be used.
nats://token@server:port
, it will be used.
secure()
.
java.lang.IllegalStateException
- if there is a conflict in the options, like a token and a user/pass