public class LightstreamerClient
extends java.lang.Object
Subscription
handling and to send messages. MpnSubscription
, a specific kind of subscription that
routes real-time updates via push notifications. 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).
|
MpnSubscription |
findMpnSubscription(java.lang.String subscriptionId)
Inquiry method that returns the MpnSubscription with the specified subscription ID, or null if not found.
The object returned by this method can be an object created by the user, via MpnSubscription constructors, or an object created by the client, to represent pre-existing MPN subscriptions. Note that objects returned by this method may be substitutued at any time with equivalent ones: do not rely on pointer matching, instead rely on the MpnSubscription.getSubscriptionId() value to verify the equivalence of two MpnSubscription objects. |
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.util.List<MpnSubscription> |
getMpnSubscriptions(java.lang.String filter)
Inquiry method that returns a collection of the existing MPN subscription with a specified status.
Can return both objects created by the user, via MpnSubscription constructors, and objects created by the client, to represent pre-existing MPN subscriptions.Note that objects in the collection may be substitutued at any time with equivalent ones: do not rely on pointer matching, instead rely on the MpnSubscription.getSubscriptionId() value to verify the equivalence of two MpnSubscription objects. |
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 |
registerForMpn(MpnDeviceInterface device)
Operation method that registers the MPN device on the server's MPN Module.
By registering an MPN device, the client enables MPN functionalities such as subscribe(MpnSubscription, boolean) . |
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(MpnSubscription subscription,
boolean coalescing)
Operation method that subscribes an MpnSubscription on server's MPN Module.
This operation adds the MpnSubscription to the list of "active" subscriptions. |
void |
subscribe(Subscription subscription)
Operation method that adds a Subscription to the list of "active" Subscriptions.
|
void |
unsubscribe(MpnSubscription subscription)
Operation method that unsubscribes an MpnSubscription from the server's MPN Module.
This operation removes the MpnSubscription from the list of "active" subscriptions. |
void |
unsubscribe(Subscription subscription)
Operation method that removes a Subscription that is currently in the "active" state.
|
void |
unsubscribeMpnSubscriptions(java.lang.String filter)
Operation method that unsubscribes all the MPN subscriptions with a specified status from the server's MPN Module.
By specifying a status filter it is possible to unsubscribe multiple MPN subscriptions at once. |
@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.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.
Note that, in this case, setting
and changing the default CookieHandler
afterwards may cause some
redundant handling (though only in the "compact" version of the library).
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
(though only in the "full" version of the library)
.
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 installedpublic void registerForMpn(@Nonnull MpnDeviceInterface device)
subscribe(MpnSubscription, boolean)
.device
- An MpnDeviceInterface
instace, carrying all the information about the MPN device.java.lang.IllegalArgumentException
- if the specified device is null.subscribe(MpnSubscription, boolean)
MpnDeviceInterface
can be registered at any time. The registration will be notified through a MpnDeviceListener.onRegistered()
event.
Note that forwarding of the registration to the server is made in a separate thread.public void subscribe(@Nonnull MpnSubscription subscription, boolean coalescing)
MpnSubscription
to the list of "active" subscriptions. MPN subscriptions are activated on the server as soon as possible
(i.e. as soon as there is a session available and subsequently as soon as the MPN device registration succeeds). Differently than real-time subscriptions,
MPN subscriptions are persisted on the server's MPN Module database and survive the session they were created on.coalescing
flag is set, the activation of two MPN subscriptions with the same Adapter Set, Data Adapter, Group, Schema and trigger expression will be
considered the same MPN subscription. Activating two such subscriptions will result in the second activation modifying the first MpnSubscription (that
could have been issued within a previous session). If the coalescing
flag is not set, two activations are always considered different MPN subscriptions,
whatever the Adapter Set, Data Adapter, Group, Schema and trigger expression are set.coalescing
flag is to allow simple apps to always activate their MPN subscriptions when the app starts, without worrying if
the same subscriptions have been activated before or not. In fact, since MPN subscriptions are persistent, if they are activated every time the app starts and
the coalescing
flag is not set, every activation is a new MPN subscription, leading to multiple push notifications for the same event.subscription
- An MpnSubscription object, carrying all the information to route real-time data via push notifications.coalescing
- A flag that specifies if the MPN subscription must coalesce with any pre-existing MPN subscription with the same Adapter Set, Data Adapter,
Group, Schema and trigger expression.java.lang.IllegalStateException
- if the given MPN subscription does not contain a field list/field schema.java.lang.IllegalStateException
- if the given MPN subscription does not contain a item list/item group.java.lang.IllegalStateException
- if there is no MPN device registered.java.lang.IllegalStateException
- if the given MPN subscription is already active.unsubscribe(MpnSubscription)
,
unsubscribeMpnSubscriptions(String)
unsubscribe(MpnSubscription)
.MpnSubscriptionListener.onSubscription()
event.public void unsubscribe(@Nonnull MpnSubscription subscription)
subscription
- An "active" MpnSubscription object.java.lang.IllegalStateException
- if the given MPN subscription is not active.java.lang.IllegalStateException
- if there is no MPN device registered.subscribe(MpnSubscription, boolean)
,
unsubscribeMpnSubscriptions(String)
MpnSubscriptionListener.onUnsubscription()
event.public void unsubscribeMpnSubscriptions(@Nullable java.lang.String filter)
TRIGGERED
it is possible
to unsubscribe all triggered MPN subscriptions. This operation removes the involved MPN subscriptions from the list of "active" subscriptions.filter
- A status name to be used to select the MPN subscriptions to unsubscribe. If null all existing MPN subscriptions
are unsubscribed. Possible filter values are:ALL
or nullTRIGGERED
SUBSCRIBED
java.lang.IllegalArgumentException
- if the given filter is not valid.java.lang.IllegalStateException
- if there is no MPN device registered.subscribe(MpnSubscription, boolean)
,
unsubscribe(MpnSubscription)
MpnSubscriptionListener.onUnsubscription()
event to all involved MPN subscriptions.@Nonnull public java.util.List<MpnSubscription> getMpnSubscriptions(@Nullable java.lang.String filter)
MpnSubscription
constructors, and objects created by the client, to represent pre-existing MPN subscriptions.MpnSubscription.getSubscriptionId()
value to verify the equivalence of two MpnSubscription objects. Substitutions may happen
when an MPN subscription is modified, or when it is coalesced with a pre-existing subscription.filter
- An MPN subscription status name to be used to select the MPN subscriptions to return. If null all existing MPN subscriptions
are returned. Possible filter values are:ALL
or nullTRIGGERED
SUBSCRIBED
MpnSubscription
with the specified status.java.lang.IllegalArgumentException
- if the given filter is not valid.java.lang.IllegalStateException
- if there is no MPN device registered.findMpnSubscription(String)
MpnDeviceListener.onSubscriptionsUpdated()
event has been notified.@Nullable public MpnSubscription findMpnSubscription(@Nonnull java.lang.String subscriptionId)
MpnSubscription.getSubscriptionId()
value to verify the equivalence of two MpnSubscription objects. Substitutions may happen
when an MPN subscription is modified, or when it is coalesced with a pre-existing subscription.subscriptionId
- The subscription ID to search for.java.lang.IllegalArgumentException
- if the given subscription ID is null.java.lang.IllegalStateException
- if there is no MPN device registered.getMpnSubscriptions(String)