Skip to content

Instantly share code, notes, and snippets.

@gridgain
Created June 3, 2011 03:36
Show Gist options
  • Save gridgain/1005829 to your computer and use it in GitHub Desktop.
Save gridgain/1005829 to your computer and use it in GitHub Desktop.
My code from Scala Days 2011 presentation.
import java.util.concurrent.CountDownLatch
import java.util._
import org.gridgain.scalar._
import scalar._
import org.gridgain.grid.GridClosureCallMode._
import org.gridgain.grid._
object ScalaDays {
def main(args: Array[String]): Unit = scalar {
pingPong()
}
def pingPong() {
val g = grid$
if (g.remoteNodes().size < 2)
sys.error("I need more than 2 nodes")
else {
val n1 = g.remoteNodes$().head
val n2 = g.remoteNodes$().tail.head
n1.remoteListenAsync(n2, new GridListenActor[String] {
def receive(nid: UUID, msg: String) {
println(msg)
val latch: CountDownLatch = g.nodeLocal.get("latch")
latch.getCount match {
case 1 => stop("STOP")
case _ => respond("PING")
}
latch.countDown
}
}).get
n2.remoteListenAsync(n1, new GridListenActor[String] {
def receive(nid: UUID, msg: String) {
println(msg)
msg match {
case "PING" => respond("PONG")
case "STOP" => stop
}
}
}).get
n1 *< (UNICAST, () => {
val latch = new CountDownLatch(10)
g.nodeLocal[String, CountDownLatch].put("latch", latch)
n2 !< "PING"
latch.await()
})
}
}
def sayHi() = println("I'm in this grid!")
def mapReduce(msg: String): Int =
grid$.remoteProjection() @< (
SPREAD,
for (w <- msg.split(" ")) yield () => {
println("Calculating for: " + w)
w.length
},
(s: Seq[Int]) => {
println("Aggregating: " + s)
s.sum
}
)
def helloWorld(msg: String) =
grid$.remoteProjection() *<
(SPREAD, for (w <- msg.split(" ")) yield () => println(w.toUpperCase))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment