Skip to main content

Dynamic Subscription Change

TimeBase allows subscribing to live data based on message attributes such as type, stream and symbol and changing subscription at any time.

There are three possible scenarios:

Basic Subscription

In the Basic scenario, subscription starts at some point of time (close to real-time) when the command reaches the server. This scenario may be applied in case precise subscription timing and order are not critical.

Basic subscription options
//Subscribe to a symbol
cursor.removeEntity(new ConstantIdentityKey("CLX20"));
cursor.addEntity(new ConstantIdentityKey("CLZ20"));

//Sybsrcibe to a message type
cursor.addTypes("epam.timebase.samples.SensorType1");

//Subscribe to a stream
TickStream stream = Arrays.stream(tickDB.listStreams())
.filter(item -> "SmartMetersOhio".equals(item.getKey()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Stream was not found"));
cursor.addStream(stream);

Precise Subscription

Precise subscription may be applied when it is necessary to set the precise timing for a new subscription. In this case, the subscribed data arrives precisely at the specified time but there is no guarantee as for the data ordering.

Precise subscription
// specify time for newly subscribed data
cursor.setTimeForNewSubscriptions(Instant.now().toEpochMilli());
cursor.addEntity(new ConstantIdentityKey("TrafficLight-4419"));

Complete Reset

This scenario may be applied in case both precise subscription timing and order are required. It is the slowest case in terms of performance due to the complete reset of all buffers. Reset is performed at a specified time, starting from which you will receive data with the precise order and time.

Subscribe for precise order and time
// reset cursor
cursor.clearAllEntities();
String[] symbolsToAdd = new String[] {"Meter-1", "Meter-2", "Meter-3"};
cursor.addEntities(
Arrays.stream(symbolsToAdd)
.map(item -> new ConstantIdentityKey(item))
.toArray(IdentityKey[]::new), 0, symbolsToAdd.length);
cursor.reset(Instant.parse("2020-08-04T10:12:35Z").toEpochMilli());