com.dxfeed.api
Class DXPublisher

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

public abstract class DXPublisher
extends Object

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

Sample usage

This section gives sample usage scenarios.

Default singleton instance

There is a singleton instance of the publisher 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 "dxpublisher.address" system property or by putting it into "dxpublisher.properties" file on JVM classpath.

Publish a single event

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

 Quote quote = new Quote("A:TEST");
 quote.setBidPrice(100);
 quote.setAskPrice(101);
 DXPublisher.getInstance().publishEvents(Arrays.asList(quote));

Monitor subscription and publish profile for any test symbol

The following code monitor subscription for Profile events and for any subscription on the string symbols that end with ":TEST" string generates and publishes a profile.

 final DXPublisher publisher = DXPublisher.getInstance();
 publisher.getSubscription(Profile.class).addChangeListener(new ObservableSubscriptionChangeListener() {
     public void symbolsAdded(Set<?> symbols) {
         List<Profile> events = new ArrayList<Profile>();
         for (Object symbol : symbols) {
             if (symbol instanceof String) {
                 String s = (String)symbol;
                 if (s.endsWith(":TEST")) {
                     Profile profile = new Profile(s);
                     profile.setDescription("Test symbol");
                     events.add(profile);
                 }
             }
         }
         publisher.publishEvents(events);
     }

     public void symbolsRemoved(Set<?> symbols) {
         // nothing to do here
     }

     public void subscriptionClosed() {
         // nothing to do here
     }
 });

Threads and locks

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


Constructor Summary
protected DXPublisher()
          Protected constructor for implementations of this class only.
 
Method Summary
static DXPublisher getInstance()
          Returns a default application-wide singleton instance of DXPublisher.
abstract
<E> ObservableSubscription<E>
getSubscription(Class<E> eventType)
          Returns observable set of subscribed symbols for the specified event type.
abstract  void publishEvents(Collection<?> events)
          Publishes events to the corresponding feed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DXPublisher

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

Method Detail

getInstance

public static DXPublisher getInstance()
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

public abstract void publishEvents(Collection<?> events)
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.

Parameters:
events - the collection of events to publish.

getSubscription

public abstract <E> ObservableSubscription<E> getSubscription(Class<E> eventType)
Returns observable set of subscribed symbols for the specified event type. Note, that subscription is represented by object symbols. Check the type of each symbol in ObservableSubscription using instanceof operation.

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.

Type Parameters:
E - the type of event.
Parameters:
eventType - the class of event.
Returns:
Observable subscription for the specified event type.


Copyright © 2013 Devexperts. All Rights Reserved.