Schedulable asynchronous methods with semi-reactive completion stages
Processing methods is provided. A method is designated as schedulable. A completion stage is generated for the method. A return of the completion stage corresponding to the method is received. It is determined whether a state of the completion stage indicates continued execution of the method. In response to determining that the state of the completion stage does indicate continued execution of the method, a set of executions of the method is continued based on an execution return of the method being a null return.
1 . A computer-implemented method for processing methods, the computer-implemented method comprising:
designating, by a computer, a method as schedulable;
generating, by the computer, a container-generated completion stage corresponding to the method, wherein the container-generated completion stage represents an entire set of scheduled executions of the method and remains incomplete across multiple scheduled executions of the method until a final termination condition occurs;
sending, by the computer, the container-generated completion stage to an invoking program context of the method;
scheduling, by the computer, a callback for an execution time of the method;
determining, by the computer, whether the container-generated completion stage has completed;
responsive to the computer determining that the container-generated completion stage has not completed, executing, by the computer, the method on a pooled thread;
receiving, by the computer, a return corresponding to an execution of the method;
determining, by the computer, whether the method returned null while the container-generated completion stage remains incomplete; and
responsive to determining that the method has returned null, continuing, by the computer, to schedule the method for a subsequent execution time.
2 . The computer-implemented method of claim 1 , wherein the method is a schedulable asynchronous method, and wherein the computer designates the method as the schedulable asynchronous method by one of adding a schedule attribute comprising a schedule to an existing annotation of the method, adding a second annotation to the method indicating the schedule, or assigning a trigger class to the method.
3 . The computer-implemented method of claim 1 further comprising:
responsive to the computer determining that a state of the container-generated completion stage does not indicate continued execution of the method, terminating, by the computer, the entire set of executions of the method based on the container-generated completion stage being completed.
4 . The computer-implemented method of claim 3 , wherein a returned result is one of a container-generated completion stage result or a completable future.
5 . The computer-implemented method of claim 1 further comprising:
receiving, by the computer, a request from the invoking program context to invoke the method asynchronously;
determining, by the computer, whether the method has a schedule in response to receiving the request; and
responsive to the computer determining that the method does have a schedule, generating, by the computer, the container-generated completion stage corresponding to the method to provide a final result of the method.
6 . The computer-implemented method of claim 1 further comprising:
sending, by the computer, the container-generated completion stage of the method to the invoking program context;
scheduling, by the computer, a callback for an execution time of the method;
detecting, by the computer, that the execution time of the method has elapsed; and
determining, by the computer, a first time whether the container-generated completion stage has completed in response to detecting that the execution time of the method has elapsed.
7 . The computer-implemented method of claim 6 further comprising:
responsive to the computer determining that the completion stage has not completed, executing, by the computer, the method on a pooled thread;
determining, by the computer, a second time whether the completion stage has completed in response to executing the method on the pooled thread; and
responsive to the computer determining that the container-generated completion stage has not completed, determining, by the computer, whether the method failed during execution on the pooled thread.
8 . The computer-implemented method of claim 7 further comprising:
responsive to the computer determining that the method has failed, completing, by the computer, the container-generated completion stage exceptionally with an indication of failure of the method;
sending, by the computer, the indication of the failure to dependent stages that have arranged to receive notification of the failure; and
stopping, by the computer, scheduling execution of the method.
9 . The computer-implemented method of claim 7 further comprising:
responsive to the computer determining that the method has not failed, determining, by the computer, whether the method returned null; and
responsive to the computer determining that the method returned null, continuing, by the computer, to schedule the method for execution.
10 . The computer-implemented method of claim 9 further comprising:
responsive to the computer determining that the method has not returned null, determining, by the computer, whether the method returned as its result the container-generated completion stage;
responsive to the computer determining that the method has not returned the container-generated completion stage as the method's result, wherein the container-generated completion stage was sent to the invoking program context, completing, by the computer, the container-generated completion stage using a result or an exceptional completion of a different container-generated completion stage generated by the method; and
sending, by the computer, the result or the exceptional completion of the different container-generated completion stage generated by the method to dependent stages that have arranged to receive notification of the result or the exceptional completion.
11 . A computer system for processing methods, the computer system comprising:
a communication fabric;
a storage device connected to the communication fabric, wherein the storage device stores program instructions; and
a processor connected to the communication fabric, wherein the processor executes the program instructions to:
designate a method as schedulable;
generate a container-generated completion stage corresponding to the method, wherein the container-generated completion stage represents an entire set of scheduled executions of the method and remains incomplete across multiple scheduled executions of the method until a final termination condition occurs;
send the container-generated completion stage to an invoking program context of the method;
schedule a callback for an execution time of the method;
determine whether the container-generated completion stage has completed; responsive to determining that the container-generated completion stage has not completed, execute the method on a pooled thread;
receive a return corresponding to an execution of the method;
determine whether the method returned null while the container-generated completion stage remains incomplete; and
responsive to determining that the method returned null, continue to schedule the method for a subsequent execution time.
12 . The computer system of claim 11 , wherein the method is a schedulable asynchronous method, and wherein the computer system designates the method as the schedulable asynchronous method by one of adding a schedule attribute comprising a schedule to an existing annotation of the method, adding a second annotation to the method indicating the schedule, or assigning a trigger class to the method.
13 . The computer system of claim 11 , wherein the processor further executes the program instructions to:
terminate a set of executions of the method based on the container-generated completion stage being completed.
14 . The computer system of claim 13 , wherein a returned result is one of a container-generated completion stage result or a completable future.
15 . The computer system of claim 11 , wherein the processor further executes the program instructions to:
receive a request from the invoking program context to invoke the method asynchronously;
determine whether the method has a schedule in response to receiving the request; and
responsive to determining that the method does have a schedule,
generate the container-generated completion stage corresponding to the method to provide a final result of the method.
16 . A computer program product for processing methods, the computer program product comprising a computer-readable storage medium having program instructions embodied therewith, the program instructions executable by a computer to cause the computer to perform a method of:
designating, by the computer, a method as schedulable;
generating, by the computer, a container-generated completion stage corresponding to the method, wherein the container-generated completion stage represents an entire set of scheduled executions of the method and remains incomplete across multiple scheduled executions of the method until a final termination condition occurs;
sending, by the computer, the container-generated completion stage to an invoking program context of the method;
scheduling, by the computer, a callback for an execution time of the method;
determining, by the computer, whether the container-generated completion stage has completed;
responsive to the computer determining that the container-generated completion stage has not completed, executing, by the computer, the method on a pooled thread;
receiving, by the computer, a return corresponding to an execution the method;
determining, by the computer, whether the method returned null while the container-generated completion stage remains incomplete; and
responsive to determining that the method has returned null, continuing, by the computer, to schedule the method for a subsequent execution time.
17 . The computer program product of claim 16 , wherein the method is a schedulable asynchronous method, and wherein the computer designates the method as the schedulable asynchronous method by one of adding a schedule attribute comprising a schedule to an existing annotation of the method, adding a second annotation to the method indicating the schedule, or assigning a trigger class to the method.
18 . The computer program product of claim 16 further comprising:
responsive to the computer determining that a state of the container-generated completion stage does not indicate continued execution of the method, terminating, by the computer, the entire set of executions of the method based on the container-generated completion stage being completed.
19 . The computer program product of claim 18 , wherein a returned result is one of a container-generated completion stage result or a completable future.
20 . The computer program product of claim 16 further comprising:
receiving, by the computer, a request from the invoking program context to invoke the method asynchronously;
determining, by the computer, whether the method has a schedule in response to receiving the request; and
responsive to the computer determining that the method does have a schedule, generating, by the computer, the container-generated completion stage corresponding to the method to provide a final result of the method.