Ask Reuben – July 2, 2025
Asynchronous Web Service
How can I not be blocked when making a Web Service call?
A typical program that makes a Web Service will have a line similar to the following
LET resp = req.getResponse()
where req is of type com.HttpRequest and resp is of type com.HttpResponse. The com.HttpRequest object has been created and populated with information needed for the Web Service call and the call has been made (typically with a doRequest() method call but there are other calls that can be used). The getResponse() method waits for the response from the Web Service and populates the com.HttpResponse object with what is returned from the Web Service.
This getResponse() method is blocking, the runtime waits until it gets a response from the Web Service.
This blocking property can be undesirable in certain scenarios. These scenarios might include …
- where it is known the web service takes a long time.
- where many independent web service calls are made in parallel. Why wait for the first to return before making the second?, why not start them all at the same time.
There is a way you can make Web Service calls asynchronously. This involves using the getASyncResponse() method instead of the getResponse() method.
It is not quite a 1-1 replacement. You have to consider what you are going to do if the Web Service has not returned anything yet and how you are going to keep checking until it does return something. The method will return NULL whilst the Web Service is still processing. When the Web Service call is finished, then it will return a NOT NULL value.
At its simplest your code might be …
#! check every one second for the Web Service to finish WHILE TRUE LET resp = rep.getAsyncResponse() IF resp IS NOT NULL THEN EXIT WHILE END IF SLEEP 1 END WHILE
… every one second check if the Web Service has finished. If it has, exit the while loop. If it has not finished, sleep for one second before trying again. A WHILE loop like this might be suitable for the many independent web service calls scenario. For the web service that takes a long time, you might use ON TIMER to make your getASyncResponse call every X seconds until it returns a NOT NULL value.
Other things you should consider
- what happens in the called web service. Does it block waiting for you to make the next getASyncResponse call?
- timeouts, do you wait for ever?
- errors, what happens if there is an error in the client or server program ?
One thing with Web Services I encourage is to experiment with a simple Add() function, see this example. If you want an interesting advanced exercise, add a SLEEP within the simple Add function so it takes some time to process, and then modify the code generated by fglrestful to use getASyncResponse(). Hint: you end up with two functions, one that returns a com.HttpResponse object and one that takes com.HttpResponse object as an input parameter.
I’d expect most Web Service calls you would make to be synchronous, but it is handy to have the asynchronous option in your toolbox.