99import com .microsoft .azure .functions .HttpStatus ;
1010import com .microsoft .durabletask .DurableTaskClient ;
1111import com .microsoft .durabletask .DurableTaskGrpcClientBuilder ;
12+ import com .microsoft .durabletask .OrchestrationMetadata ;
13+ import com .microsoft .durabletask .OrchestrationRuntimeStatus ;
1214
1315import java .io .UnsupportedEncodingException ;
1416import java .net .MalformedURLException ;
1517import java .net .URL ;
1618import java .net .URLEncoder ;
1719import java .nio .charset .StandardCharsets ;
20+ import java .time .Duration ;
21+ import java .util .concurrent .TimeoutException ;
1822
1923/**
2024 * The binding value type for the {@literal @}DurableClientInput parameter.
@@ -24,6 +28,7 @@ public class DurableClientContext {
2428 private String rpcBaseUrl ;
2529 private String taskHubName ;
2630 private String requiredQueryStringParameters ;
31+ private DurableTaskClient client ;
2732
2833 /**
2934 * Gets the name of the client binding's task hub.
@@ -51,9 +56,55 @@ public DurableTaskClient getClient() {
5156 throw new IllegalStateException ("The client context RPC base URL was invalid!" , ex );
5257 }
5358
54- return new DurableTaskGrpcClientBuilder ().port (rpcURL .getPort ()).build ();
59+ this .client = new DurableTaskGrpcClientBuilder ().port (rpcURL .getPort ()).build ();
60+ return this .client ;
5561 }
5662
63+ /**
64+ * Creates an HTTP response which either contains a payload of management URLs for a non-completed instance
65+ * or contains the payload containing the output of the completed orchestration.
66+ * <p>
67+ * If the orchestration instance completes within the specified timeout, then the HTTP response payload will
68+ * contains the output of the orchestration instance formatted as JSON. However, if the orchestration does not
69+ * complete within the specified timeout, then the HTTP response will be identical to that of the
70+ * {@link #createCheckStatusResponse(HttpRequestMessage, String)} API.
71+ * </p>
72+ * @param request the HTTP request that triggered the current function
73+ * @param instanceId the unique ID of the instance to check
74+ * @param timeout total allowed timeout for output from the durable function
75+ * @return an HTTP response which may include a 202 and location header or a 200 with the durable function output in the response body
76+ */
77+ public HttpResponseMessage waitForCompletionOrCreateCheckStatusResponse (
78+ HttpRequestMessage <?> request ,
79+ String instanceId ,
80+ Duration timeout ) {
81+ if (this .client == null ) {
82+ this .client = getClient ();
83+ }
84+ OrchestrationMetadata orchestration ;
85+ try {
86+ orchestration = this .client .waitForInstanceCompletion (instanceId , timeout , true );
87+ return request .createResponseBuilder (HttpStatus .ACCEPTED )
88+ .header ("Content-Type" , "application/json" )
89+ .body (orchestration .getSerializedOutput ())
90+ .build ();
91+ } catch (TimeoutException e ) {
92+ return createCheckStatusResponse (request , instanceId );
93+ }
94+ }
95+
96+ /**
97+ * Creates an HTTP response that is useful for checking the status of the specified instance.
98+ * <p>
99+ * The payload of the returned
100+ * @see <a href="https://learn.microsoft.com/java/api/com.microsoft.azure.functions.httpresponsemessage">HttpResponseMessage</a>
101+ * contains HTTP API URLs that can be used to query the status of the orchestration, raise events to the orchestration, or
102+ * terminate the orchestration.
103+ * </p>
104+ * @param request the HTTP request that triggered the current orchestration instance
105+ * @param instanceId the ID of the orchestration instance to check
106+ * @return an HTTP 202 response with a Location header and a payload containing instance control URLs
107+ */
57108 public HttpResponseMessage createCheckStatusResponse (HttpRequestMessage <?> request , String instanceId ) {
58109 // TODO: To better support scenarios involving proxies or application gateways, this
59110 // code should take the X-Forwarded-Host, X-Forwarded-Proto, and Forwarded HTTP
0 commit comments