如果问题听起来很幼稚,请原谅我,因为我在Akka中很新。
我试图在REST API中使用Scala Akka中的Mallet API但是获取错误请求超时
下面是我的服务器的快照
Configuration.parser.parse(args,Configuration.ConfigurationOptions()) match {
case Some(config) =>
val serverBinding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, config.interface, config.port)
serverBinding.onComplete {
case Success(bound) =>
println(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/")
case Failure(e) =>
log.error("Server could not start: ", e)
system.terminate()
}
case None =>
system.terminate()
}
Await.result(system.whenTerminated, Duration.Inf)
路由器的快照
lazy val apiRoutes: Route =
ignoreTrailingSlash {
pathSingleSlash {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
} ~
path("health") {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
} ~
pathPrefix("mallet") {
parameter('malletFile) { malletFile =>
{val modelFile: Future[MalletModel] =
(malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
complete(modelFile)
}
}
}
}
,最后是malletactor的快照
class MalletActor(implicit val uaCache: Cache[String, MalletModel],
implicit val executionContext: ExecutionContext)
extends Actor with ActorLogging with JsonSupport {
import MalletActor._
def receive: Receive = {
case GetMalletOutput(malletFile) => sender() ! createMalletResult2(malletFile)
}
def createMalletResult2(malletFile: String): MalletModel = {
logger.debug("run count...")
val res = MalletResult(malletFile)
val converted = res.Score.parseJson.convertTo[MalletRepo]
val fileName = converted.ContentId
val fileTemp = new File("src/main/resources/new_corpus/" + fileName)
if (fileTemp.exists) {
fileTemp.delete()
}
val output = new BufferedWriter(new FileWriter("src/main/resources/new_corpus/" + fileName))
output.write(converted.ContentText)
output.close()
//runMalletInferring()
val tmpDir = "src/main/resources/"
logger.debug("Import all documents to mallet...")
Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
logger.debug("Run training process...")
InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" "))
logger.debug("Inferring process finished.")
我是在调用text2vector.main时获得的错误,新的矢量化文件正在new_corpus Directory中创建,而New_corpus也正在生成。但是之后,我得到以下错误
Server online at http://127.0.0.1:9000/
12:45:38.622 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - run count...
12:45:38.634 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - Import all documents to mallet...
Couldn't open cc.mallet.util.MalletLogger resources/logging.properties file.
Perhaps the 'resources' directories weren't copied into the 'class' directory.
Continuing.
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO: Labels =
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO: src/main/resources/new_corpus/
May 26, 2019 12:45:46 PM cc.mallet.classify.tui.Text2Vectors main
INFO: rewriting previous instance list, with ID = 4e0a5a65540221c3:d508579:14b2ca15a26:-7ff7
[INFO] [05/26/2019 12:45:58.476] [Sophi-Mallet-Api-akka.actor.default-dispatcher-4] [akka.actor.ActorSystemImpl(Sophi-Mallet-Api)] Request timeout encountered for request [GET /mallet Empty]
在Web浏览器中,我也会收到错误
The server was not able to produce a timely response to your request.
Please try again in a short while!
我自己找出解决方案。如果仍然模糊,请纠正我。
需要几个变化。首先在应用程序配置文件(application.conf(
中需要更新代码。
server{
idle-timeout = infinite
request-timeout = infinite
}
settings {
akka-workers-count = 100
akka-workers-count = ${?AKKA_WORKERS_COUNT}
actor-timeout = 100
actor-timeout = ${?ACTOR_TIMEOUT}
}
,需要在服务器代码中处理问题通过将API调用在超时响应中
val timeoutResponse = HttpResponse(
StatusCodes.EnhanceYourCalm,
entity = "Running Mallet modelling.")
lazy val apiRoutes: Route =
ignoreTrailingSlash {
pathSingleSlash {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
} ~
path("health") {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
} ~
pathPrefix("mallet") {
withRequestTimeout(5.minute, request => timeoutResponse) {
parameter('malletFile) { malletFile => {
val modelFile: Future[MalletModel] =
(malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
complete(modelFile)
}
}
}
}
}