Twitter-server defines a template from which servers at Twitter are built. Twitter-server ensures that common components like an administrative HTTP server, tracing, stats, etc. are wired in correctly for production use at Twitter.


Twitter-server is published with Maven:



or, with sbt:

resolvers += "twttr" at ""

libraryDependencies += "com.twitter" %% "twitter-server" % "1.0.2"

NB: you only need to add the repository if you want to use libraries in twitter common, which are only published to For example, finagle-stats, which adds metrics, requires a twitter common library.

First we’ll need to import a few things into our namespace.

import com.twitter.finagle.{Http, Service}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
import org.jboss.netty.handler.codec.http._

Twitter-server defines its own version of the standard main. To use it, create an object extended with com.twitter.server.TwitterServer, and define the main() method (no arguments).

In this example, we use the finagle HTTP server to start a server on port 8888, the service bound to this port is a simple hello service.

object BasicServer extends TwitterServer {
  val service = new Service[HttpRequest, HttpResponse] {
    def apply(request: HttpRequest) = {
      val response =
        new DefaultHttpResponse(request.getProtocolVersion, HttpResponseStatus.OK)
      response.setContent(copiedBuffer("hello", Charsets.Utf8))

  def main() {
    val server = Http.serve(":8888", service)
    onExit {

onExit is used to register code to be run when the process shutdown is requested.

After compiling, we can start the server like any other java or scala process.

$ java -jar target/myserver-1.0.0-SNAPSHOT.jar &
[1] 66569
Feb 21, 2013 10:55:57 AM com.twitter.finagle.http.HttpMuxer$$anonfun$5 apply
INFO: HttpMuxer[/admin/metrics.json] = com.twitter.finagle.stats.MetricsExporter(<function1>)
Feb 21, 2013 10:55:57 AM com.twitter.finagle.http.HttpMuxer$$anonfun$5 apply
INFO: HttpMuxer[/stats] = com.twitter.finagle.stats.OstrichExporter(<function1>)

$ curl localhost:8888

This server is fully configured to run in Twitter’s production environment; see features for more details.


Twitter-server is a library upon which servers are built to run in the Twitter production environment.

Useful Links