public interface SessionFactory
sessions
.
Each instance is immutable, and has a number of session attributes that
determine how sessions should be established. An initial instance of this
factory with default values for all attributes can be obtained from
Diffusion.sessions()
. Methods such as inputBufferSize(int)
or maximumMessageSize(int)
that allow attributes to be changed do
not modify this instance. Each method returns a copy of this factory, with a
single changed attribute.
A new session can be created using openAsync(String)
specifying a
URL that identifies the server. This is a non-blocking method that returns
immediately, and provides the result asynchronously using a
CompletableFuture
.
open(String)
is a blocking alternative to openAsync
that
waits for a session to be established before returning.
There are two further non-blocking alternatives,
open(String, OpenCallback)
and
open(String, Object, OpenContextCallback)
that provide the result
asynchronously using a callback. These callback methods were part of the
product before CompletableFuture
became available with Java 8.
The overloaded methods openAsync()
or open()
use the
session factory attributes to locate a server rather than a URL. The server
is identified by the serverHost(String)
and
serverPort(int)
, requestPath(String)
,
secureTransport(boolean)
attributes. The
transports(SessionAttributes.Transport...)
attributes allows the
configuration of multiple transports for cascading.
If a URL is specified, it takes precedence over the serverHost
,
serverPort
, requestPath
, and secureTransport
session
factory attributes. A URL can only be used to configure a single transport
and cannot be used to configure cascading, so the transports
attribute is ignored.
URLs should take the form scheme://host:port, where scheme is chosen from the following table and determines the transport protocol used to send Diffusion messages.
Scheme | Transport Protocol |
---|---|
ws |
WebSocket. See RFC 6455 . |
wss |
WebSocket over TLS. |
xhr |
HTTP polling. |
xhrs |
HTTP polling over TLS. |
We recommend using the WebSocket protocol options (ws
or
wss
).
TLS is Transport Layer Security, commonly known as SSL. TLS-based protocols use cryptography to provide transport-level privacy, authentication, and integrity, and protects against network-sniffing and man-in-the-middle attacks. We recommend using the TLS variants for all communication. For a typical application, you should only consider not using TLS for unauthenticated ("anonymous") client sessions.
Modifier and Type | Interface and Description |
---|---|
static interface |
SessionFactory.OpenCallback
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
static interface |
SessionFactory.OpenContextCallback<C>
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
Modifier and Type | Method and Description |
---|---|
SessionFactory |
connectionTimeout(int timeout)
Sets the connection timeout.
|
SessionFactory |
credentials(Credentials credentials)
Set credentials.
|
SessionFactory |
customCredentials(byte[] bytes)
Set credentials of type
Credentials.Type.CUSTOM . |
SessionFactory |
errorHandler(Session.ErrorHandler errorHandler)
Nominates an error handler for a session.
|
SessionFactory |
httpProxy(String host,
int port)
Set the address of the HTTP Proxy that should be used to make connections
to the server.
|
SessionFactory |
httpProxy(String host,
int port,
HTTPProxyAuthentication authentication)
A variant of
httpProxy(String, int) that provides authentication
details to the HTTP proxy. |
SessionFactory |
initialRetryStrategy(RetryStrategy strategy)
Sets the initial retry strategy.
|
SessionFactory |
inputBufferSize(int size)
Sets the input buffer size for socket connection buffers and message
receiving buffers.
|
SessionFactory |
listener(Session.Listener listener)
Nominates a listener for session events from a session.
|
SessionFactory |
localSocketAddress(SocketAddress address)
Set the optional local socket address, used prior to connection.
|
SessionFactory |
maximumMessageSize(int size)
Set the maximum message size.
|
SessionFactory |
maximumQueueSize(int size)
Sets the maximum size of the outbound message queue for the connection.
|
SessionFactory |
noCredentials()
Set credentials to
Credentials.Type.NONE . |
SessionFactory |
noReconnection()
Disable reconnection.
|
Session |
open()
Open a connection to a server and request a new session.
|
<C> void |
open(C context,
SessionFactory.OpenContextCallback<C> callback)
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
void |
open(SessionFactory.OpenCallback callback)
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
Session |
open(String url)
Open a connection to a server and request a new client session.
|
<C> void |
open(String url,
C context,
SessionFactory.OpenContextCallback<C> callback)
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
void |
open(String url,
SessionFactory.OpenCallback callback)
Deprecated.
since 6.7
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead. |
CompletableFuture<Session> |
openAsync()
Open a connection to a server and request a new session.
|
CompletableFuture<Session> |
openAsync(String url)
Open a connection to a server and request a new session.
|
SessionFactory |
outputBufferSize(int size)
Sets the output buffer size for socket connection buffers and message
sending buffers.
|
SessionFactory |
password(String password)
Set credentials of type
Credentials.Type.PLAIN_PASSWORD . |
SessionFactory |
principal(String principal)
Sets the security principal.
|
SessionFactory |
properties(Map<String,String> properties)
Sets user-defined session property values.
|
SessionFactory |
property(String key,
String value)
Sets a user-defined session property value.
|
SessionFactory |
reconnectionStrategy(ReconnectionStrategy strategy)
Sets the reconnection strategy.
|
SessionFactory |
reconnectionTimeout(int timeout)
Sets the reconnection timeout.
|
SessionFactory |
recoveryBufferSize(int size)
Set the recovery buffer size.
|
SessionFactory |
requestPath(String requestPath)
Set the path used for HTTP requests.
|
SessionFactory |
secureTransport(boolean secureTransport)
Set if a secure transport should be used.
|
SessionFactory |
serverHost(String host)
Set the host name of the server to connect the session to.
|
SessionFactory |
serverPort(int port)
Set the port of the server to connect the session to.
|
SessionFactory |
sslContext(SSLContext context)
Sets the SSL Context if a secure connection is required.
|
SessionFactory |
transports(SessionAttributes.Transport... transports)
Set the transports to use to connect the session to the server.
|
SessionFactory |
writeTimeout(int timeout)
Sets the write timeout value for blocking writes.
|
SessionFactory listener(Session.Listener listener)
If not specified, there will be no session listener.
listener
- specifies a listener to be used for subsequent sessions
that are created. Null may be specified to remove any previously
specified listenerSessionFactory errorHandler(Session.ErrorHandler errorHandler)
If not specified, an instance of Session.ErrorHandler.Default
will be used.
errorHandler
- specifies the error handler to use for subsequent
sessions that are created. Null may be specified to remove any
previously specified handler and effectively revert to using the
default handlerSessionFactory principal(String principal)
By default this will be Session.ANONYMOUS
.
principal
- the principalSessionFactory noCredentials()
Credentials.Type.NONE
.
This is the default.
SessionFactory credentials(Credentials credentials)
The default is Credentials.Type.NONE
.
credentials
- the credentialsSessionFactory password(String password)
Credentials.Type.PLAIN_PASSWORD
.
The default is Credentials.Type.NONE
.
password
- the passwordSessionFactory customCredentials(byte[] bytes)
Credentials.Type.CUSTOM
.
The default is Credentials.Type.NONE
.
bytes
- the credentialsSessionFactory connectionTimeout(int timeout) throws IllegalArgumentException
This constrains the time taken to establish an initial connection to the
server. The write timeout
constrains the time
to send the connection request. The server is responsible for limiting
the overall time taken to complete a connection once it has received the
request.
The default is SessionAttributes.DEFAULT_CONNECTION_TIMEOUT
.
timeout
- the default connection timeout in milliseconds. If this
exceeds one hour (3600000ms) a warning will be logged and the
time-out will be set to one hour.IllegalArgumentException
- if an illegal timeout value is suppliedSessionFactory noReconnection()
This is equivalent to calling reconnectionTimeout(int)
with a timeout of 0
.
SessionFactory initialRetryStrategy(RetryStrategy strategy)
The strategy will determine whether a failure to open a session due to a
SessionEstablishmentTransientException
should be retried and if
so, at what interval and for how many attempts.
If no initial retry strategy is set there will be no attempt to retry after such a failure.
strategy
- the retry strategy to useSessionFactory reconnectionTimeout(int timeout) throws IllegalArgumentException
The default is SessionAttributes.DEFAULT_RECONNECTION_TIMEOUT
.
A timeout value of 0
or less is equivalent to to calling
noReconnection()
.
timeout
- the timeout duration to use when attempting to reconnect,
in millisecondsIllegalArgumentException
- if an illegal timeout value is suppliedSessionFactory reconnectionStrategy(ReconnectionStrategy strategy)
If the reconnection timeout has been set, but not a strategy, sessions will use a default strategy that attempts to reconnect on a regular basis of 5 seconds until the reconnection timeout is exceeded.
strategy
- the reconnection strategy to useSessionFactory inputBufferSize(int size) throws IllegalArgumentException
This controls both how much pending data the socket can accommodate and
how much available data will be read by the input tasks. In general
setting both this and outputBufferSize(int)
to the same
reasonable size will result in higher network throughput. The cost of the
receiving buffer is only incurred when actually reading data for
processing. The cost of the socket buffer is always incurred and may be
modified by the operating system.
The default is SessionAttributes.DEFAULT_INPUT_BUFFER_SIZE
.
size
- input buffer size in bytes. This may not be less than
SessionAttributes.MAXIMUM_MESSAGE_SIZE_MIN
.IllegalArgumentException
- if the input buffer size is invalidSessionFactory outputBufferSize(int size) throws IllegalArgumentException
This controls both how much pending data the socket can accommodate and
how much data will be batched by sending messages. In general setting
both this and inputBufferSize(int)
to the same reasonable size
will result in higher network throughput. The cost of the sending buffer
is only incurred when actually queuing data for output. The cost of the
socket buffer is always incurred and may be modified by the operating
system.
The default is SessionAttributes.DEFAULT_OUTPUT_BUFFER_SIZE
.
size
- output buffer size in bytes.IllegalArgumentException
- if the output buffer size is invalidSessionFactory localSocketAddress(SocketAddress address)
By default no local socket address is used.
address
- the local socket address. Setting this to null effectively
removes any previous settingSessionFactory sslContext(SSLContext context)
If not explicitly supplied, the default
context
will be used.
Setting the SSL Context will not enable transport layer security. It must
also be specified by the URL or secureTransport(boolean)
.
context
- the SSL Context to use when making the connectionSessionFactory writeTimeout(int timeout) throws IllegalArgumentException
Blocking writes are only used for the initial connection request. If it is not possible to complete the write within this time, the connection is closed.
The default is SessionAttributes.DEFAULT_WRITE_TIMEOUT
.
timeout
- the write timeout in milliseconds. If this exceeds one
hour (3600000ms) a warning will be logged and the time-out will be
set to one hour.IllegalArgumentException
- if timeout
is invalidSessionFactory maximumMessageSize(int size) throws IllegalArgumentException
This constrains the size of messages that will be accepted from the server and thus the size of any content that can be received. The limit is simply to protect against unexpectedly large messages.
By default, the size of received messages is unconstrained – see
SessionAttributes.DEFAULT_MAXIMUM_MESSAGE_SIZE
.
size
- the maximum message size in bytes. This must not be less than
SessionAttributes.MAXIMUM_MESSAGE_SIZE_MIN
IllegalArgumentException
- if the specified size
is invalidSessionFactory httpProxy(String host, int port) throws IllegalArgumentException
This allows connection to a server using HTTP CONNECT tunnelling through the specified proxy.
host
- the host name of the HTTP proxyport
- the port of the HTTP proxyIllegalArgumentException
- if the specified host
or
port
are invalidhttpProxy(String, int, HTTPProxyAuthentication)
SessionFactory httpProxy(String host, int port, HTTPProxyAuthentication authentication) throws IllegalArgumentException
httpProxy(String, int)
that provides authentication
details to the HTTP proxy.
This allows connection to a server using HTTP CONNECT tunnelling through the specified proxy, with authentication.
The authentication
parameter only affects communication with the
proxy, it is not passed on to the server.
Diffusion.proxyAuthentication()
and
ProxyAuthenticationFactory
can be used to create a suitable
instance. For example, to specify HTTP BASIC authentication with a user
name joe and password s3cret:
HTTPProxyAuthentication authentication = Diffusion.proxyAuthentication().basic("joe", "s3cret")
The principal(String)
and credentials(Credentials)
methods control authentication with the server and do not affect proxy
authentication.
host
- the host name of the HTTP proxyport
- the port of the HTTP proxyauthentication
- the authentication detailsIllegalArgumentException
- if the specified host
or
port
are invalidSessionFactory serverHost(String host) throws IllegalArgumentException
This value is only used if a URL is not provided when opening a session.
host
- the host name of the serverIllegalArgumentException
- if the specified host
is invalidSessionFactory serverPort(int port) throws IllegalArgumentException
This value is only used if a URL is not provided when opening a session. If the port is not set using this method or a URL, the port will be inferred based on the transport and security configuration.
The provided value must be within the range used for port numbers.
port
- the port of the serverIllegalArgumentException
- if the specified port
is invalidSessionFactory transports(SessionAttributes.Transport... transports) throws IllegalArgumentException
When a session is opened, it will try to establish a connection with each transport in the list until successful. If a connection cannot be established using any of the transports, the session will be closed.
This value is only used if a URL is not provided when opening a session.
The WebSocket transport will be used by default.
transports
- the transports to use to connect the session to the
server. The provided transports may not be null, contain null or
be an empty array.IllegalArgumentException
- if the specified transports
are
invalidSessionFactory secureTransport(boolean secureTransport)
This value is only used if a URL is not provided when opening a session.
Enables or disables the use of transport layer security. TLS is disabled
by default because in most cases to correctly enable TLS an
SSLContext
must also be provided using
sslContext(SSLContext)
.
secureTransport
- If transport layer security is enabledSessionFactory requestPath(String requestPath) throws IllegalArgumentException
This value is only used if a URL is not provided when opening a session. The path must start with a '/' and end with '/diffusion'.
The default is SessionAttributes.DEFAULT_REQUEST_PATH
.
requestPath
- the path used for HTTP requestsIllegalArgumentException
- if the specified requestPath
is
invalidSessionFactory recoveryBufferSize(int size) throws IllegalArgumentException
If the server is configured to support reconnection, a session
established with a non-zero
reconnection
time
retains a buffer of sent messages. If the session disconnects and
reconnects, this buffer is used to re-send messages that the server has
not received.
The default is SessionAttributes.DEFAULT_RECOVERY_BUFFER_SIZE
.
Higher values increase the chance of successful reconnection, but
increase the per-session memory footprint.
size
- the recovery buffer size in messages; can be 0IllegalArgumentException
- if the specified size
is
negativeSessionFactory maximumQueueSize(int size) throws IllegalArgumentException
The outbound message queue should be large enough to accommodate all the
messages sent to the server. This would include topic updates, messages
sent using Messaging
, and service requests such as registering a
handler.
It may be necessary to increase this value for applications that send messages in bursts, or continue to send messages when a session is disconnected and reconnecting. Larger values allow more messages to be queued, and increase the memory footprint of the session.
If the outbound message queue fills, sending a message will cause the session to close with an error.
The default is SessionAttributes.DEFAULT_MAXIMUM_QUEUE_SIZE
.
size
- a positive integer indicating the maximum number of messages
that may be queuedIllegalArgumentException
- if the specified size is not a positive
integerSessionFactory property(String key, String value)
Supplied session properties will be provided to the server when a session is created using this session factory. The supplied properties will be validated during authentication and may be discarded or changed.
The specified property will be added to any existing properties set for this session factory.
For details of how session properties are used see Session
.
key
- the property keyvalue
- the property valueproperties(Map)
SessionFactory properties(Map<String,String> properties)
Supplied session properties will be provided to the server when a session is created using this session factory. The supplied properties will be validated during authentication and may be discarded or changed.
The specified properties will be added to any existing properties set for this session factory. If any of the keys have been previously declared then they will be overwritten with the new values.
For details of how session properties are used see Session
.
properties
- a map of user-defined session propertiesproperty(String, String)
CompletableFuture<Session> openAsync()
The server location is configured using the serverHost(String)
,
serverPort(int)
,
transports(SessionAttributes.Transport...)
,
secureTransport(boolean)
and requestPath(String)
methods.
This method is non-blocking. The result is provided asynchronously by the
returned CompletableFuture
.
If a session was established, the CompletableFuture will complete
successfully with a Session
value.
Otherwise, the CompletableFuture will complete exceptionally with
a CompletionException
. Common reasons for failure, listed
by the exception reported as the
cause
, include:
IllegalStateException
– if any of the session
attributes are found to be inconsistent. For example, if the
default SSL context could not be loaded;
SessionEstablishmentException
– if an initial
connection could not be established;
AuthenticationException
–if the client is
insufficiently authorized to start the session, based on the
supplied client credentials.
open()
CompletableFuture<Session> openAsync(String url)
This method is a variant of openAsync()
that uses a URL to
specify the server location, ignoring the serverHost
and serverPort
,
requestPath
,
secureTransport
and
transports
session
factory attributes.
url
- the server location, see the class
documentation
If a session was established, the CompletableFuture will complete
successfully with a Session
value.
Otherwise, the CompletableFuture will complete exceptionally with
a CompletionException
. Common reasons for failure, listed
by the exception reported as the
cause
, include:
IllegalArgumentException
– if url
is
invalid;
IllegalStateException
– if any of the session
attributes are found to be inconsistent. For example, if the
default SSL context could not be loaded;
SessionEstablishmentException
– if an initial
connection could not be established;
AuthenticationException
–if the client is
insufficiently authorized to start the session, based on the
supplied client credentials.
Session open(String url) throws IllegalArgumentException, IllegalStateException, SessionEstablishmentException, AuthenticationException
This is a convenience version of openAsync(String)
which blocks
until the session has been established.
url
- the server location, see the class
documentation
IllegalArgumentException
- if url
is invalidIllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.SessionEstablishmentException
- if an initial connection could not
be establishedAuthenticationException
- if the client is insufficiently
authorized to start the session, based on the supplied client
credentials@Deprecated void open(String url, SessionFactory.OpenCallback callback) throws IllegalArgumentException, IllegalStateException
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead.
This method is a callback-based variant of openAsync(String)
. It
will return immediately. The result of the attempt to open the session
will be notified to the specified callback
.
url
- the server URL, see the class
documentation
callback
- which will be notified of the result of attempting to
open the sessionIllegalArgumentException
- if url
is invalidIllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.openAsync()
@Deprecated <C> void open(String url, C context, SessionFactory.OpenContextCallback<C> callback) throws IllegalArgumentException, IllegalStateException
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead.
This is the same as open(String, OpenCallback)
but with the
addition of an arbitrary context object.
C
- the context typeurl
- the server URL, see the class
documentation
context
- the context to pass to the callback, may be nullcallback
- which will be notified of the result of attempting to
open the sessionIllegalArgumentException
- if url
is invalidIllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.Session open() throws IllegalStateException, SessionEstablishmentException, AuthenticationException
This is a convenience version of openAsync()
which blocks until
the session has been established.
IllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.SessionEstablishmentException
- if an initial connection could not
be establishedAuthenticationException
- if the client is insufficiently
authorized to start the session, based on the supplied client
credentials@Deprecated void open(SessionFactory.OpenCallback callback) throws IllegalStateException
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead.
This method is a callback-based variant of openAsync()
. It will
return immediately. The result of the attempt to open the session will be
notified to the specified callback
.
callback
- which will be notified of the result of attempting to
open the sessionIllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.@Deprecated <C> void open(C context, SessionFactory.OpenContextCallback<C> callback) throws IllegalStateException
Methods that use callbacks are deprecated and will be removed in a future release. Use CompletableFuture variant instead.
This is the same as open(OpenCallback)
but with the addition of
an arbitrary context object.
C
- the context typecontext
- the context to pass to the callback, may be nullcallback
- which will be notified of the result of attempting to
open the sessionIllegalStateException
- if any of the session attributes are found
to be inconsistent. For example, if the default SSL context could
not be loaded.Copyright © 2024 DiffusionData Ltd. All Rights Reserved.