Skip to content

Instantly share code, notes, and snippets.

@gkojax
Created May 19, 2012 08:15
Show Gist options
  • Save gkojax/2730047 to your computer and use it in GitHub Desktop.
Save gkojax/2730047 to your computer and use it in GitHub Desktop.
2012/05/19 Finagleハッカソン
package com.twitter.finagle.example.http
import java.net.InetSocketAddress
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future
import com.twitter.finagle.http.path._
import com.twitter.finagle.builder.{Server, ServerBuilder}
import com.twitter.finagle.http._
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.Service
import org.apache.thrift.protocol.TBinaryProtocol
import com.twitter.finagle.thrift.{ThriftClientFramedCodec, ThriftClientRequest}
import com.twitter.finagle.example.thrift._
import java.util.Date
object HttpServer {
val thriftService: Service[ThriftClientRequest, Array[Byte]] = ClientBuilder()
.hosts(new InetSocketAddress(8090))
.codec(ThriftClientFramedCodec())
.hostConnectionLimit(1)
.build()
class Respond extends Service[Request, Response] {
def apply(request: Request) = {
val client = new Hello.ServiceToClient(thriftService, new TBinaryProtocol.Factory())
println("client start: " + new Date().toString)
client.hi() onSuccess { thriftResponse =>
println("getSuccess: " + new Date().toString)
} ensure {
println("ensure: " + new Date().toString)
}
val response = Response()
println("response " + new Date().toString)
response.setContentString("Ok! " + new Date().toString)
Future.value(response)
}
}
def main(args: Array[String]) {
val respond = new Respond
val httpService: Service[Request, Response] = respond
val server: Server = ServerBuilder()
.codec(RichHttp[Request](Http()))
.bindTo(new InetSocketAddress(8080))
.name("httpserver")
.build(httpService)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment