JUGAR WS REALIZAR DOS (o más) Solicitudes de GZIP Concurrentemente a veces fallan
-
21-12-2019 - |
Pregunta
myRequest:
WS.url(url)
.withHeaders("Accept-Encoding" -> "gzip")
.withQueryString("xxx","xxx")
Código utilizado para descomprimir el cuerpo
def call[T](api: WeiboApi[T])(implicit mf: Manifest[T]) = {
val param = parameters(api)
(api match {
case _: Get[T] => get(api.url, param)
case _: Post[T] => post(api.url, param)
}) map {
resp =>
try {
val decompressedBody = decompressIfGzip(resp)
api.parse(decompressedBody)
} catch {
case e: WeiboApiError => throw e
case e: Exception =>
throw new Exception("cannot parse body api " + api, e)
}
}
}
private def decompressIfGzip(resp: Response) = {
val ahcResp = resp.getAHCResponse
ahcResp.getHeader("Content-Encoding") match {
case "gzip" | "GZIP" =>
val in = ahcResp.getResponseBodyAsStream
val gzipStream = new GZIPInputStream(in)
try {
val source = scala.io.Source.fromInputStream(gzipStream)
source.mkString
} finally {
in.close()
}
case _ =>
ahcResp.getResponseBody
}
}
Luego, envío dos solicitudes al mismo tiempo.
A veces está bien
pero a veces se repone
[error] Corrupt GZIP trailer
[error] sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
[error] sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
[error] sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
[error] lib.weibo.Weibo$.lib$weibo$Weibo$$decompressIfGzip(Weibo.scala:138)
[error] lib.weibo.Weibo$$anonfun$call$1.apply(Weibo.scala:47)
[error] lib.weibo.Weibo$$anonfun$call$1.apply(Weibo.scala:42)
[error]
akka.dispatch.batchingExecutor $ lote $$ Anonfun $ RUN $ 1.ProcessBatch $ 1 (BatchingExecutor.Scala: 67) [ERROR] Akka.dispatch.batchingExecutor $ lote $$ Anonfun $ RUN $ 1.Aply $ MCV $ SP (BatchingExecutor.Scala: 82) [ERROR] Akka.dispatch.batchingExecutor $ lote $$ Anonfun $ Ejecutar $ 1.Aply (BATCHINGEXECUTOR.SCALA: 59) [ERROR] Akka.dispatch.batchingExecutor $ lote $$ Anonfun $ Ejecutar $ 1.Aply (BATCHINGEXECUTOR.SCALA: 59) [error] akka.dispatch.batchingExecutor $ BATCH.RUN (BATCHINGEXECUTOR.SCALA: 58) [error] akka.dispatch.taskinvocation.run (abstractdispatcher.scala: 42) [error] akka.dispatch.forkjoinexecutorconfigurator $ akkaforkjointask.exec (abstractdispatcher.scala: 386)
¿Qué pasa?
Solución
Resulta que los datos de respuesta a veces están dañados.
Otros consejos
No estoy seguro, pero es posible que deba envolver ese código con un WrotStreamReader:
https://stackoverflow.com/questions/3627401/gzipinputstream-to-string/3627441# 3627441