public interface Publisher
An instance of Publisher
publishes messages to remote DataSource peers on behalf of
a DataProvider
. It also provides access to a MessageFactory
that creates the
messages to be published.
DataSource for Java includes several implementations of Publisher
that you can use
in your DataSource application: see BroadcastPublisher
, ActivePublisher
and
CompatibilityPublisher
. To use one of these Publisher
s, call the appropriate
"create publisher" method of DataSource
:
DataSource.createActivePublisher(com.caplin.datasource.namespace.Namespace, DataProvider)
DataSource.createBroadcastPublisher(com.caplin.datasource.namespace.Namespace)
DataSource.createCompatibilityPublisher(com.caplin.datasource.namespace.Namespace, DataProvider)
These three child interfaces declare no methods further to those declared in
Publisher
and currently only serve to tag each Publisher
type. However,
it is possible that future versions of DataSource for Java may add publisher-type specific
methods to these interfaces if deemed appropriate or useful.
The following example shows how to create an active publisher:
import com.caplin.datasource.DataSource;
import com.caplin.datasource.namespace.Namespace;
import com.caplin.datasource.namespace.PrefixNamespace;
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 CreateActivePublisherExample
{
public static void createActivePublisherExample(DataSource dataSource)
{
// Create a simple Namespace that matches subjects starting with the "/FX".
Namespace fxNamespace = new PrefixNamespace("/FX");
// Create a DataProvider to handle requests and discards from clients
new MyDataProvider(dataSource, fxNamespace);
}
static class MyDataProvider implements DataProvider
{
@SuppressWarnings("unused")
private final Publisher publisher;
public MyDataProvider(DataSource dataSource, Namespace nameSpace)
{
// Ask the DataSource to create a publisher that uses the fxNameSpace.
publisher = dataSource.createActivePublisher(nameSpace, this);
}
@Override
public void onRequest(RequestEvent requestEvent)
{
// Respond to client request by publishing data using the publisher.
}
@Override
public void onDiscard(DiscardEvent discardEvent)
{
// Respond to client discard by stopping publishing data.
}
}
}
The following example shows a Publisher
being created by calling the
DataSource.createBroadcastPublisher(Namespace)
method of the
DataSource. Note that no DataProvider
is needed for a
BroadcastPublisher
. 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 com.caplin.datasource.DataSource;
import com.caplin.datasource.messaging.record.RecordMessage;
import com.caplin.datasource.namespace.Namespace;
import com.caplin.datasource.namespace.PrefixNamespace;
import com.caplin.datasource.publisher.Publisher;
public class CreateBroadcastPublisherExample
{
public static void createBroadcastPublisherExample(DataSource dataSource)
{
// Create a simple Namespace that matches subjects starting with the "/FX".
Namespace fxNamespace = new PrefixNamespace("/FX");
// Ask the DataSource to create a publisher that uses the fxNameSpace.
Publisher publisher = dataSource.createBroadcastPublisher(fxNamespace);
// Create a record message and broadcast it
RecordMessage message = publisher.getMessageFactory().createRecordType1Message("/FX/GBPUSD");
message.setField("Bid", "1.5905");
message.setField("Ask", "1.5920");
publisher.publishInitialMessage(message);
}
}
Modifier and Type | Method and Description |
---|---|
MessageFactory |
getMessageFactory()
Gets the
MessageFactory used to create the messages that are published via this
Publisher . |
void |
publishInitialMessage(Message message)
Publishes the initial image of the data for a subject to peers that have just requested
(subscribed to) that subject.
|
void |
publishMappingMessage(MappingMessage mappingMessage)
Publishes a message that instructs the peer that it should request a different subject and map
the updates across.
|
void |
publishSubjectErrorEvent(SubjectErrorEvent subjectErrorEvent)
Publishes to all peers subscribed to a subject an event detailing an error in the subscription
for that subject.
|
void |
publishSubjectStatusEvent(SubjectStatusEvent subjectStatusEvent)
Publishes to all subscribed peers an event about the change in status of a subject.
|
void |
publishToSubscribedPeers(Message message)
Publishes a message to subscribed peers (that is, the peers that have already received an
initial image).
|
MessageFactory getMessageFactory()
MessageFactory
used to create the messages that are published via this
Publisher
.void publishInitialMessage(Message message)
message
- The message.void publishToSubscribedPeers(Message message)
message
- The message to be published. This may be an image or an update.void publishMappingMessage(MappingMessage mappingMessage)
mappingMessage
- The message.void publishSubjectErrorEvent(SubjectErrorEvent subjectErrorEvent)
DataProvider
will use this to notify the remote peers
that the data they are subscribed to is no longer available.subjectErrorEvent
- The error event to be published.void publishSubjectStatusEvent(SubjectStatusEvent subjectStatusEvent)
subjectStatusEvent
- The subject status event to be published.Please send bug reports and comments to Caplin support