Last active
August 29, 2015 14:01
-
-
Save mariogarcia/a740f0b58f80f6a6d5f3 to your computer and use it in GitHub Desktop.
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
import groovyx.gpars.actor.Actors | |
import groovyx.gpars.actor.DefaultActor | |
import groovyx.gpars.actor.DynamicDispatchActor | |
class SupervisedException extends Exception { | |
SupervisedException(String message) { | |
super(message) | |
} | |
} | |
class Supervised extends DefaultActor { | |
Supervisor supervisor | |
void onException(Throwable th) { | |
supervisor << new SupervisedException(th.message) | |
} | |
} | |
class Supervisor extends DynamicDispatchActor { | |
Supervised supervised | |
void link(Supervised supervised) { | |
this.supervised = supervised | |
this.supervised.supervisor = this | |
} | |
} | |
def supervised = new Supervised() { | |
void act() { | |
loop { | |
react { String message -> | |
if (message == 'gift') { | |
throw new Exception(message) | |
} | |
println "NORMAL: $message" | |
} | |
} | |
} | |
}.start() | |
def supervisor = new Supervisor() { | |
void onMessage(String query) { | |
supervised << query | |
} | |
void onMessage(SupervisedException ex) { | |
println "SUPERVISED ACTOR DIES WITH MESSAGE: ${ex.message}" | |
supervised = Actors.actor { react { println it } } | |
} | |
}.start() | |
supervisor.link(supervised) | |
supervisor << "something" | |
Thread.sleep(1000) | |
supervisor << "something" | |
Thread.sleep(1000) | |
supervisor << "gift" | |
Thread.sleep(2000) | |
supervisor << "Message processed by a new supervised instance" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment