Last active
October 10, 2015 16:58
-
-
Save timyates/3722681 to your computer and use it in GitHub Desktop.
A Ping-Pong HotSwapped Untyped Akka Actor in Groovy
This file contains 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
@Grab( 'com.typesafe.akka:akka-actor_2.10:2.3.2' ) | |
@Grab( 'com.typesafe:config:1.2.0' ) | |
import akka.actor.UntypedActor | |
import akka.japi.Procedure | |
import akka.actor.ActorSystem | |
import akka.actor.Props | |
import com.typesafe.config.ConfigFactory | |
class PingPongActor extends UntypedActor { | |
static final String PING = 'PING' | |
static final String PONG = 'PONG' | |
int count = 0 | |
void onReceive( message ) { | |
if( message == PING ) { | |
println PING | |
count++ | |
Thread.sleep 100 | |
self.tell PONG, self | |
context.become( [ apply:{ msg -> | |
if( msg == PONG ) { | |
println PONG | |
count++ | |
Thread.sleep 100 | |
self.tell PING, self | |
context.unbecome() | |
} | |
} ] as Procedure ) | |
if( count > 10 ) context.stop self | |
} | |
} | |
} | |
def cl = this.class.classLoader | |
ActorSystem.create( 'PingPong', ConfigFactory.load( cl ), cl ).with { system -> | |
system.actorOf( Props.create( PingPongActor ) ).with { actor -> | |
actor.tell PingPongActor.PING, actor | |
Thread.sleep 2000 | |
shutdown() | |
} | |
} |
PS: The reason for the ClassLoader jiggery-pokery is so it can be run in the Groovy Console. YMMV running outside of it.
Just updated this to (I think) the latest version of akka
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HotSwap docs here http://doc.akka.io/docs/akka/snapshot/java/untyped-actors.html#HotSwap