好吧,是的,您需要不断询问远程资源是否已完成任务。
202 是非承诺性的,这意味着 HTTP 无法稍后发送异步响应来指示处理请求的结果。
我看到您还在使用“裸机”实用程序,例如HttpURLConnection
,这让我相信您没有任何库支持重试 HTTP 调用。
在这种情况下,您可以做的是生成一个新线程,可能使用一个ExecutorService
, 和submit
/execute
一个简单循环的任务,例如
while (!Thread.interrupted()) { ... }
调用您的 URL 直到HTTP_OK
收到。
骨架可能是
executorService.execute(() -> {
while (!Thread.interrupted()) {
// Return a valid value if `HTTP_OK`, otherwise `null`
final var result = callRemoteUrl(url);
if (result != null) {
callback.call(result);
return;
}
}
});
哪里callback
是异步接收 HTTP 结果的实例。
while (true)
HttpURLConnection connection = connection("XXX.com")
if (connection.responseCode >= HTTP_SERVER_ERROR) {
// Server/internal error, we can't do anything
// Maybe throw a custom Exception.
break;
}
if (connection.responseCode != HTTP_OK) {
try {
// Adjust the delay between calls, in ms
Thread.sleep(1000);
} catch (final InterruptedException e) {
// Ignore
}
// Try again
continue;
}
println("Got http response code: ${connection.responseCode}, message: ${connection.responseMessage}")
log.info("Successful request to ${connection.getURL()}")
//println(connection.getInputStream().getText())
LazyMap json = jsonFromExtractionConnection(connection)
//Process the data from the response JSON and put it in the Map object for processing
tryToProcessMarketDataFromExtractionJson(json, ricMap)
// Filter the empty records out and take valid map from Data for inserting into DB table .
def validMap = ricMap.findAll { key, val ->
val.fs != null
}
insertIntoDb(validMap)
writeToFile(outFile, sql(ORACLE), Select.query, Arrays.asList(Data.COLUMNS))
// We're done, end the loop
break;
}