Finagle - an intro to rpc & a sync programming in jvm
1. Finagle - An Intro to RPC &
Async programming in JVM
Prasanna Kumar.S
prasanna.sathyanarayanan@csscorp.com
Twitter @prasonscala
2. Agenda
Why RPC & Async programming is Interesting ?
Lets know about "Future" & "Promise "
Finagle !!!! - Writing Protocol Handlers
Code - Lets get in to Action
Use Cases - Where could I use Finagle
Q&A
3. Why RPC & Async
programming ?
I know my infrastructure
I wanna provide an platform
Isolated computation with callable API's
Don't call me let me call u !!!
RPC + Async gives steroids for your server apps
4. Lets know about
Future
The fundmental abstraction of Async Programming (Fingale as
well :-) )
A Future is computation which has not yet
performed, i.e it is supposed to happen !!!
6. Exposing a Service - (Server
Side)
// implement a service
val service = new Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest) = {
Future(new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK))
}
}
// configure the service
ServerBuilder().codec(Http()).bindTo(new InetSocketAddress(10000)).na
me("chutti-server").build(service)
7. Consuming with Client
val client = ClientBuilder().codec(Http()).hosts("10.10.27.14:10000")
.hostConnectionLimit(1).build()
val req = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET, "/")
val fut = client(req)
fut onSuccess (msg => {
println(msg)
})
8. Putting it all
object ChuttiServer {
private lazy val globalResponse = (req: HttpRequest) => {
val resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResp
onseStatus.OK)
val htmlRes = "welcome finagle session !!!!".getBytes()
resp.setContent(ChannelBuffers.copiedBuffer(htmlRes))
Future.value(resp)
}
def start(): Unit = {
// implement a service
val service = new Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest) = {
req.getUri() match {
case "/" => globalResponse(req)
case _ => Future(new DefaultHttpResponse(HttpVersion.HTTP_1
_1, HttpResponseStatus.NOT_FOUND))
} } }
val fil = new ChuttiServerFilter[HttpRequest, HttpResponse](Durat
ion.fromMilliseconds(100), new JavaTimer)
val svc = fil andThen fil andThen service
// configure the service
ServerBuilder().codec(Http()).bindTo(new InetSocketAddress(10000)
).name("localhost").build(svc)
}
}
// Filter definition
class ChuttiServerFilter[HttpRequest, HttpResponse](timeout: Duration
, timer: Timer)
extends Filter[HttpRequest, HttpResponse, HttpRequest, HttpResponse]
10. Putting it all (Contd.)
object ChuttiClient {
def main(args: Array[String]) {
val client = ClientBuilder().codec(Http()).
hosts("10.10.27.14:10000").hostConnectionLimit(1).build()
val req = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/")
val fut = client(req)
fut onSuccess (msg => {
println(msg)
})
}
}