Lightstreamer Java Client SDK 5.2.0 API Reference

Summary

  1. Introduction
  2. Installing
  3. Quickstart
  4. Logging
  5. Compatibility
  6. Documentation
  7. Support
  8. License
  9. Packages

Introduction

This Java library enables any Java application to communicate bidirectionally with the Lightstreamer Server. The API allows to subscribe to real-time data pushed by the server and to send any message to the server.

The library exposes a fully asynchronous API. All the API calls that require any action from the library itself are queued for processing by a dedicated thread before being carried out. The same thread is also used to carry notifications for the appropriate listeners as provided by the custom code. Blocking operations and internal housekeeping are performed on different threads.

The library offers automatic recovery from connection failures, automatic selection of the best available transport, and full decoupling of subscription and connection operations. The subscriptions are always meant as subscriptions "to the LightstreamerClient", not "to the Server"; the LightstreamerClient is responsible of forwarding the subscriptions to the Server and re-forwarding all the subscriptions whenever the connection is broken and then reopened.

The Java library can be available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

Installing

To add a dependency using Maven:

<dependency>
  <groupId>com.lightstreamer</groupId>
  <artifactId>ls-javase-client</artifactId>
  <version>5.2.0</version>
</dependency>
To add a dependency using Gradle:

dependencies {
  implementation("com.lightstreamer:ls-javase-client:5.2.0")
}

Quickstart

To connect to a Lightstreamer Server, a LightstreamerClient object has to be created, configured, and instructed to connect to the Lightstreamer Server. A minimal version of the code that creates a LightstreamerClient and connects to the Lightstreamer Server on https://push.lightstreamer.com will look like this:

LightstreamerClient client = new LightstreamerClient("https://push.lightstreamer.com/","DEMO");
client.connect();
For each subscription to be subscribed to a Lightstreamer Server a Subscription instance is needed. A simple Subscription containing three items and two fields to be subscribed in MERGE mode is easily created (see Lightstreamer General Concepts):

String[] items = { "item1","item2","item3" };
String[] fields = { "stock_name","last_price" };
Subscription sub = new Subscription("MERGE",items,fields);
sub.setDataAdapter("QUOTE_ADAPTER");
sub.setRequestedSnapshot("yes");
client.subscribe(sub);
Before sending the subscription to the server, usually at least one SubscriptionListener is attached to the Subscription instance in order to consume the real-time updates. The following code shows the values of the fields stock_name and last_price each time a new update is received for the subscription:

sub.addListener(new SubscriptionListener() {
    public void onItemUpdate(ItemUpdate obj) {
        System.out.println(obj.getValue("stock_name") + ": " + obj.getValue("last_price"));
    }
    // other methods...
});

Logging

To enable the internal client logger, create an instance of LoggerProvider and set it as the default provider of LightstreamerClient.

LightstreamerClient.setLoggerProvider(new ConsoleLoggerProvider(ConsoleLogLevel.DEBUG));

Compatibility

JavaSE Client requires Java version 8 or later.

The library is compatible with Lightstreamer Server since version 7.4.0.

Documentation

Support

For questions and support please use the Official Forum. The issue list of this page is exclusively for bug reports and feature requests.

License

Apache 2.0
Packages