Skip to main content

Message sequences

Experimental

Message sequences are available since TimeBase 5.7.22. The feature and its API may change.

A sequence is a long number stored with a message. Each stream has its own numbering. Within one space, the cursor reads messages with the same timestamp in sequence order.

Enable sequences​

Choose a StreamOptions.sequences mode when creating the stream.

ModeBehavior
NONEDefault. No sequences.
AUTOThe server assigns sequences that are unique within the stream.
EXPLICITYour application supplies sequences.

Sequences require a durable stream with storage version 5. You cannot change the mode after creation.

For example:

Create an AUTO stream
StreamOptions options = new StreamOptions(StreamScope.DURABLE, "ticks", null, 0);
options.version = "5.0";
options.sequences = SequencesSupport.AUTO;
DXTickStream stream = db.createStream("ticks", options);

You can also enable sequences when creating a stream with DDL.

Write messages​

AUTO​

Send messages as usual. The server assigns sequences starting at 1, in the arrival order.

Write with an assigned sequence
try (TickLoader<InstrumentMessage> loader = stream.createLoader(
new LoadingOptions(false, LoadingOptions.WriteMode.APPEND))) {
loader.send(message);
}

EXPLICIT​

Create the stream with SequencesSupport.EXPLICIT in place of SequencesSupport.AUTO.

Write with an explicit sequence
LoadingOptions loading = new LoadingOptions(false, LoadingOptions.WriteMode.APPEND);
try (SequencedTickLoader<InstrumentMessage> loader =
(SequencedTickLoader<InstrumentMessage>) stream.createLoader(loading)) {
loader.send(message, 100);
}

Supply a positive sequence. For the same symbol and timestamp, sequence values must stay equal or increase.

Call send(message) or pass SequenceSource.NO_SEQUENCE to let the server assign the next value.

Read sequences​

Call getSequence() after cursor.next() returns true.

Read sequence values
try (TickCursor cursor = stream.select(0, new SelectionOptions())) {
while (cursor.next()) {
System.out.println(cursor.getSequence());
}
}

SequenceSource.NO_SEQUENCE value means no sequence is available.

Ordering limits

Sequences may have gaps. Do not compare them across streams. In EXPLICIT mode, a message with a later timestamp can have a lower sequence.

Loading errors​

By default, the loader skips messages with invalid sequences and keeps accepting messages. Listeners receive InvalidSequenceException.

Each loader reports the first InvalidSequenceException in a one-second window individually. It groups subsequent errors of that type in the same window into an AggregateLoadingError with the count and last error. The loader sends the summary on the next sequence error or flush() after the window ends, or when you call close(). Check for both error types in your listener.

To report and stop at the first invalid sequence, set NotifyAndAbort before creating the loader.

Abort on an invalid sequence
loading.addErrorAction(InvalidSequenceException.class, LoadingOptions.ErrorAction.NotifyAndAbort);

See loader error handling for other error actions.