Using the RECEIVE command

You can use the RECEIVE command to determine if any of the asynchronous requests you issued have completed.

RECEIVE returns the output from those requests. The output returned can be:
  • A resource table record representing an event named in a previous LISTEN command
  • An ASYNCREQ resource table record representing completion of an asynchronous GET, PERFORM, REFRESH, or SET request.
Note: Before you issue the RECEIVE command, you should issue the ADDRESS command and check the SENTINEL value to determine if there are any outstanding asynchronous requests to be received. If the sentinel value is 0, there are no outstanding asynchronous requests to be received.

As an example, your program might issue a LISTEN command and a GET command with the NOWAIT operand on the same API thread. Then, in response to a RECEIVE command, you would receive either an ASYNCREQ resource table record for the GET command or a resource table record associated with the event you were listening for.

Alternatively, you can use multiple API threads to separate the output returned by subsequent RECEIVE commands. For example, you might create one thread and use it only for receiving event notifications from the LISTEN command. You might also create another thread for use by other API functions. In this way, you can control what output is returned by the RECEIVE commands issued against each thread.

Another reason you might want to create multiple API threads is because each thread can have only 256 asynchronous requests outstanding at one time. If your program issues a large number of asynchronous requests on a single API thread, you should issue the RECEIVE command at regular intervals. If a processing thread reaches its maximum of 256, asynchronous requests are discarded and are not processed.

A third reason you might want to create multiple API threads is because the size of a thread buffer pool for asynchronous requests is limited to 16K. With longer requests, a thread can reach the buffer limit with far fewer asynchronous requests than the maximum of 256.

By default, the RECEIVE command waits until asynchronous output is available before returning control to your program. This means processing is suspended until an asynchronous request completes. As an alternative to waiting indefinitely, you can specify one of these options on the RECEIVE command:
DELAY(data-value)
Checks for asynchronous output, waits the specified number of seconds for output to become available, and then returns control to the processing thread, with or without output.
IMMEDIATE
Checks for asynchronous output and then immediately returns control to the processing thread, whether or not any output is available.