Skip to main content

Topic Sets

info

This API is currently in beta and available via KeySessionExperimental.

A Topic Set is a named, publisher-managed collection of topics. A subscriber subscribes to the set and the api automatically applies topics contained within it. The Topic Set owner then controls which topics the subscriber receives by adding or removing topics from the set.

Creating a Topic Set

The topic used when creating the set defines the name and the message type of the Topic Set.

TopicSet set = session.createTopicSet(
Topics.create(PriceDto.class, "Price", "UST", "OTR"));

set.add(Topics.create(PriceDto.class, "Price", "UST", "2Y"));
set.add(Topics.create(PriceDto.class, "Price", "UST", "5Y"));
set.add(Topics.create(PriceDto.class, "Price", "UST", "10Y"));

Result result = set.applyChanges();

Calls to add(...) and remove(...) stage local changes. The staged changes are published when applyChanges() is called.

Updating a Topic Set

Topic Sets can be updated by adding or removing topics and then applying the changes.

set.remove(Topics.create(PriceDto.class, "Price", "UST", "2Y"));
set.add(Topics.create(PriceDto.class, "Price", "UST", "30Y"));

Result result = set.applyChanges();

The call to applyChanges() is atomic from the subscriber's point of view.

Subscribing to a Topic Set

Register a route for the message type, then subscribe to the Topic Set.

session.routing().dto(PriceDto.class, this::processPrice, Optional.empty());

experimental.subscribeTopicSet("Price", "UST", "OTR", new TopicSetSubscriptionListener() {
@Override
public void complete(long count) {
log.info("Topic Set subscription complete, got {} items", count);
}
});

Topic Sets can contain only one message type. Topic Sets cannot be nested.