Search code examples
arraysscalaconcurrencyakkaactor

Akka actors not receiving Array[Byte]?


I am trying to write an actor that counts messages it receives and prints when it hits a specific number of messages seen.

I got it to work when I am sending the messages to the actor as a string, but when I try to send and Array[Bytes], the actor doesn't perform it's receive function, but it also doesn't invoke its catch else case.

Code for handling strings which works:

class CountingActor extends Actor {

  val log = Logging(context.system, this)
  val startTime = System.currentTimeMillis
  var count = 0
  val actorName = self.path.name

  def Count: Actor.Receive = {
    case message: String =>
      count += 1
      if(count % 50000 == 0 && count != 0){
        var elapsed = (System.currentTimeMillis - startTime) / 1000.0
        var mps = count / elapsed
        log.info(s"Processed $count messages in $elapsed ($mps msg/s) in $actorName")
      }

    case _ => log.info("Something happened and I dont know, it wasn't a string")
  }

  def receive = Count

}

The code that fails to process and Array[Byte] is exactly the same but I specify the case as Array[Byte] instead of string.

class CountingActor extends Actor {

  val log = Logging(context.system, this)
  val startTime = System.currentTimeMillis
  var count = 0
  val actorName = self.path.name

  def Count: Actor.Receive = {
    case message: Array[Byte] =>
      count += 1
      if(count % 50000 == 0 && count != 0){
        var elapsed = (System.currentTimeMillis - startTime) / 1000.0
        var mps = count / elapsed
        log.info(s"Processed $count messages in $elapsed ($mps msg/s) in $actorName")
      }

    case _ => log.info("Something happened and I dont know, it wasn't a string")
  }

  def receive = Count

}

Solution

  • I've tried this scenario

      val s = ActorSystem()
      val ca = s.actorOf(Props[CountingActor])
      ca ! Array[Byte](1, 1, 0)
    

    and it works just fine. Try to add additional logging after

    case message: Array[Byte] =>
    

    to see this. Now your code doesn't show any visible reaction on this message if count is less than 50000, so how do you know that "the actor doesn't perform it's receive function"?