Subscribing

The SDK provides several options for subscribing to data in a project.

Subscribing to data using the SDK involves:
  1. Create a Subscriber object. Create the object directly or retrieve it from a Project object.
  2. Connect the Subscriber object.
  3. Subscribe to the desired streams.
  4. In direct access mode, retrieve events using the Subscriber.getNextEvent() object. In callback and select access modes, the SDK generates events and passes them back to user code.
  5. For data events, retrieve MessageReader. This encapsulates a single message from the ESP project. It may consist of a single data row, or a transaction or envelope block with multiple data rows.
  6. Retrieve one or more RowReaders. Use the methods in RowReader to read in individual fields.

This example demonstrates subscribing to a stream in direct access mode with default options.

        p.connect(60000);

        subscriber = p.createSubscriber();

        String strName = "WIN1";

        

        subscriber.subscribeStream("WIN1");

        subscriber.connect();



        // Various data type we will be reading        

        BigDatetime bigdatetime = null;

        Money m = null;

        byte[] binary = null;



        // Logic to exit loop goes here

        while (true) {

            SubscriberEvent event = subscriber.getNextEvent();

            switch (event.getType()) {

            case SYNC_START:

                break;

            case SYNC_END:

                break;

                

            // There is data to read

            case DATA:

                while ( reader.hasNextRow() ) {

                    RowReader rr = reader.nextRowReader();

                    for (int j = 0; j < rr.getSchema().getColumnCount(); ++j) {

                        if ( rr.isNull(j))

                            continue;

                            

                        // This is legal but it is better to cache the data types array

                        

                        switch ( rr.getSchema().getColumnTypes()[j]) {

                        case INTEGER:

                            rr.getInteger(j);

                            break;

                        case LONG:

                            rr.getLong(j);

                            break;

                        case STRING:

                            rr.getString(j);

                            break;

                        case TIMESTAMP:

                            rr.getTimestamp(j));

                            break;

                        case MONEY01:

                            m = rr.getMoney(j);

                            break;

                            

                        // ...

                        // process other data types

                        // ...

                        

                        }

                    }

                }

                break;

            }



        }

        subscriber.disconnect();