I have codes working okay for sending a single historical data request, but want to refactor it to be able to send multiple requests in different threads.
Of now, the EClientSocket, EReaderSignal, EReader are instance variables in the HistoricalData class
So the TWS connection belongs to the HistoricalData object,
My question is, shouldn't the connection belong to the class instead or each requesting instance needs its own connection? If I create 3 HistoricalData instances, each opens a thread and sends an asynchronous request for Bid/Ask/Trades, do I need 3 EClientSocket instances and each connecting individually? And there will be one message queue per EClientSocket obj?
Conceptually, I would have thought there is one connection and each thread sharing it. But given the EWrapper callbacks are instance methods, the EReader has to know which instance each message in the queue belongs to, so each instance needs its own socket and ereader? Can't the EReader identifies the instance by some request ID?
How about EJavaSignal? Can it be static?
Such as below, but I guess no as EReaderSignal and EReader are closely connected?
Of now, the EClientSocket, EReaderSignal, EReader are instance variables in the HistoricalData class
Code:
private EClientSocket client;
private EReaderSignal readerSignal;
private EReader reader;
So the TWS connection belongs to the HistoricalData object,
Code:
private void openConnection(int port) {
this.readerSignal = new EJavaSignal();
this.client = new EClientSocket(this, this.readerSignal);
this.client.eConnect("127.0.0.1", port, 0);
this.reader = new EReader(this.client, this.readerSignal);
this.reader.start(); //open a new thread to listen to incoming messages
}
My question is, shouldn't the connection belong to the class instead or each requesting instance needs its own connection? If I create 3 HistoricalData instances, each opens a thread and sends an asynchronous request for Bid/Ask/Trades, do I need 3 EClientSocket instances and each connecting individually? And there will be one message queue per EClientSocket obj?
Conceptually, I would have thought there is one connection and each thread sharing it. But given the EWrapper callbacks are instance methods, the EReader has to know which instance each message in the queue belongs to, so each instance needs its own socket and ereader? Can't the EReader identifies the instance by some request ID?
How about EJavaSignal? Can it be static?
Such as below, but I guess no as EReaderSignal and EReader are closely connected?
Code:
static private EReaderSignal signal = new EJavaSignal();
this.client = new EClientSocket(this, signal);
this.reader = new EReader(this.client, signal);
Last edited: