Skip to content

Instantly share code, notes, and snippets.

@asim
Created August 8, 2010 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asim/514163 to your computer and use it in GitHub Desktop.
Save asim/514163 to your computer and use it in GitHub Desktop.
import scala.actors.Actor
import scala.actors.Actor._
import javax.mail._
import javax.mail.internet._
import java.util.Properties._
case class Request(sender : Actor, payload : String)
case class Ready(sender : Actor)
case object Stop
object Client extends Application {
val body = scala.io.Source.fromFile("foo1").mkString
def consumer(n : Int) = actor {
loop {
react {
case Ready(sender) =>
sender ! Ready(self)
case Request(sender, payload) =>
//println("request to consumer " + n + " with " + payload)
// Set up the mail object
val properties = System.getProperties
properties.put("mail.smtp.host","127.0.0.1")
properties.put("mail.smtp.port", "2025")
val session = Session.getDefaultInstance(properties)
val message = new MimeMessage(session)
// Set the from, to, subject, body text
message.setFrom(new InternetAddress("test@example.org"))
message.setRecipients(Message.RecipientType.TO, "spam@mopoke.co.uk")
message.setSubject("Greetings from langref.org")
message.setText(body)
Transport.send(payload)
println("consumer " + n + " is done processing")
case Stop => exit
}
}
}
// a pool of 10 consumers
val consumers = for (n <- 0 to 1000) yield consumer(n)
val coordinator = actor {
loop {
react {
case msg @ Request(sender, payload) =>
consumers.foreach( arg => { arg ! Ready(self) })
react {
// send the request to the first available consumer
case Ready(consumer) => consumer ! msg
}
case Stop =>
consumers.foreach( arg => { arg ! Stop } )
exit
}
}
}
for (i <- 0 to 1000) coordinator ! Request(self, body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment