Sample code for using multiple contexts to receive market data
When using MDA, when you call an action that takes a context as a parameter you must specify a reference to the main context. However, you can use multiple contexts to receive and operate on market data. The following code provides an example of using multiple contexts.
using com.apama.session.SessionHandlerFactory;
using com.apama.session.SessionHandler;
using com.apama.md.TradeSubscriberFactory;
using com.apama.md.TradeSubscriber;
using com.apama.md.client.CurrentTradeInterface;
monitor SubscriberExample3 {
action onload() {
context mainContext := context.current();
// Spawn to a separate context for each symbol
spawn init(mainContext,"SYM1") to context("SYM1");
spawn init(mainContext,"SYM2") to context("SYM2");
spawn init(mainContext,"SYM3") to context("SYM3");
}
action init(context mainContext, string symbol) {
// Connect to a session
SessionHandler sessionHandler := SessionHandlerFactory.connect(
mainContext, "MySession", "MyTransport");
// Subscribe to depth updates for each context's symbol.
// This is a way to use the factory to subscribe
// without returning a subscriber object.
TradeSubscriberFactory.create(
sessionHandler).subscribe(symbol);
}
// Process the trade
on all com.apama.md.T() {
...
}
}