1

我想使用喷雾客户端从一个演员发出一个网络请求,这个网络请求需要一个参数,该参数必须是接收消息的一部分。为了做到这一点,我创建了一个案例类,如:

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")
       }
     } 
   } 
}

当消息是案例类时,我是否处于不同的上下文中?

感谢您的任何帮助

问候阿基拉

4

0 回答 0