Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于异步http api请求,获取Java中的http响应代码202_Java_Api_Httprequest_Httpresponse_Dss - Fatal编程技术网

对于异步http api请求,获取Java中的http响应代码202

对于异步http api请求,获取Java中的http响应代码202,java,api,httprequest,httpresponse,dss,Java,Api,Httprequest,Httpresponse,Dss,我有以下到url的DSShttp连接: private static HttpURLConnection connection(String urlSpec) { HttpURLConnection connection = new URL(urlSpec).openConnection() as HttpURLConnection connection.setRequestProperty('Prefer', 'respond-async, wait=60')

我有以下到url的
DSS
http连接:

private static HttpURLConnection connection(String urlSpec) {
        HttpURLConnection connection = new URL(urlSpec).openConnection() as HttpURLConnection
        connection.setRequestProperty('Prefer', 'respond-async, wait=60')
        connection.setRequestProperty('Accept', 'application/json')
 
        connection.setRequestMethod("POST")
        connection.setRequestProperty("Content-Type", "application/json; utf-8")
        connection.setDoOutput(true)
        connection
    }
下面是我检查http响应的代码部分,如果响应是http
200
,即http\u OK,那么我可以获取数据并插入到数据库表中。 但现在的问题是,在处理过程中,我现在处于
获取http错误代码为202
之间,这是
http\u接受的
,因此我无法将此数据处理到数据库表中


我认为在请求异步时应该使用
http202
。这意味着服务器已经收到您的查询并正在处理它。我们需要通过重试
202
响应中发送的新
URL
,不断检查请求的状态,直到您获得
http200
。但是我不知道如何才能做到这一点?

是的,您需要不断询问远程资源任务是否已完成

202是非提交的,这意味着HTTP以后无法发送指示处理请求结果的异步响应

我看到您也在使用“裸机”实用程序,例如
HttpURLConnection
,这让我相信您没有任何库支持重试HTTP调用

在这种情况下,您可以创建一个新线程,可能使用
ExecutorService
,以及
submit
/
执行一个简单循环的任务,例如

while (!Thread.interrupted()) { ... }
调用您的URL,直到收到
HTTP\u 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结果的实例



我在处理http请求、响应和rest api方面是个新手,也没有使用线程…你能帮助我在哪里实际使用这个while循环以及如何使用吗?我不需要像处理http:200那样单独处理http:202吗?它只对线程有效吗?如果我增加等待时间'Prefer','response async,wait=60',@Symonds你的问题相当广泛,但您可以检查我所做的编辑作为起点。@Symonds响应异步只是一个影响服务器行为的头。@Symonds我建议生成一个新线程,以避免在应用程序继续调用URL时阻塞整个应用程序。
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;
}