Skip to main content

KeySession

Class

trading.keysquare.api.session.KeySession

Session provides connectivity to the platform.

Methods

Session Lifecycle

Methods to start and stop the session.

void start()

Start the session. This is synchronous and will block the calling thread until a connection is established.

void stop()

Stop the session.

Type Routing

Methods to register message processors for different types of messages.

void register(Class<T> messageDecoderFlyweightClass, MessageProcessor<T> messageProcessor)

Register a message processor where length of the message is required

This may only be invoked before starting the session.

void register(Class<T> messageDecoderFlyweightClass, SimpleMessageProcessor<T> messageProcessor)

Register a simple message processor where the length of a message isn't required.

This may only be invoked before starting the session.

void register(Class<T> clazz, DtoMessageProcessor<T> messageProcessor)

Register a message processor for a DTO messaging.

This may only be invoked before starting the session.

void register(Class<T> clazz, FlexibleMessageProcessor<T> flexibleProcessor)

Register a message processor for Flexible messaging

This may only be invoked before starting the session.

void register(Class<T> messageDecoderFlyweightClass, MessageProcessor<T> messageProcessor, MessageProcessor<T> voidedMessageProcessor)

Register a message processor where length of the message is required

The first processor provided will have valid messages routed to it and the second processor will have voided messages routed to it.

This may only be invoked before starting the session.

void register(Class<T> messageDecoderFlyweightClass, SimpleMessageProcessor<T> messageProcessor, SimpleMessageProcessor<T> voidedMessageProcessor)

Register a simple message processor where the length of a message isn't required.

The first processor provided will have valid messages routed to it and the second processor will have voided messages routed to it.

This may only be invoked before starting the session.

void register(Class<T> clazz, DtoMessageProcessor<T> messageProcessor, DtoMessageProcessor<T> vodiedMessageProcessor)

Register a message processor for a DTO messaging.

The first processor provided will have valid messages routed to it and the second processor will have voided messages routed to it.

This may only be invoked before starting the session.

void register(Class<T> clazz, FlexibleMessageProcessor<T> flexibleProcessor, FlexibleMessageProcessor<T> voidedFlexibleProcessor)

Register a message processor for Flexible messaging.

The first processor provided will have valid messages routed to it and the second processor will have voided messages routed to it.

This may only be invoked before starting the session.

Subscription

Methods to subscribe to data.

Result subscribe(SubscriptionType subscriptionType, SubscriptionListener listener, Topic[] topics)

Subscribe to data.

This can be invoked before or after a session has been started.

ParameterDescription
subscriptionTypeLIVE - where only live data is required, CACHED - where only cached data is required, CACHED_AND_LIVE - where both cached and live data is required
SubscriptionListenerA listener that will be notified when the subscription is completed.
topicsvarargs of topics of interest

Return value > 0 indicates success.

Throws IllegalStateException if subscription is unsuccessful.

Subscribe to all cached and live RFQs
// Create a topic which can be used to subscribe to all RFQs
Topic rfqTopic = Topics.topic(RfqEncoder.class, Topic.SOURCE_WILDCARD, Topic.GROUP_WILDCARD, Topic.ID_WILDCARD);

// Subscribe to RFQs published on the platform
Result result = session.subscribe("rfq", SubscriptionType.CACHED_AND_LIVE, rfqTopic);
if (result.isOk()) {
...
}

Result unsubscribe(UnsubscriptionListener listener, Topic[] topics)

Unsubscribe to data.

Note that the topic(s) provided should match those that were first provided when subscribing.

This can be invoked after a session has been started.

Publication

Methods to publish data.

Result publish(DirectBuffer buffer, int offset, int length)

Publish a SBE buffer from an offset for a given length. This will use the topic details provided in the header.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(DirectBuffer buffer)

Publish an entire SBE buffer. This will use the topic details provided in the header.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(MessageEncoderFlyweight messageEncoderFlyweight)

Publish an entire SBE buffer wrapped by the provided encoder flyweight. This will use the topic details provided in the header.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(KeyDto dto)

Publish the DTO provided. This will publish using the topic details found in the header of the DTO.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(MutableKeyDto dto, CharSequence topicSource, CharSequence topicGroup, CharSequence topicId)

Publish a DTO to the specified topic.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(Object object, KeyHeader keyHeader)

Publish an Object that can be serialized, the topic structure in the specified keyHeader will be used.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Result publish(Object object, CharSequence topicSource, CharSequence topicGroup, CharSequence topicId)

Publish an Object that can be serialized, to the specified topic. This is a convenience method, the method that accepts an Object and KeyHeader should be preferred.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Misc

Miscellaneous methods that can be used by applications.

void setKeySessionListener(KeySessionListener keySessionListener)

Set the listener. This should only be invoked before calling start().

int getApplicationId()

Get the application id of the application.

This can be invoked after starting the session.

String getApplicationName()

Get the application name of the application.

This can be invoked before starting the session.

int getApplicationGroupId()

Get the application group id of the application. This will default to the application id if there is no associated group.

This can be invoked after starting the session.

String getApplicationGroupName()

Get the application group name of the application. This will default to the application name if there is no associated group.

This can be invoked before starting the session.

boolean isConnected()

Returns a boolean indicating if the session is connected to the platform.

int resolveApplicationId(String applicationName)

Given an application name, resolve this back to an application id.

This can be invoked after starting the session.

Note that this will only perform resolution for applications that are known prior to the application starting up. New applications created post startup will only be resolved following a restart of the application.

String resolveApplicationName(int applicationId)

Given an application id, resolve this back to an application name.

This can be invoked after starting the session.

Note that this will only perform resolution for applications that are known prior to the application starting up. New applications created post startup will only be resolved following a restart of the application.

Monitoring

Methods to related to monitoring such as reporting statuses and create monitoring metrics.

void readinessState(ReadinessState state)

Indicate readiness for this application which will be captured by monitoring.

This can be invoked after starting the session.

Send LifeCycle information to Monitoring
// Use monitoring life-cycle status to indicate that the application is online and ready
session.readinessState(ReadinessState.READY);

Result applicationStatus(String label, ApplicationCustomState state, String message)

@deprecated Use applicationCustomStatus instead.

Result applicationCustomStatus(String label, ApplicationCustomState state, String message)

Indicate a custom status for this application which will be captured by monitoring.

This can be invoked after starting the session.

Return a Result which can be checked for success.

Send a specific Application State information to Monitoring
// Use monitoring application status to send an arbitrary status
session.applicationStatus("Rfq", ApplicationState.OK, "RFQs Received");

Histogram createHistogram(String name)

Create a histogram which will be be captured by monitoring.

This can be invoked before starting the session.

Create a histogram to track price latency
Histogram histogram = session.createHistogram("PriceLatency");
histogram.record(long latency);

Counter createCounter(String name)

Create a counter which will be be captured by monitoring.

This can be invoked before starting the session.