com.dxfeed.api
Class DXFeed

java.lang.Object
  extended by com.dxfeed.api.DXFeed

public abstract class DXFeed
extends Object

Main entry class for dxFeed API (read it first).

Sample usage

This section gives sample usage scenarios.

Default singleton instance

There is a singleton instance of the feed that is returned by 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 "dxfeed.address" system property or by putting it into "dxfeed.properties" file on JVM classpath. dxFeed API samples come with a ready-to-use "dxfeed.properties" file that contains an address of dxFeed demo feed at "demo.dxfeed.com:7300".

Subscribe for single event type

The following code creates listener that prints mid price for each quote and subscribes for quotes on SPDR S&P 500 ETF symbol:

 DXFeedSubscription<Quote> sub = DXFeed.getInstance().createSubscription(Quote.class);
 sub.addEventListener(new DXFeedEventListener<Quote>() {
     public void eventsReceived(List<Quote> quotes) {
         for (Quote quote : quotes)
             System.out.println("Mid = " + (quote.getBidPrice() + quote.getAskPrice()) / 2);
     }
 });
 sub.addSymbols("SPY");
Note, that order of calls is important here. By attaching listeners first and then setting subscription we ensure that the current quote gets received by the listener. See DXFeedSubscription.addSymbols for details.

Subscribe for multiple event types

The following code creates listener that prints each received event and subscribes for quotes and trades on SPDR S&P 500 ETF symbol:

 DXFeedSubscription<MarketEvent> sub = DXFeed.getInstance().<MarketEvent>createSubscription(Quote.class, Trade.class);
 sub.addEventListener(new DXFeedEventListener<MarketEvent>() {
     public void eventsReceived(List<MarketEvent> events) {
         for (MarketEvent event : events)
             System.out.println(event);
     }
 });
 sub.addSymbols("SPY");

Subscribe for event and query periodically its last value

The following code subscribes for trades on SPDR S&P 500 ETF symbol and prints last trade every second.

 DXFeedSubscription<Trade> sub = DXFeed.getInstance().createSubscription(Trade.class);
 sub.addSymbols("SPY");
 while (true) {
     System.out.println(feed.getLastEvent(new Trade("SPY")));
     Thread.sleep(1000);
 }

Threads and locks

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

Implementation details

dxFeed API is implemented on top of QDS. dxFeed API classes itself are in "dxfeed-api.jar", but their implementation is in "qds.jar". You need have "qds.jar" in your classpath in order to use dxFeed API.


Constructor Summary
protected DXFeed()
          Protected constructor for implementations of this class only.
 
Method Summary
abstract  void attachSubscription(DXFeedSubscription<?> subscription)
          Attaches the given subscription to this feed.
<E> DXFeedSubscription<E>
createSubscription(Class<? extends E>... eventTypes)
          Creates new subscription for multiple event types that is attached to this feed.
<E> DXFeedSubscription<E>
createSubscription(Class<? extends E> eventType)
          Creates new subscription for a single event type that is attached to this feed.
<E extends TimeSeriesEvent>
DXFeedTimeSeriesSubscription<E>
createTimeSeriesSubscription(Class<? extends E>... eventTypes)
          Creates new time series subscription for multiple event types that is attached to this feed.
<E extends TimeSeriesEvent>
DXFeedTimeSeriesSubscription<E>
createTimeSeriesSubscription(Class<? extends E> eventType)
          Creates new time series subscription for a single event type that is attached to this feed.
abstract  void detachSubscription(DXFeedSubscription<?> subscription)
          Detaches the given subscription from this feed.
static DXFeed getInstance()
          Returns a default application-wide singleton instance of DXFeed.
abstract
<E extends LastingEvent>
E
getLastEvent(E event)
          Returns the last event for the specified symbol and event type.
<E extends LastingEvent>
Collection<E>
getLastEvents(Collection<E> events)
          Returns the last events for the specified symbols and event types.
protected static
<E> void
processEvents(DXFeedSubscription<E> subscription, List<E> events)
          Processes received events.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DXFeed

protected DXFeed()
Protected constructor for implementations of this class only.

Method Detail

getInstance

public static DXFeed getInstance()
Returns a default application-wide singleton instance of DXFeed. Most applications use only a single data-source and should rely on this method to get one. This is a shortcut to DXEndpoint.getInstance(DXEndpoint.Role.FEED).getFeed().


createSubscription

public final <E> DXFeedSubscription<E> createSubscription(Class<? extends E> eventType)
Creates new subscription for a single event type that is attached to this feed. For multiple event types in one subscription use createSubscription(Class[]) createSubscription(Class... eventTypes)} This method creates new DXFeedSubscription and invokes attachSubscription(com.dxfeed.api.DXFeedSubscription).

Type Parameters:
E - the type of events.
Parameters:
eventType - the class of event types.
See Also:
DXFeedSubscription.DXFeedSubscription(Class), attachSubscription(DXFeedSubscription)

createSubscription

public final <E> DXFeedSubscription<E> createSubscription(Class<? extends E>... eventTypes)
Creates new subscription for multiple event types that is attached to this feed. For a single event type use createSubscrtiption(Class eventType) This method creates new DXFeedSubscription and invokes attachSubscription(com.dxfeed.api.DXFeedSubscription).

Type Parameters:
E - the type of events.
Parameters:
eventTypes - the classes of event types.
See Also:
DXFeedSubscription.DXFeedSubscription(Class[]), attachSubscription(DXFeedSubscription)

createTimeSeriesSubscription

public final <E extends TimeSeriesEvent> DXFeedTimeSeriesSubscription<E> createTimeSeriesSubscription(Class<? extends E> eventType)
Creates new time series subscription for a single event type that is attached to this feed. For multiple event types in one subscription use createTimeSeriesSubscription(Class[]) createTimeSeriesSubscription(Class... eventTypes)} This method creates new DXFeedTimeSeriesSubscription and invokes attachSubscription(com.dxfeed.api.DXFeedSubscription).

Type Parameters:
E - the type of events.
Parameters:
eventType - the class of event types.
See Also:
DXFeedTimeSeriesSubscription.DXFeedTimeSeriesSubscription(Class), attachSubscription(DXFeedSubscription)

createTimeSeriesSubscription

public final <E extends TimeSeriesEvent> DXFeedTimeSeriesSubscription<E> createTimeSeriesSubscription(Class<? extends E>... eventTypes)
Creates new time series subscription for multiple event types that is attached to this feed. For a single event type use createTimeSeriesSubscription(Class eventType) This method creates new DXFeedTimeSeriesSubscription and invokes attachSubscription(com.dxfeed.api.DXFeedSubscription).

Type Parameters:
E - the type of events.
Parameters:
eventTypes - the classes of event types.
See Also:
DXFeedTimeSeriesSubscription.DXFeedTimeSeriesSubscription(Class[]), attachSubscription(DXFeedSubscription)

attachSubscription

public abstract void attachSubscription(DXFeedSubscription<?> subscription)
Attaches the given subscription to this feed. This method does nothing if the corresponding subscription is already attached to this feed.

This feed publishes data to the attached subscription. Application can attach DXFeedEventListener via DXFeedSubscription.addEventListener(com.dxfeed.api.DXFeedEventListener) to get notified about data changes and can change its data subscription via DXFeedSubscription methods.

Implementation notes

This method adds a non-serializable ObservableSubscriptionChangeListener for the given subscription via DXFeedSubscription.addChangeListener(com.dxfeed.api.osub.ObservableSubscriptionChangeListener) method. Attachment is lost when subscription is serialized and deserialized.

Parameters:
subscription - the subscription.
Throws:
NullPointerException - if the subscription is null.
See Also:
DXFeedSubscription

detachSubscription

public abstract void detachSubscription(DXFeedSubscription<?> subscription)
Detaches the given subscription from this feed. This method does nothing if the corresponding subscription is not attached to this feed.

Implementation notes

This method removes ObservableSubscriptionChangeListener from the given subscription via DXFeedSubscription.removeChangeListener(com.dxfeed.api.osub.ObservableSubscriptionChangeListener) method.

Parameters:
subscription - the subscription.
Throws:
NullPointerException - if the subscription is null.
See Also:
DXFeedSubscription

getLastEvent

public abstract <E extends LastingEvent> E getLastEvent(E event)
Returns the last event for the specified symbol and event type. This method works only for event types that implement LastingEvent marker interface. This method goes not make any remote calls to the uplink data provider. It just retrieves last received event from the local cache of this feed. The events are stored in the cache only if there is some attached DXFeedSubscription that is subscribed to the corresponding symbol and event type WildcardSymbol.ALL subscription does not count for that purpose.

This method fills in the values for the last event into the event argument. If the last event is not available for any reason (no subscription, no connection to uplink, etc). then the event object is not changed. This method always returns the same event instance that is passed to it as an argument.

Note, that this method does not work when DXEndpoint was created with STREAM_FEED role.

Type Parameters:
E - the type of event.
Parameters:
event - the event.
Returns:
the same event.
Throws:
NullPointerException - if the event is null.

getLastEvents

public <E extends LastingEvent> Collection<E> getLastEvents(Collection<E> events)
Returns the last events for the specified symbols and event types. This is a bulk version of getLastEvent method.

Note, that this method does not work when DXEndpoint was created with STREAM_FEED role.

Type Parameters:
E - the type of event.
Parameters:
events - the collection of events.
Returns:
the same collection of events.
Throws:
NullPointerException - if the collection or any event in it is null.

processEvents

protected static <E> void processEvents(DXFeedSubscription<E> subscription,
                                        List<E> events)
Processes received events. This methods invokes DXFeedEventListener.eventsReceived(java.util.List) on all installed event listeners. This is a protected method for use by DXFeed implementation classes only.

Type Parameters:
E - the type of events.
Parameters:
events - the list of received events.


Copyright © 2013 Devexperts. All Rights Reserved.