6.3.2 Receiving query result data (in-process connection custom adaptor)

This subsection explains the basic flow for receiving the query result data of an in-process connection custom adaptor, based on an implementation example.

Either the polling method or the callback method can be used by the in-process connection adaptor for receiving query result data. Each of these methods is explained below.

Organization of this subsection
(1) Polling method
(2) Callback method

(1) Polling method

This method dynamically receives stream query result data from the SDP server.

An implementation example of receiving query result data using the polling method is described below.

Implementation example

public class Inpro_ReceiveSample1 implements StreamInprocessUP {
 // Implement StreamInprocessUP.
 public void execute(SDPConnector con) {
   try {
     // 1. Connect to the output stream.
     StreamOutput o = con.openStreamOutput("GROUP","QUERY1");

     // 2. Receive tuples.
     try {
       while(true) {
         ArrayList tupleList = o.getAll();
       }
     } catch (SDPClientEndOfStreamException e) {
       System.out.println("Data reception completed");
     }

     // 3. Disconnect from the output stream.
     o.close();

     // 4. Disconnect from the SDP server.
     con.close();
   } catch (SDPClientException e) {
     System.err.println(e.getMessage());
   }
 }
}

Explanation of the implementation details

The meaning of each process is explained below. The numbers correspond to the comment numbers in the implementation example.

  1. Uses the SDPConnector type object (con), which has been passed as a parameter of the execute method, to connect to an output stream that has "GROUP" as its query group name and "QUERY1" as its query name, and acquires the StreamOutput type object (o).

    StreamOutput o = con.openStreamOutput("GROUP","QUERY1");

  2. Uses the StreamOutput type object (o) to acquire the query result data (tuples).

    ArrayList tupleList = o.getAll();

  3. When receiving is completed, the StreamOutput type object (o) is used to disconnect from the output stream.

    o.close();

  4. Uses the SDPConnector type object (con) to disconnect from the SDP server.

    con.close();

(2) Callback method

In this method, the actual method for receiving query result data is initiated by the SDP server.

Implementation examples of receiving query result data using the callback method are described below.

To use the callback method for receiving the query result data, you need to implement the following two methods:

An implementation example of each is explained below.

Implementation example (using the execute method)

public class Inpro_ReceiveSample2 implements StreamInprocessUP {
 // Implement StreamInprocessUP.
 public void execute(SDPConnector con) {  // Process that
               // is executed when the application starts.
   // 1. Connect to the output stream.
   StreamOutput o = con.openStreamOutput("GROUP","QUERY2");

   // 2. Generate a listener object for callback.
   CallBack notifiable = new CallBack();

   // 3. Register the listener object for callback.
   o.registerForNotification(notifiable);
 }
 public void stop() {  // Process that is executed when
                       // the application stops.
   // 4. Cancel the listener object for callback.
   o.unregisterForNotification(notifiable);

   // 5. Disconnect from the SDP server.
   con.close();
 }
}

Implementation example (using the onEvent method)

public class CallBack implements StreamEventListener {
 // Implement StreamEventListener.
 public void onEvent(StreamTuple tuple) {
   // 6. Processing when tuples are received
   System.out.println("Tuple received:" + tuple);
 }
}

Explanation of the implementation details

The meaning of each process is explained below. The numbers correspond to the comment numbers in the implementation examples.

  1. Uses the SDPConnector type object (con), which is specified in the parameter of the execute method, to connect to an output stream that has "GROUP" as its query group name and "QUERY2" as its query name, and acquires the StreamOutput type object (o).

    StreamOutput o = con.openStreamOutput("GROUP","QUERY2");

  2. Generates an object (notifiable) in the implemented class (CallBack) of the StreamEventListener interface.

    CallBack notifiable = new CallBack();

  3. Uses the StreamOutput type object (o) to register the listener object for the callback created in step 2.

    o.registerForNotification(notifiable);

  4. When stopping the custom adaptor, the StreamOutput type object (o) is used to cancel the listener object for callback (notifiable).

    o.unregisterForNotification(notifiable);

  5. When stopping the custom adaptor, the SDPConnector type object (con) is used to disconnect from the SDP server.

    con.close();

  6. When the tuples of the query result arrive at the output stream, the content defined in the onEvent method of the StreamEventListener interface is executed.

    public void onEvent(StreamTuple tuple) {
    // 6. Processing when tuples are received
     }