How to get offers

Brief

The article discusses how to get offers in Java.

Details

There are two ways to get offers in your Java application.
The first way is to use table listener. In this case you should:

  1. Create a session, a session status listener, subscribe the listener to the session status (see this article).

  2. Implement the IO2GTableListener interface in a table listener class:

    public class TableListener implements IO2GTableListener {...}
  3. Allow the use of table manager (it should be done before login):

    mSession.useTableManager(O2GTableManagerMode.YES);


  4. Log in (see this article)

  5. Get O2GTableManager:

    O2GTableManager tableManager = mSession.getTableManager();
  6. Get "Offers" table:

    O2GOffersTable offersTable = (O2GOffersTable)tableManager.getTable(O2GTableType.OFFERS);
  7. The table can be processed to get information about offers:

    
    O2GTableIterator iterator = new O2GTableIterator();
    O2GOfferTableRow offerTableRow = offersTable.getNextRow(iterator);
    while (offerTableRow!=null)
    {
        System.out.println("Instrument = " + offerTableRow.getInstrument() + "; Bid = " + offerTableRow.getBid() + "; Ask = " + offerTableRow.getAsk());
        offerTableRow = offersTable.getNextRow(iterator);
    }
    
  8. To capture updates of "Offers" table create table listener and subscribe it to "Update" event of "Offers" table:

    
    TableListener tableListener = new TableListener();
    offersTable.subscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
    
  9. Process table updates in "onChanged" event of IO2GTableListener

    
    // Implementation of IO2GTableListener interface public method onChanged
    public void onChanged(String rowID, O2GRow rowData) {
        O2GOfferTableRow offerTableRow = (O2GOfferTableRow)(rowData);
        if (offerTableRow!=null)
            System.out.println("Instrument = " + offerTableRow.getInstrument() + "; Bid = " + offerTableRow.getBid() + "; Ask = " + offerTableRow.getAsk());
    }
    
  10. Unsubscribe from table listener when you are done:

    offersTable.unsubscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
  11. Log out, unsubscribe from the session status listener, and dispose the session:

    
    mSession.logout();
    mSession.unsubscribeSessionStatus(statusListener);
    mSession.dispose();
    

Get offers with table manager example: [show]

The second way to get offers is to use response listener. In that case you should:

  1. Create a session, a session status listener, subscribe the listener to the session status (see this article).

  2. Implement the IO2GResponseListener interface in a response listener class:

    public class ResponseListener implements IO2GResponseListener {...}
  3. Subscribe an object of this class to the session response:

    
    ResponseListener responseListener = new ResponseListener(...)
    session.subscribeResponse(responseListener);
    
  4. Log in (see this article).

  5. Use the listener onSessionStatusChanged function to capture the CONNECTED event.

  6. Get O2GLoginRules:

    O2GLoginRules rules = mSession.getLoginRules();
  7. Then you can get O2GResponse:

    O2GResponse response = loginRules.getTableRefreshResponse(O2GTableType.OFFERS);


    The response can be processed to extract necessary information.

  8. You should also capture O2GResponse in the onTablesUpdates function of a response listener class and process the response.

  9. To get information from the "Offers" table from O2GResponse:
    1. Create O2GResponseReaderFactory:

    O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();


    2. Check the readerFactory object for validity:

    if (readerFactory == null) {
    return;
    }

    3. Create O2GOffersTableResponseReader:

    O2GOffersTableResponseReader reader = readerFactory.createOffersTableReader(response);


    4. Process row by row:

    
    for (i = 0; i < reader.size(); i++)
    {
        O2GOfferRow row = reader.getRow(i);
        //information like row.OfferID, row.Instrument, row.Bid, row.Ask could be extracted now
    }
    
  10. Log out, unsubscribe from the session status listener and response listener, and dispose the session when you are done:

    
    mSession.logout();
    mSession.unsubscribeSessionStatus(statusListener);
    mSession.unsubscribeResponse(responseListener);
    mSession.dispose();
    

Get offers example: [show]

back