Last active
January 21, 2016 02:34
-
-
Save skuenzli/1c4d409c7da9ca8f9649 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
class NumberController { | |
static final FiniteDuration DURATION_3_SECONDS = FiniteDuration.create(3, TimeUnit.SECONDS) | |
static final TIMEOUT_3_SECONDS = Timeout.durationToTimeout(DURATION_3_SECONDS) | |
ActorSystem actorSystem //actorSystem is injected by Spring via standard Grails behavior | |
def random() { | |
def requestId = UUID.randomUUID().toString() | |
// describe the properties of the desired actor | |
def actorType = "NumberActor" | |
Props props = SpringExtProvider.get(actorSystem).props(actorType) | |
// ask akka to create the actor | |
// use unique actor name because this will be an ephemeral, stateless actor | |
// managed as a prototype bean in the spring context | |
def actorName = "${actorType}-${requestId}" | |
ActorRef numberActorRef = actorSystem.actorOf(props, actorName) | |
def query = new NumberActor.GetRandomInteger(id: requestId, max: Integer.MAX_VALUE) | |
Future<Object> futureResults = ask(numberActorRef, query, TIMEOUT_3_SECONDS) | |
Promises.task { | |
def randomIntegerResults = Await.result(futureResults, DURATION_3_SECONDS) | |
println("randomIntegerResults: " + randomIntegerResults) | |
actorSystem.stop(numberActorRef) | |
render(contentType: 'application/json') { | |
id = requestId | |
randomInteger = randomIntegerResults | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment