66
77import com .alibaba .dashscope .base .HalfDuplexParamBase ;
88import com .alibaba .dashscope .common .DashScopeResult ;
9+ import com .alibaba .dashscope .common .Status ;
910import com .alibaba .dashscope .common .TaskStatus ;
1011import com .alibaba .dashscope .exception .ApiException ;
1112import com .alibaba .dashscope .exception .NoApiKeyException ;
@@ -76,12 +77,17 @@ public DashScopeResult asyncCall(ParamT param, ServiceOption serviceOption)
7677 * @param apiKey The api-key.
7778 * @param baseUrl The base http url.
7879 * @param customHeaders The custom headers.
80+ * @param timeoutSeconds The maximum time to wait in seconds.
7981 * @return The task result.
8082 * @throws NoApiKeyException Can not find api key
81- * @throws ApiException The request failed, possibly due to a network or data error.
83+ * @throws ApiException The request failed, possibly due to a network or data error, or timeout .
8284 */
8385 public DashScopeResult wait (
84- String taskId , String apiKey , String baseUrl , Map <String , String > customHeaders )
86+ String taskId ,
87+ String apiKey ,
88+ String baseUrl ,
89+ Map <String , String > customHeaders ,
90+ long timeoutSeconds )
8591 throws ApiException , NoApiKeyException {
8692 AsyncTaskOption serviceOption =
8793 AsyncTaskOption .builder ()
@@ -98,7 +104,23 @@ public DashScopeResult wait(
98104 int maxWaitMilliseconds = 5 * 1000 ;
99105 int incrementSteps = 3 ;
100106 int step = 0 ;
107+ long startTime = System .currentTimeMillis ();
108+ long timeoutMillis = timeoutSeconds > 0 ? timeoutSeconds * 1000L : -1L ;
101109 while (true ) {
110+ if (timeoutMillis > 0 ) {
111+ long elapsed = System .currentTimeMillis () - startTime ;
112+ if (elapsed >= timeoutMillis ) {
113+ throw new ApiException (
114+ Status .builder ()
115+ .statusCode (HttpURLConnection .HTTP_CLIENT_TIMEOUT )
116+ .code ("TaskWaitTimeout" )
117+ .message (
118+ StringUtils .format (
119+ "Waiting for task [%s] timed out after %d ms (timeoutSeconds=%d)." ,
120+ taskId , elapsed , timeoutSeconds ))
121+ .build ());
122+ }
123+ }
102124 try {
103125 DashScopeResult taskResult = client .send (req );
104126 JsonObject output = (JsonObject ) taskResult .getOutput ();
@@ -121,9 +143,20 @@ public DashScopeResult wait(
121143 ? maxWaitMilliseconds
122144 : waitMilliseconds * 2 ;
123145 }
146+ long sleepMs = waitMilliseconds ;
147+ if (timeoutMillis > 0 ) {
148+ long remaining = timeoutMillis - (System .currentTimeMillis () - startTime );
149+ if (remaining <= 0 ) {
150+ continue ;
151+ }
152+ if (remaining < sleepMs ) {
153+ sleepMs = remaining ;
154+ }
155+ }
124156 try {
125- Thread .sleep (waitMilliseconds );
126- } catch (InterruptedException ignored ) {
157+ Thread .sleep (sleepMs );
158+ } catch (InterruptedException e ) {
159+ Thread .currentThread ().interrupt ();
127160 }
128161 }
129162 } catch (ApiException e ) {
@@ -135,6 +168,23 @@ public DashScopeResult wait(
135168 }
136169 }
137170
171+ /**
172+ * Wait for async task completed and return task result.
173+ *
174+ * @param taskId The async task id.
175+ * @param apiKey The api-key.
176+ * @param baseUrl The base http url.
177+ * @param customHeaders The custom headers.
178+ * @return The task result.
179+ * @throws NoApiKeyException Can not find api key
180+ * @throws ApiException The request failed, possibly due to a network or data error.
181+ */
182+ public DashScopeResult wait (
183+ String taskId , String apiKey , String baseUrl , Map <String , String > customHeaders )
184+ throws ApiException , NoApiKeyException {
185+ return wait (taskId , apiKey , baseUrl , customHeaders , -1 );
186+ }
187+
138188 /**
139189 * Wait for async task completed and return task result.
140190 *
0 commit comments