How Do I: Poll Another Service for Information?

When you want to communicate with a web service that does not support asynchronous callbacks, you will have to poll the web service until the result is available. With polling, you call a function at regular intervals to check if the result is ready.

This is easy to accomplish using a Timer control.

To Add a Timer Control to Your Web Service for Polling

  1. In Design View, select your web service.
  2. From Controls in the Insert Menu, select Timer.
  3. In the dialog that appears, in Step 1 of the dialog, enter a name for the variable that will represent your control (for example, you might enter timer). The name you enter must be a valid Java identifier.
  4. In Step 2 of the dialog, in the timeout field, enter the amount of time until the first poll should occur (for example, 5 seconds).
  5. In Step 2 of the dialog, in the repeats-every field, enter the amount of time between polls (for example, 5 seconds).
  6. Call the timer's start method when you want to begin polling. Your code to start the timer might look something like this:
    service.startOperation();
    timer.start();
    
  7. Each time the timer goes off, it calls your handler for the onTimeout callback. In this handler, you should call the web service to see if the information is available. Your code might look something like this:
    private void timer_onTimeout(long time)
    {
        if (service.isOperationDone())
        {
            timer.stop();
            // process operation result...
        }
    }
    
  8. Be sure to call the stop method on the timer when you want to stop polling.

Related Topics

Timer Control