Scala newbie vproducer/consumer attempt running out of memory

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-05-01T09:37:21Z Indexed on 2010/05/01 9:47 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|

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. Its finding the cached actors,but quickly runs out of memory. Any help is appreciated. Or any one has any good code on producers consumers doing real stuff (not just adding numbers), please post.

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
}

© Stack Overflow or respective owner

Related posts about scala

Related posts about producer