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

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

#include <DXPublisher.hpp>

+ Inheritance diagram for dxfcpp::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 dxfcpp::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) noexcept
 Publishes events to the corresponding feed.
 
std::string toString () const noexcept override
 Returns a string representation of the current object.
 
- Public Member Functions inherited from dxfcpp::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 dxfcpp::Entity
virtual ~Entity () noexcept=default
 The default virtual d-tor.
 

Static Public Member Functions

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

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()

std::shared_ptr< DXPublisher > dxfcpp::DXPublisher::getInstance ( )
staticnoexcept

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().

◆ publishEvents() [1/4]

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

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]

void dxfcpp::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:93
static std::shared_ptr< DXEndpoint > create()
Creates an endpoint with FEED role.
Definition DXEndpoint.hpp:592
@ 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:45
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 dxfcpp::DXPublisher::publishEvents ( std::initializer_list< std::shared_ptr< EventType > > events)
inlinenoexcept

Publishes events to the corresponding feed.

Parameters
collectionThe collection of events to publish.

◆ publishEvents() [4/4]

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

Publishes an event to the corresponding feed.

Parameters
eventThe event to publish.

◆ toString()

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

Returns a string representation of the current object.

Returns
a string representation

Reimplemented from dxfcpp::SharedEntity.