As of version 0.10.0, Babl applications gained the ability to broadcast messages to multiple topics simultaneously. This new functionality makes publishing of market data even more efficient.
Multi-Topic Broadcast API
To use the API, an application must create and maintain membership of topics:
private static final class MarketDataApplication
implements Application, BroadcastSource
{
private Broadcast broadcast;
private int[] topicIds;
@Override
public void setBroadcast(final Broadcast broadcast)
{
// store the Broadcast implementation
this.broadcast = broadcast;
this.topicIds = new int[] {
MARKET_DATA_FULL_DEPTH, MARKET_DATA_TOP_OF_BOOK};
// create a topic for broadcast
broadcast.createTopic(MARKET_DATA_FULL_DEPTH);
broadcast.createTopic(MARKET_DATA_TOP_OF_BOOK);
}
@Override
public int onSessionConnected(final Session session)
{
// add new sessions to the topic
broadcast.addToTopic(MARKET_DATA_TOP_OF_BOOK, session.id());
return SendResult.OK;
}
public void onMarketDataUpdate(final MarketDataUpdate update)
{
final DirectBuffer buffer = serialise(update);
// send a message to all sessions registered on the topic
broadcast.sendToTopics(topicIds, buffer, 0, buffer.capacity());
}
}
Application messages broadcast to multiple topics will be handed to the registered message transformer before being written to sessions. A message transformer simply creates a topic-dependent view based on the topic ID:
final class MarketDataTransformer
implements MessageTransformer
{
private final ExpandableArrayBuffer dst =
new ExpandableArrayBuffer();
private final TransformResult transformResult =
new TransformResult();
@Override
public TransformResult transform(
final int topicId,
final DirectBuffer input,
final int offset,
final int length)
{
if (topicId == MARKET_DATA_TOP_OF_BOOK)
{
transformResult.set(dst, 0, encodeTopOfBook(input, offset, length));
}
else
{
transformResult.set(dst, 0, encodeFullDepth(input, offset, length));
}
return transformResult;
}
}
The message transformer must be registered before server start-up using the configuration API:
new BablConfig().sessionContainerConfig()
.messageTransformerFactory(topicId -> new MarketDataTransformer());
More Info
Babl source-code is available on Github, and releases are published to maven central and Docker hub.
Full documentation is available at https://babl.ws.
Enjoyed this post? You might be interested in subscribing to the Four Steps Newsletter, a monthly newsletter covering techniques for writing faster, more performant software.
Follow @epickrram