在参数

时间:2017-06-05 10:21:20

标签: scala playframework websocket akka actor

我正在尝试使用播放frmework中的akka​​ actor来实现websocket。

HomeController.scala

  def socket = WebSocket.accept[String, String] { request =>
    ActorFlow.actorRef(out => FileObserverActor.props(out))
  }

演员/ FileUploaderActor.scala

class FileUploaderActor extends Actor{
  override def receive: Receive = {

    case UploadFile(billerId, filename, subCategory, count, dueDate) =>

      val fileOberverActor = ActorSystem().actorOf(Props[FileObserverActor])
      val billerData = BillerFileUploadMetaData(billerId,filename,count,"ACTIVE",
        new java.sql.Timestamp(new java.util.Date().getTime),subCategory,dueDate)
      val sparkSession = SparkContextHelper.sparkSession;
      import sparkSession.implicits._
      val rdd = sparkSession.sparkContext.parallelize(Seq(billerData))
      val df = rdd.toDF()
      df.write.format("org.apache.spark.sql.cassandra").options(Map("keyspace" -> "billerplatform_schema", "table" -> "biller_file_uploads")).mode(SaveMode.Append).save
      fileOberverActor ! FileUploaded(filename, count)
  }
}

演员/ FileObserverActor.scala

class FileObserverActor(out: ActorRef) extends Actor{
  def receive = {
    case FileUploaded(fileName, totalRecords) =>
      out ! ("Got the file " + fileName)
  }

}

object FileObserverActor{
  def props(out: ActorRef)  = Props(new FileObserverActor(out))
}

获得追踪:

java.lang.IllegalArgumentException: no matching constructor found on class Actors.FileObserverActor for arguments []
    at akka.util.Reflect$.error$1(Reflect.scala:81)
    at akka.util.Reflect$.findConstructor(Reflect.scala:105)
    at akka.actor.NoArgsReflectConstructor.<init>(IndirectActorProducer.scala:103)
    at akka.actor.IndirectActorProducer$.apply(IndirectActorProducer.scala:60)
    at akka.actor.Props.producer(Props.scala:131)
    at akka.actor.Props.<init>(Props.scala:144)
    at akka.actor.Props$.apply(Props.scala:86)
    at Actors.FileUploaderActor$$anonfun$receive$1.applyOrElse(FileUploaderActor.scala:15)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:497)
    at Actors.FileUploaderActor.aroundReceive(FileUploaderActor.scala:10)

这是在FileUploaderActor中创建actor实例的问题吗?你能帮我调试这个问题吗?

编辑:  我为“FileObserverActor”创建了一个伴随对象,因为它需要通过websocket在控制器和FileObserverActor之间建立连接,

def socket = WebSocket.accept[String, String] { request =>
    ActorFlow.actorRef(out => FileObserverActor.props(out))
  }

现在我也希望从“FileUploadActor”向“FileObserverActor”发送消息,但我无法从“FileUploadActor”创建实例,因为“FileObserverActor”参数化为websout连接的“out:ActorRef”。现在如何从“FileUploadActor”向“FileObserverActor”发送消息?

1 个答案:

答案 0 :(得分:2)

正如错误消息指出的那样,它期待FileObserverActor的无参数构造函数,但它找不到。如果您在创建out: ActorRef时需要FileObserverActor,请考虑在创建时传递引用。

防御线在Actors/FileUploaderActor.scala

val fileOberverActor = ActorSystem().actorOf(Props[FileObserverActor])

您可能希望将其更改为

val fileOberverActor = ActorSystem().actorOf(FileObserverActor.props(outActorRef))

有关详细信息,请查看Akka中Props的官方文档。