Multiple Asynchronous Method Calls

Continuing with the earlier example, we now show you how to call the same method as a parallel task and call the same method for another instance of the Person class.

Although you can call different methods for the same object, for this example we are simply outlining one scenario of how parallel method calls can be handled.

asynchronousCall(aPerson1, aPerson2: Person; ts: TimeStamp input);
vars
    mtdCtx1, mtdCtx2, mtdCtxX : JadeMethodContext;
    result1, result2 : String;
begin
    ...
    create mtdCtx1 transient;
    create mtdCtx2 transient;
    mtdCtx1.workerAppName := "WorkerApplication";
    mtdCtx2.workerAppName := "WorkerApplication";
    mtdCtx1.invoke(aPerson1, getAddress, ts);
    mtdCtx2.invoke(aPerson2, getAddress, ts);
    while true do
        mtdCtxX := process.waitForMethods(mtdCtx1, mtdCtx2);
        if mtdCtxX = null then
            break; // all requests complete
        endif;
        if mtdCtxX.getErrorNumber() = 0 then
            if mtdCtxX = mtdCtx1 then
                result1 := mtdCtxX.getReturnValue().String;
            endif;
            if mtdCtxX = mtdCtx2 then
                result2 := mtdCtxX.getReturnValue().String;
            endif;
        else
           write "Error: " & mtdCtxX.getErrorNumber().String;
        endif;
    endwhile;
    ...
epilog
    delete mtdCtx1;
    delete mtdCtx2;
end;

As in the first code example earlier in this document, a number of conditions must be met before the above example completes successfully. The aPerson1 and aPerson2 instances of the Person class must be persistent or sharedTransient and there must be a minimum of one application with the name WorkerApplication running on the current node.

If only one application with the name WorkerApplication is running, there will be no parallel processing of requests observed, because the single worker process would handle the first asynchronous method call and after finishing it, the process would then handle the second request. In other words, at least two worker processes must be run concurrently to achieve parallel execution of the intended asynchronous method calls.

The above example is similar to the first code example, as we have to prepare all of the objects in a similar manner to the first example. However, this example shows how results can be retrieved from each asynchronous method call. An important point to note is that the order in which the results are received may not be the same as the order in which the calls were invoked if you are running multiple server processes (that is, the result order is undefined). One consequence of this is that you have to check which JadeMethodContext object is received after calling the invoke method.

When you know which object has been received, you can assign the results for the different asynchronous method calls. If all requests are handled and the invoke method is invoked again, the current process no longer gets blocked and the method immediately returns null, which confirms that all calls have been processed.