Search code examples
scalaplayframeworkwebsocketakkaactor

No matching constructor found on class Actors.FileObserverActor for arguments


I am trying to implement websocket using akka actors in play frmework.

HomeController.scala

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

Actors/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)
  }
}

Actors/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))
}

Getting trace:

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)

Is this a problem with creating an actor instance in FileUploaderActor? Can you help me to debug this issue.

Edit: I have created a companion object for "FileObserverActor" because its needed to establish a connection between controller and FileObserverActor through websocket,

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

Now i also want to send message to "FileObserverActor" from "FileUploadActor", but i couldnt create a instance from "FileUploadActor" since the "FileObserverActor" is parameterised with "out:ActorRef" for websocket connection. Now how can i send a message to "FileObserverActor" from "FileUploadActor" ?


Solution

  • As the error message points out it is expecting a no-argument constructor to FileObserverActor but it's not found. Given you need the out: ActorRef while creating FileObserverActor, consider passing a reference when you create it.

    The offending line is on Actors/FileUploaderActor.scala

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

    You might want to change it to something like

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

    For more information checkout the official documentation on Props in Akka.