Skip to content

Instantly share code, notes, and snippets.

@mrioan
Created December 20, 2013 15:01
Show Gist options
  • Save mrioan/8055969 to your computer and use it in GitHub Desktop.
Save mrioan/8055969 to your computer and use it in GitHub Desktop.
Option B: Non Blocking API
Option B: Non Blocking API
Stormpath Wrapper Service
-------------------------
class NonBlockingExecution(implicit executionContext: ExecutionContext) {
def doAsync[S]( func: () => S) : Future[S] = {
val promise = Promise[S]
promise.success(func())
promise.future
}
}
Client using the Wrapper
------------------------
object Main {
var applicationRestUrl : String = "https://api.stormpath.com/v1/applications/sTwbyZ1qo74eDM4gTo2H93"
var path : String = System.getProperty("user.home") + "/.stormpath/apiKey.properties"
var client : Client = new ClientBuilder().setApiKeyFileLocation(path).build()
var application : Application = client.getResource(applicationRestUrl, classOf[Application])
def main(args: Array[String]) {
val authenticationResult : AuthenticationResult = application.authenticateAccount(new UsernamePasswordRequest("[email protected]", "XXX", null))
val account : Account = authenticationResult.getAccount()
val maxBlockingBacklog: Int = 100
val service = new ThreadPoolExecutor(32, /* corePoolSize */
64, /* maximumPoolSize */
120L, TimeUnit.SECONDS, /* keepAliveTime */
new ArrayBlockingQueue[Runnable](
maxBlockingBacklog,
false /* we don't care about FIFO */
), /* workQueue */
Executors.defaultThreadFactory())
// allow the pool to shrink below corePoolSize after keepAliveTime
service.allowCoreThreadTimeOut(true)
implicit val executionContext = ExecutionContext.fromExecutorService(service)
val execution : NonBlockingExecution = new NonBlockingExecution()(executionContext)
val future : Future[GroupList] = execution.doAsync(
() => account.getGroups
)
future onSuccess {
case groupList => groupList.foreach(println(_))
}
executionContext.shutdown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment