Either method would work. The decision should be based on how you plan to use the information (i.e. how you are processing the events).
Note that when requesting multiple tickers you would need to know which one you just received. E.g. if subscribed to MSFT and GOOG, and if you use the same object to receive events (like in your bottom sample code), you will not know which one has just arrived. Note, that OTTrade.symbol will NOT normally be set by opentick server! Therefore, you have to take care of preserving and passing this info yourself.
What I like to do is to use anonymous inner classes for this task, like this:
---------------------
In this example I routed the events to the same class that sets up the subscriptions. Naturally, you can route to some other class in a similar fashion.
But if you want to process each even by a separate handler object (like in your first sample), then just pass the ticker to the handler at its creation time, so that receiver knows which ticker it is processing:
---------------------
for(String ticker : tickers)
{
TickStreamCommand cmdTick = new TickStreamCommand();
cmdTick.setExchangeCode("@");
cmdTick.setSymbolCode(ticker);
TradeHandler tradeHandler = new TradeHandler(ticker); ////// <<== this line was changed
cmdTick.setTradeDelegate(tradeHandler);
cmdTick.setCompletionDelegate(tradeHandler);
IRequest request = connection.prepareRequest(cmdTick);
try
{
request.submit();
}
catch(Exception e)
{
log.error(msg);
}
}
-----------------------
Hope this helps,
-Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Mike
Whenever i subscribe for multipls symbols, I do something like following
for(String ticker : tickers)
{
TickStreamCommand cmdTick = new TickStreamCommand();
cmdTick.setExchangeCode("@");
cmdTick.setSymbolCode(ticker);
TradeHandler tradeHandler = new TradeHandler();
cmdTick.setTradeDelegate(tradeHandler);
cmdTick.setCompletionDelegate(tradeHandler);
IRequest request = connection.prepareRequest(cmdTick);
try
{
request.submit();
}
catch(Exception e)
{
log.error(msg);
}
}
Shall I create a new TradeHandler for every request ? Or can I pass the same tradehandler to all requests
On orther word, could I do the following:
TradeHandler tradeHandler = new TradeHandler();
for(String ticker : tickers)
{
TickStreamCommand cmdTick = new TickStreamCommand();
cmdTick.setExchangeCode("@");
cmdTick.setSymbolCode(ticker);
cmdTick.setTradeDelegate(tradeHandler);
cmdTick.setCompletionDelegate(tradeHandler);
IRequest request = connection.prepareRequest(cmdTick);
try
{
request.submit();
}
catch(Exception e)
{
log.error(msg);
}
}
Matt,
Either method would work. The decision should be based on how you plan to use the information (i.e. how you are processing the events).
Note that when requesting multiple tickers you would need to know which one you just received. E.g. if subscribed to MSFT and GOOG, and if you use the same object to receive events (like in your bottom sample code), you will not know which one has just arrived. Note, that OTTrade.symbol will NOT normally be set by opentick server! Therefore, you have to take care of preserving and passing this info yourself.
What I like to do is to use anonymous inner classes for this task, like this:
------------------------
for(final String ticker : tickers)
{
TickStreamCommand cmdTick = new TickStreamCommand();
cmdTick.setExchangeCode("@");
cmdTick.setSymbolCode(ticker);
cmdTick.setTradeDelegate(new IDataDelegate<OTTrade>() {
public void onData(OTTrade trade) {
handleTrade(ticker, trade);
}
});
cmdTick.setCompletionDelegate(new ICompletionDelegate() {
public void onDataEnd(OTError error) {
handleCompletion(ticker, error);
}
});
IRequest request = connection.prepareRequest(cmdTick);
try
{
request.submit();
}
catch(Exception e)
{
log.error(msg);
}
}
private void handleTrade(String ticker, OTTrade trade) {
...
}
private void handleCompletion(String ticker, OTError error) {
...
}
---------------------
In this example I routed the events to the same class that sets up the subscriptions. Naturally, you can route to some other class in a similar fashion.
But if you want to process each even by a separate handler object (like in your first sample), then just pass the ticker to the handler at its creation time, so that receiver knows which ticker it is processing:
---------------------
for(String ticker : tickers)
{
TickStreamCommand cmdTick = new TickStreamCommand();
cmdTick.setExchangeCode("@");
cmdTick.setSymbolCode(ticker);
TradeHandler tradeHandler = new TradeHandler(ticker); ////// <<== this line was changed
cmdTick.setTradeDelegate(tradeHandler);
cmdTick.setCompletionDelegate(tradeHandler);
IRequest request = connection.prepareRequest(cmdTick);
try
{
request.submit();
}
catch(Exception e)
{
log.error(msg);
}
}
-----------------------
Hope this helps,
-Mike