Apama Capital Markets Foundation Documentation : Capital Markets Foundation : Market Data Management : Market data subscribers : Subscribing to market data : Subscribing to news
Subscribing to news
Working with news events is different from working with the other market data event types in two ways:
*A particular news datasource might send out news stories that have parameters that are not captured in a basic news (com.apama.md.N) event. When this is the case there is a schema that provides metadata about extra parameters. You can programmatically access this schema to obtain the format of the extra parameters.
*A news event can be deleted by the source that published it.
In addition to the com.apama.md.N event interface, which is the basic news datastream event, CMF provides the following event interfaces for working with news. See the ApamaDoc for details.
Event Interface
Purpose
Description
com.apama.md.ND
News deletion
Contains fields for stories that have been deleted. Like the com.apama.md.N event, it contains a newsId field. In an ND event the value of its newsId field might match the value of the newsId field in a previous N event. It is possible, however, that this is not the case and that the original news item was not received because of filtering. An ND event also contains fields for the story's source (sessionId), symbol, and data.
com.apama.md.NSI
Metadata for one extra parameter
A news schema item describes an extra parameter that is associated with a news event. It contains fields for the parameter data type, parameter description, and whether this item is an addition, change, or deletion from the schema.
com.apama.md.NS
Schema that contains metadata for all extra news parameters
A schema is a dictionary of NSI events. An NS event contains the schema for the extra parameters associated with any news story received from the datasource identified by the NS.sessionId field.
com.apama.md.NSD
Schema update
A new schema update event also contains a dictionary of NSI events. You use NSD events to apply schema changes to an NS event. Typically, it is expected that the schema for extra parameters will be initialized as part of adapter set-up and you will rarely need to update it.
Because there is a dedicated event type for news stories that have been deleted (com.apama.md.ND), it is possible to specifically listen for them. However, when a CMF application receives a news-delete event there is no callback that is automatically invoked. Update callbacks are executed for only news events.
The following sample code shows a subscription to receive all news events, regardless of symbol. When a new story is received, this sample sets up a listener for a deletion event for that story.
using com.apama.session.SessionHandlerFactory;
using com.apama.session.SessionHandler;
using com.apama.md.NewsSubscriberFactory;
using com.apama.md.NewsSubscriber;
using com.apama.md.ND;
using com.apama.md.DatastreamConstants;
using com.apama.md.client.CurrentNewsInterface;
 
monitor NewsSubscriberExample1 {
context mainContext := context.current();
 
action onload() {
// The RFA adapter exposes the N2_UBMS feed as a separate session.
NewsSubscriber newsSubscriber :=
NewsSubscriberFactory.create(
SessionHandlerFactory.connect(
mainContext, "N2_UBMS", "RfaAdapter"));
 
// Subscribing to a blank symbol subscribes to all news.
// The specified update callback is executed each time
// a news event is received.
newsSubscriber.subscribeCb("", onSymbolNews);
}
 
action onSymbolNews(CurrentNewsInterface newsIface) {
 
// Process new or updated news events.
...
 
// For new news stories, listen for deletion of that story.
 
if (newsIface.getUpdateType() = DatastreamConstants.UPDATETYPE_ADD)
then {
ND newsDelete;
on all ND(newsId=newsIface.getNewsId()):newsDelete {
// Process newsDelete.
...
}
}
}
}
A news (com.apama.md.N) event has a dictionary type data field. If the adapter supplies any extra news event parameters the dictionary in the data field contains metadata for the extra parameters. This dictionary is referred to as the schema. The schema provides a description for any possible extra parameter for the whole feed, not just individual news stories. You might have entries in the schema that do not appear in every news story. To obtain the schema, execute CurrentNewsInterface.getNewsSchema().
The following sample code shows how to obtain the description for an item in the schema data dictionary.
using com.apama.session.SessionHandlerFactory;
using com.apama.session.SessionHandler;
using com.apama.md.NewsSubscriberFactory;
using com.apama.md.NewsSubscriber;
using com.apama.md.NSI;
using com.apama.md.client.CurrentNewInterface;
 
monitor NewsSubscriberExample2 {
 
context mainContext := context.current();
 
action onload() {
NewsSubscriber newsSubscriber := NewsSubscriberFactory.create(
SessionHandlerFactory.connect(
mainContext, "N2_UBMS", "RfaAdapter"));
 
// Subscribing to all news and specify an updated callback
newsSubscriber.subscribeCb("", onAllNewsUpdate);
}
 
action onAllNewsUpdate(CurrentNewsInterface newsUpdate) {
 
dictionary<string, string> newsData := newsUpdate.getData();
 
string dataKey;
for dataKey in newsData.keys() {
NSI newsSchemaItem := newsUpdate.getNewsSchemaField(dataKey);

// Get the data type and description for the news data field.
...
}
}
}
The next sample uses a connection callback to set up listeners for news stories for a particular symbol at a particular value.
using com.apama.session.SessionHandlerFactory;
using com.apama.session.SessionHandler;
using com.apama.md.NewsSubscriberFactory;
using com.apama.md.NewsSubscriber;
using com.apama.md.N;
 
monitor NewsSubscriberExample3 {
 
context mainContext := context.current();
 
action onload() {
// Connect to the session and specify a connection callback.
SessionHandler handler := SessionHandlerFactory.connectCb(
mainContext, "N2_UBMS", "RfaAdapter", onSessionConnected);
}
 
action onSessionConnected(SessionHandler handler) {
 
string symbol := "496";
 
// Subscribe to U.S. Non-farm payrolls.
NewsSubscriber newsSubscriber := NewsSubscriberFactory.
subscribe(handler, symbol);
 
// Listen for U.S. Non-farm payroll updates on this session
// with an actual value > 200,000.
N newsStory;
on all N(sessionId=handler.getSessionInfo().getSessionId(),
symbol=symbol, actualValue > 200000):newStory {
 
// Process news update.
}
}
}
Copyright © 2013-2017 Software AG, Darmstadt, Germany. (Innovation Release)

Product LogoContact Support   |   Community   |   Feedback