我正在尝试使用akka http生成PUT请求,我的休息服务端点在使用curl完成时按预期工作,但在我通过创建Http连接生成它时显示超时错误。
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import de.heikoseeberger.akkahttpcirce.CirceSupport
import io.circe.Json
import org.joda.time.DateTime
case class JobDetails (jobName: String, appId: Option[String]=None, status: Option[String]=None,runningSince:Option[Long]=None)
trait UpdateJobStatus extends CirceSupport with Config {
def UpdateJobDetails(jobDetails :JobDetails) {
implicit val actorSystem = ActorSystem("update")
implicit val materializer: ActorMaterializer = ActorMaterializer()
val DetailsList = List(
("jobName",Json.fromString(jobDetails.jobName)),
("appId",Json.fromString(jobDetails.appId.get)),
("status",Json.fromString(jobDetails.status.get)),
("runningSince",Json.fromLong(DateTime.now.getMillis)))
val jobDetailJson:Json = Json.fromFields(DetailsList)
val endpoint =Uri.from(scheme = "http",host = httpHost,port = 9000,path = "/updateJobDetails/TestJob")
val updateRequest = HttpRequest(
HttpMethods.PUT,
uri = endpoint,
entity = HttpEntity(MediaTypes.`application/json`, jobDetailJson.toString()))
Http().singleRequest(updateRequest)
}
}
object Test extends App with UpdateJobStatus{
val test = JobDetails("TestJob",Some("1234"),Some("Killed"),Some(21))
UpdateJobDetails(test)
}
错误:
DEBUG] [05/25/2017 01:20:02.934] [main] [EventStream(akka://UpdateService)] logger log1-Logging$DefaultLogger started
[DEBUG] [05/25/2017 01:20:02.935] [main] [EventStream(akka://UpdateService)] Default Loggers started
[DEBUG] [05/25/2017 01:20:03.122] [main] [AkkaSSLConfig(akka://UpdateService)] Initializing AkkaSSLConfig extension...
[DEBUG] [05/25/2017 01:20:03.124] [main] [AkkaSSLConfig(akka://UpdateService)] buildHostnameVerifier: created hostname verifier: com.typesafe.sslconfig.ssl.DefaultHostnameVerifier@19e7a160
[DEBUG] [05/25/2017 01:20:04.098] [UpdateService-akka.actor.default-dispatcher-4] [akka://UpdateService/user/pool-master/PoolInterfaceActor-0] (Re-)starting host connection pool to 0.0.0.0:9000
[DEBUG] [05/25/2017 01:20:04.315] [UpdateService-akka.actor.default-dispatcher-4] [akka://UpdateService/system/IO-TCP/selectors/$a/0] Resolving 0.0.0.0 before connecting
[DEBUG] [05/25/2017 01:20:04.333] [UpdateService-akka.actor.default-dispatcher-5] [akka://UpdateService/system/IO-DNS] Resolution request for 0.0.0.0 from Actor[akka://UpdateService/system/IO-TCP/selectors/$a/0#-1469947037]
[DEBUG] [05/25/2017 01:20:04.341] [UpdateService-akka.actor.default-dispatcher-5] [akka://UpdateService/system/IO-TCP/selectors/$a/0] Attempting connection to [/0.0.0.0:9000]
[DEBUG] [05/25/2017 01:20:14.359] [UpdateService-akka.actor.default-dispatcher-3] [akka://UpdateService/system/IO-TCP/selectors/$a/0] Connect timeout expired, could not establish connection to [0.0.0.0:9000]
虽然
curl -v -H "Content-Type: application/json" \
-X PUT http://0.0.0.0:9000/updateJobDetails/TestJob \
-d '{"jobName":"TestJob","appId":"1234","status":"Killed","runningSince":1}'
作品
答案 0 :(得分:0)
您正在创建请求,结果为Future[HttpResponse]
。
我认为你应该直接处理Future
完成,或者通过将结果传递给一个actor,然后接收一个类型为HttpResponse
的消息。
可以找到一个示例here。
如果这仍然可以让您超时,我建议您使用网络流量监控器Wireshark深入了解。