Interface DataProvider
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 TypeMethodDescriptionvoidonDiscard(DiscardEvent discardEvent) Callback that informs the DataProvider that an earlier requested subject has now been discarded.voidonRequest(RequestEvent requestEvent) Callback that informs theDataProviderthat a new request has been received and it should start sending data.default voidsetPublisher(Publisher publisher) Sets a publisher for theDataProviderto use when publishing data.
-
Method Details
-
setPublisher
Sets a publisher for theDataProviderto use when publishing data.This setter should be implemented when the
DataProvideris 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- ThePublisherfor thisDataProviderto use when publishing data.
-
onRequest
Callback that informs theDataProviderthat a new request has been received and it should start sending data.The action that a
DataProvidershould take whenonRequestis called depends on the type ofPublisherthat is being used by the DataSource application.If the
Publisheris anActivePublisher, theDataProvidershould 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
DataProvidershould 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
DataProvidercan simply callPublisher.publishInitialMessage(com.caplin.datasource.messaging.Message)to publish the current image of the data to the subscribing peer.
If the
Publisheris aCompatibilityPublishertheDataProvidershould perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject(). - The
Peermaking the request should be retrieved by callingRequestEvent.getPeer(). - The fact that the
Peeris 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
DataProvidershould 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
DataProvidercan 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
DataProvidershould take whenonDiscardis called depends on the type ofPublisherthat is being used by the DataSource application.If the
Publisheris anActivePublisher, theDataProvidershould 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
Publisheris aCompatibilityPublishertheDataProvidershould perform the following tasks when this method is called:- The subject should be retrieved by calling
RequestEvent.getSubject(). - The
Peermaking 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
-