Scala:在Map值中重复执行方法

时间:2017-11-23 14:40:27

标签: java scala dictionary lazy-evaluation apache-httpclient-4.x

我已经使用来自Apache HttpComponents的HttpClient v4.5.3实现了一个执行HTTP请求的Scala函数。想法是解析响应并检查它是否正常(即返回代码200)。如果不是,请重试n次。

当响应正常时,一切都按预期工作。 我可以从记录器输出中看到,如果首先出现HTTP错误,重试也会按预期执行。但是,当它失败了n次时,它似乎又重新启动了整个过程。

这是执行请求的函数:

import org.apache.http.client.methods.{CloseableHttpResponse, HttpPost, HttpUriRequest}
[...]

class SearchQuery {
  [...]
  def executeRequest(retries: Int = 2, delay: Int = 1): Option[SearchResponse] = {
      val request: HttpUriRequest = makeRequest
      val response: CloseableHttpResponse = config.httpClient.execute(request)
      val searchResults: Option[SearchResponse] = parseResponse(response)
      response.close()
      if (searchResults.isEmpty && retries > 0) {
        logger.warn(s"Failed to retrieve response from ${source}. " +
          s"Retrying $retries more time(s) in $delay second(s)...")
        Thread.sleep(delay * 1000)
        executeRequest(retries - 1, delay)
      }
      else searchResults
  }
  [...]
}

方法makeRequest()生成HTTP请求。 config.httpClient提供可重复使用的HttpClient实例。

除递归调用外,调用executeRequests()如下所示。这个想法是让一个或多个服务器(源)由调用者定义,并行执行调用;它们以enum类型Source定义。 queryFromfile()读取给定文件的内容以生成SearchQuery对象,然后从中调用executeRequest

val sources: Set[Source] = Set(Source.1)
val file: File = ...
val responses: parallel.ParMap[Source, Option[SearchResponse]] = sources
  .par
  .map(source => (source, SearchQuery.queryFromFile(file, source)))
  .toMap
  .mapValues(query => query.executeRequest())

在此示例中,executeRequest应该为每个源调用一次。一个来源通过罚款,另一个预计在这里失败。

来自日志:

14:56:48.656 [scala-execution-context-global-15] WARN  query.SearchQuery - Failed to retrieve response from source1. Retrying 2 more time(s) in 3 second(s)...
14:57:07.136 [scala-execution-context-global-15] WARN  query.SearchQuery - Failed to retrieve response from source1. Retrying 1 more time(s) in 3 second(s)...
14:57:25.538 [ScalaTest-run-running-FileProcessorTest] ERROR process.FileProcessor - Failed to retrieve results for file 'XXX' from sources: source1. Continuing.
14:57:40.933 [scala-execution-context-global-15] WARN  query.SearchQuery - Failed to retrieve response from source1. Retrying 2 more time(s) in 3 second(s)...
14:57:59.214 [scala-execution-context-global-15] WARN  query.SearchQuery - Failed to retrieve response from source1. Retrying 1 more time(s) in 3 second(s)...

使用IntelliJ调试器,逻辑也看起来与预期一样,在n次尝试后最终在行else searchResults中结束。但是,执行者不是实际返回结果,而是跳回if子句,进入递归调用executeRequest(retries - 1, delay)

更新 我发现这种行为是由responses val:

上的后续操作引起的
val emptyResponses = responses.filter(_._2.isEmpty)
if (emptyResponses.nonEmpty)
  logger.error(
    s"Failed to retrieve results for '$fileName' from sources: " +
      s"${emptyResponses.keys.mkString(",")}. Continuing."
  )

在这里,我检查是否有任何调用失败,如果有,则记录错误。我不明白为什么这会再次触发executeRequest()来电。为什么呢?

此外,以下行不会导致对executeRequest()的第二次调用,即使它在概念上看起来非常相似:

responses.values
  .filter(response => response.isDefined)
  .map(response => response.get)

1 个答案:

答案 0 :(得分:0)

访问值时再次执行executeResponse()的原因是每次访问时都会重新计算Scala Map的值。 responses因此需要建模为元组(source, Option[SearchResponse]

val responses = sources
  .par
  .map(source => (source, SearchQuery.queryFromFile(file, source)))
  .map(pair => (pair._1, pair._2.executeRequest()))

这基本上消除了mapValues的简洁,但导致元组中第二个元素的固定值。