Interface DataProvider
-
public 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, aDataProvider
implementation requires a reference to itsPublisher
so it may use that publisher to send messages. The example shows the recommended way to do this, which is to create thePublisher
in the constructor of theDataProvider
.Note: if your
Publisher
is a com.caplin.datasource.publisher.BroadcastPublisher, you do not need to implement aDataProvider
for it. This is because the connected peers (to which the broadcast data updates are sent) do not send theDataSource
any subscription or discard requests for the broadcast subjects, so noDataProvider
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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
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.
-
-
-
Method Detail
-
onRequest
void onRequest(RequestEvent requestEvent)
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
void onDiscard(DiscardEvent discardEvent)
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
-
-