views:

121

answers:

1

I am trying to create a producer/consumer type Scala app. The LoopControl just sends a message to the MessageReceiver continually. The MessageReceiver then delegates work to the MessageCreatorActor (whose work is to check a map for an object, and if not found create one and start it up). Each MessageActor created by this MessageCreatorActor is associated with an Id. Eventually this is where I want to do business logic. But I run out of memory after 15 minutes. Any help is appreciated

import scala.actors.Actor
import java.util.HashMap;
import scala.actors.Actor._

case object LoopControl
case object MessageReceiver
case object MessageActor
case object MessageActorCreator

class MessageReceiver(msg: String) extends Actor {

 var messageActorMap = new HashMap[String, MessageActor]
 val messageCreatorActor  = new MessageActorCreator(null, null)

 def act() {
  messageCreatorActor.start
  loop {
   react {
   case MessageActor(messageId) =>   
   if (msg.length() > 0) {         
     var messageActor = messageActorMap.get(messageId);

     if(messageActor == null) {
      messageCreatorActor ! MessageActorCreator(messageId, messageActorMap)
     }else {   
      messageActor ! MessageActor
     }
    }
   }
  }
 }
}

case class MessageActorCreator(msg:String, messageActorMap: HashMap[String, MessageActor]) extends Actor {
 def act() {
   loop {
     react {
       case MessageActorCreator(messageId, messageActorMap) =>
         if(messageId != null ) {
    var messageActor = new MessageActor(messageId);
    messageActorMap.put(messageId, messageActor)
    println(messageActorMap)
    messageActor.start
    messageActor ! MessageActor
        }
     }
   }
    }
}

class LoopControl(messageReceiver:MessageReceiver) extends Actor {
  var count : Int = 0;
  def act() {
    while (true) {
          messageReceiver ! MessageActor ("00-122-0X95-FEC0" + count)
          //Thread.sleep(100)
          count = count +1;
          if(count > 5) {
           count = 0;
          }
    }
  }
}

case class MessageActor(msg: String) extends Actor {
 def act() {
   loop {
     react {
       case MessageActor =>
         println()
         println("MessageActor: Got something-> " + msg) 
     }
   }
    }
}

object messages extends Application {

 val messageReceiver = new MessageReceiver("bootstrap")
 val loopControl = new LoopControl(messageReceiver)

 messageReceiver.start
 loopControl.start
}
+2  A: 

I am wondering if your code is actually finding the existing objects in the map?

If it isn't ever finding the old ones it would keep creating new MessageActors until running out of memory.

Try testing for that.

Zan Lynx
It is finding the existing object in the map. I could verify that using a println(...)
Nick