Skip to content

Instantly share code, notes, and snippets.

@kramer
Created August 6, 2014 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kramer/4fea054842bd9f76441e to your computer and use it in GitHub Desktop.
Save kramer/4fea054842bd9f76441e to your computer and use it in GitHub Desktop.
Heavy Self Calling Ratpack
buildscript {
repositories {
maven { url "http://oss.jfrog.org/repo" }
mavenCentral()
}
dependencies {
classpath 'io.ratpack:ratpack-gradle:0.9.7'
}
}
repositories {
maven { url "http://oss.jfrog.org/repo" }
mavenCentral()
maven { url "http://repo.springsource.org/repo" } // for springloaded
}
// The “ratpack” plugin applies the “application” plugin, making it easy to create a standalone application.
// See: http://gradle.org/docs/current/userguide/application_plugin.html
apply plugin: "io.ratpack.ratpack-groovy"
// The “ratpack” plugin is IDEA aware.
// It will create a run configuration in IDEA to launch your app in your IDE, with hot reloading.
apply plugin: "idea"
idea {
project {
jdkName "1.7"
languageLevel "1.7"
}
}
configurations.all {
exclude module: "groovy"
}
dependencies {
// SpringLoaded enables runtime hot reloading.
// It is not part of the app runtime and is not shipped in the distribution.
springloaded "org.springsource.springloaded:springloaded-core:1.1.4"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0", {
exclude module: "groovy-all"
}
compile 'io.ratpack:ratpack-rx:0.9.7'
compile 'com.ning:async-http-client:1.8.13'
}
import com.ning.http.client.AsyncHttpClient
import com.ning.http.client.AsyncHttpClientConfig
import rx.schedulers.Schedulers
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import static ratpack.groovy.Groovy.ratpack
import static ratpack.rx.RxRatpack.initialize
import static ratpack.rx.RxRatpack.observe
ratpack {
bindings {
initialize()
bind AsyncHttpClient.class, new AsyncHttpClient(
new AsyncHttpClientConfig.Builder().setConnectionTimeoutInMs(5000).setRequestTimeoutInMs(5000).build()
)
}
handlers {
get("multi") { AsyncHttpClient client ->
// call "single" endpoint multiple times
def count = 0
rx.Observable.from((1..2000)).flatMap({
/*
[1]
*/
rx.Observable.from(
client.prepareGet("http://127.0.0.1:5050/single").execute(),
Schedulers.io()
)
/*
[2]
observe(blocking({
client.prepareGet("http://127.0.0.1:5050/single").execute()
}))
*/
/*
[3]
rx.Observable.from(
client.prepareGet("http://127.0.0.1:5050/single").execute(),
new ExecControllerBackedScheduler(launchConfig.execController)
).doOnError({
it.printStackTrace()
})
*/
/*
[4]
rx.Observable.from(
client.prepareGet("http://127.0.0.1:5050/single").execute(),
Schedulers.from(launchConfig.execController.blockingExecutor)
).doOnError({
it.printStackTrace()
})
*/
}).subscribe({
println count
count++
}, {
it.printStackTrace()
}, {
println "complete! - emitted $count times"
render "complete! - emitted $count times"
})
}
get("single") {
observe(blocking({
// simulate some blocking load
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec("0123456789abcdef".bytes, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec("AAAAAAAAAAAAAAAA".bytes));
6400.times { cipher.update(it.byteValue()) }
cipher.doFinal("0123456789ABCDEF".bytes).toString()
})).subscribe({
render "ok $it"
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment