LibraryLink ToToggle FramesPrintFeedback

Implementing an Asynchronous Client with the Polling Approach

The polling approach is the more straightforward of the two approaches to developing an asynchronous application. The client invokes the asynchronous method called OperationNameAsync() and is returned a Response<T> object that it polls for a response. What the client does while it is waiting for a response is depends on the requirements of the application. There are two basic patterns for handling the polling:

  • Non-blocking polling — You periodically check to see if the result is ready by calling the non-blocking Response<T>.isDone() method. If the result is ready, the client processes it. If it not, the client continues doing other things.

  • Blocking polling — You call Response<T>.get() right away, and block until the response arrives (optionally specifying a timeout).

Example 18.5 illustrates using non-blocking polling to make an asynchronous invocation on the greetMeSometime operation defined in Example 18.1. The client invokes the asynchronous operation and periodically checks to see if the result is returned.


The code in Example 18.5 does the following:

1

Invokes the greetMeSometimeAsync() on the proxy.

The method call returns the Response<GreetMeSometimeResponse> object to the client immediately. The FUSE Services Framework runtime handles the details of receiving the reply from the remote endpoint and populating the Response<GreetMeSometimeResponse> object.

[Note]Note

The runtime transmits the request to the remote endpoint's greetMeSometime() method and handles the details of the asynchronous nature of the call transparently. The endpoint, and therefore the service implementation, never worries about the details of how the client intends to wait for a response.

2

Checks to see if a response has arrived by checking the isDone() of the returned Response object.

If the response has not arrived, the client continues working before checking again.

3

When the response arrives, the client retrieves it from the Response object using the get() method.

When using the block polling pattern, the Response object's isDone() is never called. Instead, the Response object's get() method is called immediately after invoking the remote operation. The get() blocks until the response is available.

[Tip]Tip

You can also pass a timeout limit to the get() method.

Example 18.6 shows a client that uses blocking polling.