Created
August 8, 2010 15:39
-
-
Save asim/514163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[email protected]")) | |
message.setRecipients(Message.RecipientType.TO, "[email protected]") | |
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