interface IO2GResponseListener
Brief
The interface provides method signatures to process notifications about request completions, request failures and tables updates.
Details
The interface must be implemented by an application in order to process the trading server responses.
Responses come from the trading server in two ways:
A response provided by the server as an answer to a user request.
Requests may be successfully completed or failed:
- to process notifications about requests successful completion, you must use the onRequestCompleted method;
- to process notifications about requests failures, you must use the onRequestFailed method.
A response provided by the server as a result of trading tables updates.
To process these notifications, you must use the
onTablesUpdates method.
For complete details of processing the response contents, refer to the O2GResponse class documentation.
If you want to use the methods of the IO2GResponseListener
interface, you must create a class that implements the interface. For
example,
public class ResponseListener implements IO2GResponseListener { }
An instance of the class implementing the IO2GResponseListener
interface must be subscribed to a session object. It is
accomplished by calling the
O2GSession
.subscribeResponse
method.
For example,
mSession = O2GTransport.createSession();
ResponseListener responseListener = new ResponseListener(mSession);
mSession.subscribeResponse(responseListener);
The subscribed instance must be unsubscribed from a session object after calling
the O2GSession.logout method, but before calling the
O2GSession.dispose method. It is accomplished by calling the
O2GSession
.unsubscribeResponse
method.
For example,
mSession.logout();
mSession.unsubscribeResponse(responseListener);
mSession.dispose();
For the interface implementation details, see the example below.
Example
Process notifications about the request to get offers and live offers updates [show]
Process notifications about the request to get offers and live offers updates [hide]
package getoffers;
import com.fxcore2.IO2GResponseListener;
import com.fxcore2.O2GResponse;
import com.fxcore2.O2GResponseReaderFactory;
import com.fxcore2.O2GResponseType;
import com.fxcore2.O2GOffersTableResponseReader;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GOfferRow;
import com.fxcore2.O2GTableType;
import com.fxcore2.O2GTablesUpdatesReader;
public class ResponseListener implements IO2GResponseListener {
// Session and request variables
private O2GSession mSession = null;
private String mRequestID = "";
// Constructor
public ResponseListener(O2GSession session) {
mSession = session;
}
//Set request
public void setRequest(String requestID) {
mRequestID = requestID;
}
// Implementation of IO2GResponseListener interface public method onRequestCompleted
public void onRequestCompleted(String requestID, O2GResponse response) {
if (requestID.equals(mRequestID)) {
O2GResponseReaderFactory readerFactory = mSession.getResponseReaderFactory();
if (readerFactory != null) {
O2GOffersTableResponseReader reader = readerFactory.createOffersTableReader(response);
for (int i = 0; i < reader.size(); i++) {
O2GOfferRow offer = reader.getRow(i);
System.out.println(" This is a response to your request: \nInstrument = " +
offer.getInstrument() +
" Bid = " + offer.getBid() +
" Ask = " + offer.getAsk());
}
}
}
}
// Implementation of IO2GResponseListener interface public method onRequestFailed
public void onRequestFailed(String requestID, String error) {
System.out.println("Request failed. requestID= " + requestID + "; error= " + error);
}
// Implementation of IO2GResponseListener interface public method onTablesUpdates
public void onTablesUpdates(O2GResponse response) {
O2GResponseReaderFactory factory = mSession.getResponseReaderFactory();
if (factory == null) {
return;
}
O2GTablesUpdatesReader updatesReader = factory.createTablesUpdatesReader(response);
for (int i = 0; i < updatesReader.size(); i++) {
O2GTableType tableType = updatesReader.getUpdateTable(i);
if (tableType == O2GTableType.OFFERS) {
O2GOfferRow offer = updatesReader.getOfferRow(i);
System.out.println(" This is a live update: \nInstrument = " + offer.getInstrument() +
" Bid = " + offer.getBid() +
" Ask = " + offer.getAsk());
}
}
}
}
The namespace is com.fxcore2
.
Public Methods |
onRequestCompleted
|
Processes a notification about a request successful completion.
|
onRequestFailed
|
Processes a notification about a request failure.
|
onTablesUpdates
|
Processes notifications about tables updates.
|
back