dxFeed Graal CXX API
Loading...
Searching...
No Matches
DXPublisher Struct Reference

Provides API for publishing of events to local or remote DXFeed. More...

#include <DXPublisher.hpp>

+ Inheritance diagram for DXPublisher:

Public Types

using Ptr = std::shared_ptr<DXPublisher>
 The alias to a type of shared pointer to the DXPublisher object.
 
using Unique = std::unique_ptr<DXPublisher>
 The alias to a type of unique pointer to the DXPublisher object.
 
- Public Types inherited from SharedEntity
using Ptr = std::shared_ptr<SharedEntity>
 The alias to a type of shared pointer to the SharedEntity object.
 

Public Member Functions

template<typename EventsCollection >
void publishEvents (EventsCollection &&events) noexcept
 Publishes events to the corresponding feed.
 
void publishEvents (std::initializer_list< std::shared_ptr< EventType > > events) noexcept
 Publishes events to the corresponding feed.
 
void publishEvents (std::shared_ptr< EventType > event) noexcept
 Publishes an event to the corresponding feed.
 
template<typename EventIt >
void publishEvents (EventIt begin, EventIt end)
 Publishes events to the corresponding feed.
 
std::shared_ptr< ObservableSubscriptiongetSubscription (const EventTypeEnum &eventType)
 Returns observable set of subscribed symbols for the specified event type.
 
std::string toString () const noexcept override
 Returns a string representation of the current object.
 
- Public Member Functions inherited from SharedEntity
template<typename T >
bool is () const noexcept
 Checks that pointer to the current type could be converted to type T* In other words: whether type T belongs to the type hierarchy in which the current type resides.
 
template<typename T >
std::shared_ptr< T > sharedAs () noexcept
 Returns a pointer to the current object wrapped in a smart pointer to type T.
 
template<typename T >
std::shared_ptr< T > sharedAs () const noexcept
 Returns a pointer to the current object wrapped in a smart pointer to type T.
 
- Public Member Functions inherited from Entity
virtual ~Entity () noexcept=default
 The default virtual d-tor.
 

Static Public Member Functions

static std::shared_ptr< DXPublishergetInstance ()
 Returns a default application-wide singleton instance of DXPublisher.
 

Friends

struct DXEndpoint
 

Detailed Description

Provides API for publishing of events to local or remote DXFeed.

Sample usage

This section gives sample usage scenarios.

Default singleton instance

There is a singleton instance of the publisher that is returned by DXPublisher::getInstance() method. It is created on the first use with default configuration properties that are explained in detail in documentation for DXEndpoint class in the "Default properties" section. In particular, you can provide a default address to connect using 'dxpublisher.address' system property or by putting it into 'dxpublisher.properties' file in the current directory.

Publish a single event

The following code publishes a single quote for a "A:TEST" symbol:


auto quote = std::make_shared<Quote>("A:TEST");
quote->setBidPrice(100);
quote->setAskPrice(101);
DXPublisher::getInstance()->publishEvents(quote);

Threads and locks

This class is thread-safe and can be used concurrently from multiple threads without external synchronization.

Member Function Documentation

◆ getInstance()

DXFCPP_BEGIN_NAMESPACE std::shared_ptr< DXPublisher > DXPublisher::getInstance ( )
static

Returns a default application-wide singleton instance of DXPublisher.

Most applications use only a single data-sink and should rely on this method to get one. This is a shortcut to DXEndpoint::getInstance(DXEndpoint::Role::PUBLISHER)->getPublisher().

◆ getSubscription()

std::shared_ptr< ObservableSubscription > DXPublisher::getSubscription ( const EventTypeEnum & eventType)

Returns observable set of subscribed symbols for the specified event type.

Note, that subscription is represented by SymbolWrapper symbols. Check the type of each symbol in ObservableSubscription using SymbolWrapper::isStringSymbol(), SymbolWrapper::isWildcardSymbol(), SymbolWrapper::isIndexedEventSubscriptionSymbol(), SymbolWrapper::isTimeSeriesSubscriptionSymbol(), SymbolWrapper::isCandleSymbol().

The set of subscribed symbols contains WildcardSymbol::ALL if and only if there is a subscription to this wildcard symbol.

If DXFeedTimeSeriesSubscription is used to subscribe to time-service of the events of this type, then instances of TimeSeriesSubscriptionSymbol class represent the corresponding subscription item.

The resulting observable subscription can generate repeated ObservableSubscriptionChangeListener::onSymbolsAdded_ notifications to its listeners for the same symbols without the corresponding ObservableSubscriptionChangeListener::onSymbolsRemoved_ notifications in between them. It happens when subscription disappears, cached data is lost, and subscription reappears again. On each ObservableSubscriptionChangeListener::onSymbolsAdded_ notification data provider shall publish the most recent events for the corresponding symbols.

Parameters
eventTypeThe event type.
Returns
Observable subscription for the specified event type.

◆ publishEvents() [1/4]

template<typename EventIt >
void DXPublisher::publishEvents ( EventIt begin,
EventIt end )
inline

Publishes events to the corresponding feed.

Template Parameters
EventItThe collection's iterator type
Parameters
beginThe beginning of the collection of events.
endThe end of events collection.

◆ publishEvents() [2/4]

template<typename EventsCollection >
void DXPublisher::publishEvents ( EventsCollection && events)
inlinenoexcept

Publishes events to the corresponding feed.

If the endpoint of this publisher has role of DXEndpoint::Role::PUBLISHER and it is connected, the published events will be delivered to the remote endpoints. Local feed will always receive published events.

This method serializes all events into internal representation, so that the instance of the collection as well as the instances of events can be reused after invocation of this method returns.

DXFeed instances that are connected to this publisher either locally or via network receive published events if and only if they are subscribed to the corresponding symbols, or they are subscribed via WildcardSymbol::ALL, or, in case of TimeSeriesEvent type, they are subscribed via DXFeedTimeSeriesSubscription for the corresponding symbol and time frame.

Published events are not stored and get immediately lost if there is no subscription. Last published events of LastingEvent types are cached as long as subscription to them is maintained via a specific event symbol (WildcardSymbol::ALL does not count) and the cache is discarded as soon as subscription disappears.

Example:

using namespace dxfcpp;
using namespace dxfcpp::literals;
using namespace std::literals;
System::setProperty("dxendpoint.eventTime", "true");
System::setProperty("dxscheme.nanoTime", "true");
auto feed = endpoint->getFeed();
auto publisher = endpoint->getPublisher();
auto sub = feed->createSubscription({Quote::TYPE});
auto quote = std::make_shared<Quote>("AAPL");
quote->setEventTime(1692975409000);
quote->setTimeNanoPart(123);
quote->setSequence(123);
quote->setBidTime(1692975409000);
quote->setBidExchangeCode('B');
quote->setBidPrice(176.08);
quote->setBidSize(1.0);
quote->setAskTime(1692975409000);
quote->setAskExchangeCode('A');
quote->setAskPrice(176.16);
quote->setAskSize(2.0);
sub->addEventListener([](const auto &events) {
for (const auto &e : events) {
if (const auto &q = e->template sharedAs<Quote>(); q) {
std::cout << q << std::endl;
}
}
});
sub->addSymbols("AAPL");
publisher->publishEvents(quote);
publisher->publishEvents({quote, quote});
std::vector<EventType::Ptr> events{};
events.push_back(quote);
events.push_back(quote);
publisher->publishEvents(events);
std::this_thread::sleep_for(1000ms);
endpoint->close();
static const EventTypeEnum & TYPE
Type identifier and additional information about the current event class.
Definition Quote.hpp:69
static std::shared_ptr< DXEndpoint > create()
Creates an endpoint with FEED role.
Definition DXEndpoint.cpp:447
@ FEED
FEED endpoint connects to the remote data feed provider and is optimized for real-time or delayed dat...
std::shared_ptr< T > sharedAs() noexcept
Returns a pointer to the current object wrapped in a smart pointer to type T.
Definition SharedEntity.hpp:47
static bool setProperty(const std::string &key, const std::string &value)
Sets the JVM system property indicated by the specified key.
Definition System.cpp:18
Template Parameters
EventsCollectionThe type of events collection (for example: std::vector<::EventType::Ptr>)
Parameters
eventsThe collection of events to publish.

◆ publishEvents() [3/4]

void DXPublisher::publishEvents ( std::initializer_list< std::shared_ptr< EventType > > events)
inlinenoexcept

Publishes events to the corresponding feed.

Parameters
eventsThe collection of events to publish.

◆ publishEvents() [4/4]

void DXPublisher::publishEvents ( std::shared_ptr< EventType > event)
inlinenoexcept

Publishes an event to the corresponding feed.

Parameters
eventThe event to publish.

◆ toString()

std::string DXPublisher::toString ( ) const
overridevirtualnoexcept

Returns a string representation of the current object.

Returns
a string representation

Reimplemented from SharedEntity.