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, 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
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.
import java.util.Map;
import java.util.Set;
import com.caplin.datasource.DataSource;
import com.caplin.datasource.messaging.record.RecordType1Message;
import com.caplin.datasource.namespace.Namespace;
import com.caplin.datasource.publisher.DataProvider;
import com.caplin.datasource.publisher.DiscardEvent;
import com.caplin.datasource.publisher.Publisher;
import com.caplin.datasource.publisher.RequestEvent;
public class ExampleDataProvider implements DataProvider
{
private final Publisher publisher;
public ExampleDataProvider(DataSource datasource, Namespace namespace)
{
this.publisher = datasource.createActivePublisher(namespace, this);
}
@Override
public void onRequest(RequestEvent requestEvent)
{
// Subscribe to back end system that supplies the data.
// ...
// Create a message for the DataSource subject.
RecordType1Message message = publisher.getMessageFactory().createRecordType1Message(requestEvent.getSubject());
// Mark the message as containing an image.
message.setImage(true);
// Add data to the message.
message.setField("Bid", "1.23");
// Send the image message to the newly subscribed DataSource peer.
publisher.publishInitialMessage(message);
}
@Override
public void onDiscard(DiscardEvent discardEvent)
{
// Unsubscribe from back end system that supplies the data.
// ...
}
// Once this DataProvider has subscribed to the back end system,
// the back end pushes messages into this DataProvider via this message
// listener callback. The details of the callback parameters,
// and the transformation of the data needed to populate the
// DataSource message, depend on the nature of the back end system.
public void onDataUpdate(String subject, Map messageData)
{
// Create a message for the appropriate DataSource Subject.
RecordType1Message message = publisher.getMessageFactory().createRecordType1Message(subject);
// Transform the back end update's data into DataSource message fields.
Set mapKeys = messageData.keySet();
for(String name : mapKeys)
{
message.setField(name, messageData.get(name));
}
//Publish the message
publisher.publishToSubscribedPeers(message);
}
}
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.
Modifier and Type | Method and 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 the
DataProvider that a new request has been received and
it should start sending data. |
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 when onRequest
is called
depends on the type of Publisher
that is being used by
the DataSource application.
If the Publisher
is an
ActivePublisher
, the DataProvider
should
perform the following tasks when this method is called:
RequestEvent.getSubject()
.DataProvider
should call
Publisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer.DataProvider
can simply
call
Publisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer.
If the Publisher
is a
CompatibilityPublisher
the DataProvider
should perform the following tasks when this method is called:
RequestEvent.getSubject()
.Peer
making the request should be retrieved by calling
RequestEvent.getPeer()
.Peer
is subscribed to the subject should be recorded in a
data structure.DataProvider
should call
Publisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer.DataProvider
can simply call
Publisher.publishInitialMessage(com.caplin.datasource.messaging.Message)
to publish the current image of the data to the subscribing peer.
requestEvent
- The event that describes the request (which peer and which subject).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 when onDiscard
is called
depends on the type of Publisher
that is being used by
the DataSource application.
If the Publisher
is an
ActivePublisher
, the DataProvider
should
perform the following tasks when this method is called:
RequestEvent.getSubject()
.
If the Publisher
is a
CompatibilityPublisher
the DataProvider
should perform the following tasks when this method is called:
RequestEvent.getSubject()
.Peer
making the request should be retrieved by calling
RequestEvent.getPeer()
.onRequest(RequestEvent)
.
discardEvent
- The event that describes the discard (which peer and which subject).Please send bug reports and comments to Caplin support