sách gpt4 ai đã đi

java - 如何解决play framework 2.2.1 connect timeout异常问题

In lại 作者:行者123 更新时间:2023-11-29 08:52:17 26 4
mua khóa gpt4 Nike

Tôi đang sử dụng scala hiện hữu play framework 中调用一个 web 服务。代码遵循 Producer/Consumer 模式。每次调用 WS 大约需要 2 秒。但是很多这样的调用都超过了 120 秒(这是 Play 中的默认超时)。因此它抛出一个异常:java.net.connectException 正好在 120 秒后。

câu hỏi:

  1. 为什么将所有调用的时间加起来,而不是单独处理它们,因此超时不会成为问题。

  2. 我通过解决这个问题尝试了一种增加超时的解决方案:fixed ws.timeout .但对我来说,问题仍然存在。

  3. 是线程还是并发的问题?

这是类的代码:

class WS(sentenceList: List[String], queue: BlockingQueue[Future[Response]], filename: String) {

val listofJson = new ListBuffer[(String, JsValue)]
listofJson.clear

def callWSProducer() = {

sentenceList.foreach { name =>
val data = Json.obj(
"input_sent" -> name,
"Filename" -> filename)

val holder: Future[Response] = WS.url("http://0.0.0.0:8015/endpoint/").withHeaders("Content-Type" -> "application/json").post(data)
implicit val context = scala.concurrent.ExecutionContext.Implicits.global
queue.put(holder)
}
}

def WSConsumer(): List[(String, JsValue)] = {

sentenceList.foreach { name =>

val result = Await.result(queue.take(), 100.second)

val out = (result.json \ "sentence");

listofJson += ((name, out));
}

return listofJson.toList

}

}

我进入控制台时出错:

error.txt

biên tập:

让我把问题说得更清楚一点。首先,通过创建上述类的对象从 Controller (主线程)调用上述函数。上面的 Json 列表返回给 Controller , Controller 又将其返回给 View 。因为我们必须返回列表,所以我们能想到的唯一可能的方法是使用等待(阻塞)机制。

我知道代码存在线程问题,但至少有人可以指出这些问题。我们尝试过的所有方法都会导致上面提到的 120 秒超时,或者当我们的等待 block 中存在某种死锁时导致 100 秒 future 超时,例如当我们使用类似于此处提到的解决方案时:Scala Play Resolve a list of futures

1 Câu trả lời

我很困惑为什么我们必须阻止?我想我们可以为此想出一个非阻塞的解决方案。但首先让我们解决超时问题。对于 WS,您可以使用 ws.timeout 属性配置超时。我不确定为什么那不起作用。对于在您的 application.conf 文件中设置为 0 的实验

ws.timeout=0

这实际上是将超时设置为永远。这可能会在您的代码中引发新问题。我不确定。现在让我们看看阻塞/等待的东西。这样的事情怎么样

  object Application extends Controller {

//using Scala global implicits is a bad practice
//implicit val context = scala.concurrent.ExecutionContext.Implicits.global

//use the play one or even better use a custom execution context read
//http://www.playframework.com/documentation/2.1.0/ThreadPools
implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext

def index = Action.async {
invokeServices(send your sentence list).map { listOfPairs =>
//do your transformation here to create the result
}
}
//invokeServices hopefully capture all that you are doing inside the WS class but no blocking
//and mutation.
def invokeServices(sentenceList: List[String]): Future[List[(String, JsValue)]] = {
val responses: List[Future[(String, JsValue)]] = sentenceList.map { name =>
val data = Json.obj(
"input_sent" -> name,
"Filename" -> name)

val response: Future[Response] =
WS.url("http://0.0.0.0:8015/endpoint/").withHeaders("Content-Type" -> "application/json").post(data)
response.map(result => (name, result.json \ "sentence"))
}
//converts List[Future[A]] to Future[List[A]]
Future.sequence(responses)
}

}

关于java - 如何解决play framework 2.2.1 connect timeout异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174845/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com