Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Quick Start With Finagle

DZone's Guide to

Quick Start With Finagle

Want to test out an RPC system for the JVM designed for high-concurrency servers? Let's take Finagle for a spin with some Scala.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Finagle is an extensible RPC system for the JVM, used to construct high-concurrency servers. Finagle implements uniform client and server APIs for several protocols, and is designed for high performance and concurrency. Most of Finagle’s code is protocol agnostic, simplifying the implementation of new protocols.

Today, I am going to implement a Finagle example using Scala, where I am sending the request with some message and get a future response using Finagle.

First, let’s define a service. Here, we define a service to receive an HTTP request:

def apply(request: Request) = {
    request.method match {

        case Method.Post =>
        request.uri match {

            case "/" =>
            log.error("in post")
            val str = request.getContentString()
            //any business logic
            val response = Response(Version.Http11, Status.Ok)
            response.contentString = "Hello..!! " + str
            Future.value(response)

            case _ =>
            log.error("REQUEST NOT FOUND")
            Future.value(Response(Version.Http11, Status.NotFound))
        }

        case Method.Get =>
        request.uri match {

            case "/" =>
            val str = request.getContentString()
            //any business logic
            val response = Response(Version.Http11, Status.Ok)
            response.contentString = "Thank You " + str
            Future.value(response)

            case _ =>
            log.error("REQUEST NOT FOUND")
            Future.value(Response(Version.Http11, Status.NotFound))
        }
    }
}


Here, we get the request. After that, we are simply matching what type of request it is, either GET or POST. Request.uri tell us about the endpoint of the request.

Then, initiate and start our server:

import java.net.InetSocketAddress
import com.twitter.finagle.Http
import com.twitter.finagle.builder. {
    Server,
    ServerBuilder
}

class ComputeServerBuilder {
    val response = new ComputeResponse
    val address = new InetSocketAddress(10000)

    def start: Server = ServerBuilder()
        .stack(Http.server)
        .bindTo(address)
        .name("HttpServer")
        .build(response)
}


Last, let’s define a client to consume this server.

computeServerBuilder = new ComputeServerBuilder
server = computeServerBuilder.start
client = ClientBuilder()
    .stack(Http.client)
    .hosts(computeServerBuilder.address)
    .hostConnectionLimit(1)
    .build()


Now, we can send any number of requests using this client to our server.

Here, I made some test cases to hit the server can check whether we are getting a response successfully or not like shown below:

val postRequest = Request(Version.Http11, Method.Post, "/")
postRequest.setContentString("Knoldus")
val postFutureResponse = client(postRequest).asScala
postFutureResponse.map(response => println(response.getContentString()))
postFutureResponse.map(response => assert(response.status === Status.Ok && response.contentString.contains("Knoldus")))


To run it by yourself, you can also clone my sample example from my git repo.

When I start implementing the Finagle, the challenge that I face in using Finagle is in handling the future between two different APIs, i.e. Scala and Twitter itself. To deal with this, I used some implicit conversion of futures between these two APIs. That code is also available on my repo.

To learn about the core of Finagle, you can also read this great blog: Finagle: Controlling the Future Of RPC systems which helped me a lot while I was learning.

References

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
scala ,rest api ,java ,finagle ,rpc ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}