public class LightstreamerClient
extends java.lang.Object
Subscription
handling and to send messages. Modifier and Type | Field and Description |
---|---|
ConnectionDetails |
connectionDetails
Data object that contains the details needed to open a connection to
a Lightstreamer Server.
|
ConnectionOptions |
connectionOptions
Data object that contains options and policies for the connection to
the server.
|
static java.lang.String |
LIB_NAME
A constant string representing the name of the library.
|
static java.lang.String |
LIB_VERSION
A constant string representing the version of the library.
|
Constructor and Description |
---|
LightstreamerClient(java.lang.String serverAddress,
java.lang.String adapterSet)
Creates an object to be configured to connect to a Lightstreamer server
and to handle all the communications with it.
|
Modifier and Type | Method and Description |
---|---|
static void |
addCookies(java.net.URI uri,
java.util.List<java.net.HttpCookie> cookies)
Static method that can be used to share cookies between connections to the Server
(performed by this library) and connections to other sites that are performed
by the application.
|
void |
addListener(ClientListener listener)
Adds a listener that will receive events from the LightstreamerClient instance.
|
void |
connect()
Operation method that requests to open a Session against the configured Lightstreamer Server.
|
void |
disconnect()
Operation method that requests to close the Session opened against the configured Lightstreamer Server
(if any).
|
java.util.concurrent.Future<java.lang.Void> |
disconnectFuture()
Works just like
disconnect() , but also returns a Future which is notified
when all involved threads started by all LightstreamerClient instances living in the JVM have been terminated, because no
more activities need to be managed and hence event dispatching is no longer necessary. |
static java.util.List<java.net.HttpCookie> |
getCookies(java.net.URI uri)
Static inquiry method that can be used to share cookies between connections to the Server
(performed by this library) and connections to other sites that are performed
by the application.
|
java.util.List<ClientListener> |
getListeners()
Returns a list containing the
ClientListener instances that were added to this client. |
java.lang.String |
getStatus()
Inquiry method that gets the current client status and transport (when applicable).
|
java.util.List<Subscription> |
getSubscriptions()
Inquiry method that returns a list containing all the Subscription instances that are
currently "active" on this LightstreamerClient.
|
void |
removeListener(ClientListener listener)
Removes a listener from the LightstreamerClient instance so that it will not receive events anymore.
|
void |
sendMessage(java.lang.String message)
A simplified version of the
sendMessage(String,String,int,ClientMessageListener,boolean) . |
void |
sendMessage(java.lang.String message,
java.lang.String sequence,
int delayTimeout,
ClientMessageListener listener,
boolean enqueueWhileDisconnected)
Operation method that sends a message to the Server.
|
static void |
setLoggerProvider(com.lightstreamer.log.LoggerProvider provider)
Static method that permits to configure the logging system used by the library.
|
static void |
setTrustManagerFactory(javax.net.ssl.TrustManagerFactory factory)
Provides a mean to control the way TLS certificates are evaluated, with the possibility to accept untrusted ones.
|
void |
subscribe(Subscription subscription)
Operation method that adds a Subscription to the list of "active" Subscriptions.
|
void |
unsubscribe(Subscription subscription)
Operation method that removes a Subscription that is currently in the "active" state.
|
@Nonnull public static final java.lang.String LIB_NAME
@Nonnull public static final java.lang.String LIB_VERSION
@Nonnull public final ConnectionOptions connectionOptions
@Nonnull public final ConnectionDetails connectionDetails
public LightstreamerClient(@Nullable java.lang.String serverAddress, @Nullable java.lang.String adapterSet)
serverAddress
- the address of the Lightstreamer Server to
which this LightstreamerClient will connect to. It is possible to specify it later
by using null here. See ConnectionDetails.setServerAddress(String)
for details.adapterSet
- the name of the Adapter Set mounted on Lightstreamer Server
to be used to handle all requests in the Session associated with this
LightstreamerClient. It is possible not to specify it at all or to specify
it later by using null here. See ConnectionDetails.setAdapterSet(String)
for details.java.lang.IllegalArgumentException
- if a not valid address is passed. See
ConnectionDetails.setServerAddress(String)
for details.public static void setLoggerProvider(@Nullable com.lightstreamer.log.LoggerProvider provider)
provider
- A LoggerProvider
instance that will be used to generate log messages by the library classes.public void addListener(@Nonnull ClientListener listener)
listener
- An object that will receive the events as documented in the
ClientListener interface.removeListener(ClientListener)
public void removeListener(@Nonnull ClientListener listener)
listener
- The listener to be removed.addListener(ClientListener)
@Nonnull public java.util.List<ClientListener> getListeners()
ClientListener
instances that were added to this client.addListener(ClientListener)
public void connect()
ConnectionOptions.setForcedTransport(String)
, the so called "Stream-Sense" mechanism is started:
if the client does not receive any answer for some seconds from the streaming connection, then it
will automatically open a polling connection. java.lang.IllegalStateException
- if no server address was configured.getStatus()
,
disconnect()
,
ClientListener.onStatusChange(String)
,
ConnectionDetails.setServerAddress(String)
getStatus()
right after connect() might not reflect the change yet. public void disconnect()
connect()
getStatus()
right after disconnect() might not reflect the change yet. @Nonnull public java.util.concurrent.Future<java.lang.Void> disconnectFuture()
disconnect()
, but also returns a Future
which is notified
when all involved threads started by all LightstreamerClient
instances living in the JVM have been terminated, because no
more activities need to be managed and hence event dispatching is no longer necessary.
Such method is especially useful in those environments which require an appropriate resource management,
like "full" Java EE application servers or even the simpler Servlet Containers.
The method should be used in replacement of disconnect()
in all those circumstances where
it is indispensable to guarantee a complete shutdown of all user threads, in order to avoid potential memory leaks and waste resources.
For example, in a web application, it might be possible to leverage a hook like ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent) to trigger the await of threads termination, before the ServletContext will be shut down.
public void contextDestroyed(ServletContext sce) {
// client is already instantiated elsewhere and a connection to LightstreamerServer is in place.
Future disconnected = client.disconnectFuture();
// Blocks until all threads have been terminated.
disconnected.get()
}
Future
object representing pending completion of the disconnection task.disconnect()
,
connect()
@Nonnull public java.lang.String getStatus()
ClientListener.onStatusChange(String)
public void subscribe(@Nonnull Subscription subscription)
subscription
- A Subscription object, carrying all the information needed to process real-time values.unsubscribe(Subscription)
unsubscribe(Subscription)
. SubscriptionListener.onSubscription()
event.public void unsubscribe(@Nonnull Subscription subscription)
subscription
- An "active" Subscription object that was activated by this LightstreamerClient
instance.SubscriptionListener.onUnsubscription()
event.@Nonnull public java.util.List<Subscription> getSubscriptions()
subscribe(Subscription)
public void sendMessage(@Nonnull java.lang.String message)
sendMessage(String,String,int,ClientMessageListener,boolean)
.
The internal implementation will call
sendMessage(message,null,-1,null,false);
Note that this invocation involves no sequence and no listener, hence an optimized
fire-and-forget behavior will be applied.message
- a text message, whose interpretation is entirely demanded to the Metadata Adapter
associated to the current connection.public void sendMessage(@Nonnull java.lang.String message, @Nullable java.lang.String sequence, int delayTimeout, @Nullable ClientMessageListener listener, boolean enqueueWhileDisconnected)
ClientMessageListener.onDiscarded(String)
.
Note that, because of the parallel transport of the messages, if a zero or very low timeout is
set for a message and the previous message was sent immediately before, it is possible that the
latter gets discarded even if no communication issues occur.
The Server may also enforce its own timeout on missing messages, to prevent keeping the subsequent
messages for long time. message
- a text message, whose interpretation is entirely demanded to the Metadata Adapter
associated to the current connection.sequence
- an alphanumeric identifier, used to identify a subset of messages to be managed in sequence;
underscore characters are also allowed. If the "UNORDERED_MESSAGES" identifier is supplied, the message will
be processed in the special way described above. The parameter is optional; if set to null, "UNORDERED_MESSAGES"
is used as the sequence name.delayTimeout
- a timeout, expressed in milliseconds. If higher than the Server configured timeout
on missing messages, the latter will be used instead. listener
- an object suitable for receiving notifications about the processing outcome. The parameter is
optional; if not supplied, no notification will be available.enqueueWhileDisconnected
- if this flag is set to true, and the client is in a disconnected status when
the provided message is handled, then the message is not aborted right away but is queued waiting for a new
session. Note that the message can still be aborted later when a new session is established.ClientMessageListener.onAbort(java.lang.String, boolean)
event will be fired. public static void addCookies(@Nonnull java.net.URI uri, @Nonnull java.util.List<java.net.HttpCookie> cookies)
CookieHandler
is not available;
then it will always stick to the internal storage.
CookieHandler
is available
upon the first usage of the cookies, the library, from then on, will always stick
to the default it finds upon each request; in this case, the cookie storage will be
already shared with the rest of the application. However, whenever a default
CookieHandler
of type different from CookieManager
is found, the library will not be able to use it and will skip cookie handling
.
uri
- the URI from which the supplied cookies were received. It cannot be null.cookies
- a list of cookies, represented in the JDK-provided type.getCookies(java.net.URI)
connect()
method. However it can be invoked at any time;
it will affect the internal cookie set immediately and the sending of cookies
on the next HTTP request or WebSocket establishment.@Nonnull public static java.util.List<java.net.HttpCookie> getCookies(@Nullable java.net.URI uri)
addCookies(java.net.URI, java.util.List<java.net.HttpCookie>)
for clarifications on when cookies are directly stored
by the library and when not.uri
- the URI to which the cookies should be sent, or null.public static void setTrustManagerFactory(@Nonnull javax.net.ssl.TrustManagerFactory factory)
factory
- trust manager factoryjava.lang.NullPointerException
- if the factory is nulljava.lang.IllegalStateException
- if a factory is already installed