Created
December 20, 2013 15:00
-
-
Save mrioan/8055959 to your computer and use it in GitHub Desktop.
Option A: Blocking API
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
Option A: Blocking API | |
Stormpath Wrapper Service | |
------------------------- | |
class BlockingExecution { | |
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) | |
def doSync[S]( func: () => S ) : S = { | |
implicit val stormpathExecutionContext = ExecutionContext.fromExecutorService(service) | |
val result: Future[S] = Future[S] { | |
func() | |
} | |
Await.result(result, 30 seconds) | |
} | |
} | |
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 execute : BlockingExecution = new BlockingExecution() | |
val groups : GroupList = execute.doSync(() => account.getGroups() ) | |
groups.foreach(println(_)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment