我想使用一个演员的喷涂客户端发出一个Web请求,这个Web请求需要一个参数,该参数必须是接收消息的一部分。为了做到这一点,我创建了一个案例类,如:
case class MessageWithParam(param: String)
演员看起来像这样:
import akka.actor.{Actor, ActorLogging}
import akka.util.Timeout
import spray.client.pipelining._
import spray.http.{FormData, HttpRequest, HttpResponse}
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.{Failure, Success}
import scala.xml.XML
import org.example.MessageWithParam
class WebCall extends Actor with ActorLogging {
import context.dispatcher
implicit val timeout: Timeout = Duration(5, SECONDS) // TODO Eliminate magic number
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
def receive = {
case MessageWithParam(param) => {
val sendingActor = sender()
val data = Seq("Param" -> param)
val request = Post("http://my.server.com/request",FormData(data))
pipeline(request) onComplete {
case Success(httpResponse: HttpResponse) => {
...
}
case Failure(error) => {
...
}
}
}
}
}
我的问题是,每当消息是一个字符串时,例如:
def receive = {
case "Message" => {
...
}
服务电话已被取消。但是,如果我使用案例类以便能够参数化消息,那么Web调用就会超时(但我用wireshark检查过),甚至没有建立与服务器的连接。
调用消息的代码:
import akka.actor.{ActorSystem, Props}
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit}
import org.example.WebCall
import org.example.MessgeWithParam
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.concurrent.duration._
class WebCallTest extends TestKit(ActorSystem("WebCallTest",
ConfigFactory.parseString(TestConfig.config)))
with DefaultTimeout with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {
val webCallRef = system.actorOf(Props(classOf[WebCall]))
override def afterAll {
shutdown()
}
"A WebCall" should {
"Respond to a 'example' call with a 'success' message" in {
within(5000 millis) {
webCallRef ! MessgeWithParam("example")
expectMsg("success")
}
}
}
}
当消息是案例类时,我处于不同的上下文中吗?
感谢您的帮助
关心Akira