Skip to content

Instantly share code, notes, and snippets.

@mariusae
Created February 21, 2012 18:53
Show Gist options
  • Select an option

  • Save mariusae/1878117 to your computer and use it in GitHub Desktop.

Select an option

Save mariusae/1878117 to your computer and use it in GitHub Desktop.
package com.twitter.finagle.example.echo
package router
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder, Server}
import com.twitter.util.Future
import java.net.InetSocketAddress
object EchoServer {
def main(args: Array[String]) {
val service = new Service[String, String] {
def apply(request: String) = {
println("I'm server,I received request message:" + request)
Future.value("I'm server. Where are you from?")
}
}
val server: Server = ServerBuilder()
.codec(StringCodec)
.bindTo(new InetSocketAddress(8082))
.name("Server")
.build(service)
}
}
object EchoRouter {
def main(args: Array[String]) {
val client: Service[String, String] = ClientBuilder()
.codec(StringCodec)
.hosts(new InetSocketAddress(8082))
.hostConnectionLimit(1)
.logger(java.util.logging.Logger.getLogger("client"))
.build()
val proxyService = new Service[String, String] {
def apply(request: String) = {
println("I'm router, I received request message:" + request)
client(request+"\n").onSuccess {
result => println("Router received result asynchronously: "
+ result)
} onFailure {
error => error.printStackTrace()
}
}
}
val server: Server = ServerBuilder()
.codec(StringCodec)
.bindTo(new InetSocketAddress(8081))
.name("Router")
.logger(java.util.logging.Logger.getLogger("xxx"))
.build(proxyService)
}
}
object EchoClient {
def main(args: Array[String]) {
val client: Service[String, String] = ClientBuilder()
.codec(StringCodec)
.hosts(new InetSocketAddress(8081))
.hostConnectionLimit(1)
.build()
client("Hi,I'm client.\n").onSuccess {
result => println("Client received result asynchronously: " +
result)
} onFailure {
error => error.printStackTrace()
} ensure {
client.release()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment