Interface DataProvider
Interface that must be implemented in order to provide data updates to DataSource peers. It contains callbacks that handle subscription and discard requests from peers.
The following example shows a skeleton implementation of the DataProvider
interface.
Typically, a DataProvider
implementation requires a reference to its
Publisher
so it may use that publisher to send messages.
The example shows the recommended way to do this, which is to create the
Publisher
in the constructor of the
DataProvider
.
Note: if your Publisher
is a
com.caplin.datasource.publisher.BroadcastPublisher, you do not need to implement a
DataProvider
for it. This is because the connected peers (to which the broadcast
data updates are sent) do not send the DataSource
any subscription or discard
requests for the broadcast subjects, so no DataProvider
is needed to handle such
events.
Note: the DataProvider
methods are not called on a dedicated worker thread.
Therefore, if any of these methods are likely take a relatively long time to execute, they should
be coded to run in a separate thread.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
onDiscard
(DiscardEvent discardEvent) Callback that informs the DataProvider that an earlier requested subject has now been discarded.void
onRequest
(RequestEvent requestEvent) Callback that informs theDataProvider
that a new request has been received and it should start sending data.default void
setPublisher
(Publisher publisher) Sets a publisher for theDataProvider
to use when publishing data.
-
Method Details
-
setPublisher
Sets a publisher for the
DataProvider
to use when publishing data.This setter should be implemented when the
DataProvider
is defined as an anonymous inner class in order to make the publisher accessible within theonRequest(RequestEvent)
andonDiscard(DiscardEvent)
methods.Example:
Publisher publisher = dataSource.createActivePublisher( new PrefixNamespace("/FX/"), new DataProvider() { Publisher pricePublisher;
- Parameters:
publisher
- ThePublisher
for thisDataProvider
to use when publishing data.
-
onRequest
Callback that informs the
DataProvider
that a new request has been received and it should start sending data.The action that a
DataProvider
should take whenonRequest
is called depends on the type ofPublisher
that is being used by the DataSource application.If the
Publisher
is anActivePublisher
, theDataProvider
should perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject()
. - If that DataSource is not already subscribed to the back end system that supplies the data for this subject, the DataSource should make a subscription for the subject. *
- When data is received from the back end system for the subject, the
DataProvider
should callPublisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer. - If the DataSource is already subscribed to the back end system for this subject and the
DataSource has cached the latest values for the subject (this could happen if, for example,
another peer has already requested the subject), then the
DataProvider
can simply callPublisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer.
If the
Publisher
is aCompatibilityPublisher
theDataProvider
should perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject()
. - The
Peer
making the request should be retrieved by callingRequestEvent.getPeer()
. - The fact that the
Peer
is subscribed to the subject should be recorded in a data structure. - If that DataSource is not already subscribed to the back end system that supplies the data for this subject, the DataSource should make a subscription for the subject.
- When data is received from the back end system for the subject, the
DataProvider
should callPublisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer. - If the DataSource is already subscribed to the back end system for this subject and the
DataSource has cached the latest values for the subject, then the
DataProvider
can simply callPublisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer. - The data structure that records which peers are subscribed to which subjects will be used later to determine when the DataSource should unsubscribe from the back end system that supplies the data.
- Parameters:
requestEvent
- The event that describes the request (which peer and which subject).
- The subject should be retrieved by calling
-
onDiscard
Callback that informs the DataProvider that an earlier requested subject has now been discarded.
The action that a
DataProvider
should take whenonDiscard
is called depends on the type ofPublisher
that is being used by the DataSource application.If the
Publisher
is anActivePublisher
, theDataProvider
should perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject()
. - The DataSource should unsubscribe from the back end system that supplies the data for this subject.
- The DataSource should stop publishing updates for this subject.
If the
Publisher
is aCompatibilityPublisher
theDataProvider
should perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject()
. - The
Peer
making the request should be retrieved by callingRequestEvent.getPeer()
. - The data structure that records which peers are subscribed to which subjects should be
retrieved (see
onRequest(RequestEvent)
. - The peer should be removed from the list of peers that are subscribed to this subject.
- If there are still peers subscribed to the subject, no further action is necessary.
- If there are now no peers subscribed to the subject then the DataSource should unsubscribe from the back end system that supplies the data for this subject, and stop publishing updates for this subject.
- Parameters:
discardEvent
- The event that describes the discard (which peer and which subject).
- The subject should be retrieved by calling
-