wait for the last async response

davidvilla

Member
Hi,

I have a async web service call and a internal procedure to process the response.
Code:
DO i = 1 TO 500:   
RUN <WSmethod> IN <WebService> ASYNCHRONOUS SET hProc 
    EVENT-PROCEDURE "pExportResponse" (INPUT Ip1, OUTPUT Op1) NO-ERROR.
PROCESS EVENTS.
END.

WAIT-FOR PROCEDURE-COMPLETE OF hProc. 

<Delete and disconnect block >

PROCEDURE pExportResponse:
    DEFINE INPUT PARAMETER Op1 AS LONGCHAR NO-UNDO.
    IF SELF = hproc THEN
    DO:
        EXPORT STREAM mystream "response-->" .
        EXPORT STREAM mystream Op1.
    END.          
END.

In the above sample code, the async call issues requests and the event proc pExportResponse exports the response. But once all 500 requests are issued, the program terminates. I need to wait for all the 500 responses even though there are no more requests to be issued.
The WAIT-FOR statement above didn't help. If I put the WAIT-FOR within the i = 1 to 500 loop after each PROCESS EVENTS stmt, then the each request is waiting for the previous response to be processed. So that is not async. I may be messing up with the WAIT-FOR.

Kindly advise.
 

RealHeavyDude

Well-Known Member
I don't think you code works how you suppose it should.

Your loop must not be left until you know that you have received a response for each request. Your DO loop is left automatically when the 500th iteration is processed - you can not base when the loop is left on the iteration number. After PROCESS EVENTS you need to check a condition whether all request have been processed. You could keep track of the state of your request in a Temp-Table and leave the loop as soon as you can't find a record anymore which state does not qualify to leave the loop.

Heavy Regards, RealHeavyDude.
 

RealHeavyDude

Well-Known Member
I am afraid - no. If you invoke several web service calls ( or procedure calls on an AppServer ) asynchronously almost at the same time you must keep track of them yourself. Since the ABL is not multi-threaded it obviously lacks such features. Nevertheless you should be able to roll your own - although I must admit that I have never implemented something like that. Basically the asynchronous call feature was implemented to not block user input during a request that takes a long time to complete ( for example a report generation on the AppSever ) not really to work around the missing threading functionality.

Heavy Regards, RealHeavyDude.
 

durkadurka

New Member
Why not queue the waits in a temp-table?

Code:
DEFINE TEMP-TABLE ttWaitList NO-UNDO
  FIELD hWait AS HANDLE.


DO i = 1 TO 500:   
    RUN <WSmethod> IN <WebService> ASYNCHRONOUS SET hProc 
        EVENT-PROCEDURE "pExportResponse" (INPUT Ip1, OUTPUT Op1) NO-ERROR.
    CREATE ttWaitList.
    ASSIGN ttWaitList.hWait = hProc.
END.


FOR EACH ttWaitList:
  WAIT-FOR PROCEDURE-COMPLETE OF ttWaitList.hWait.
END.


<Delete and disconnect block >


PROCEDURE pExportResponse:
    DEFINE INPUT PARAMETER Op1 AS LONGCHAR NO-UNDO.
    EXPORT STREAM mystream "response-->" .
    EXPORT STREAM mystream Op1.        
END.
 
Top