Search code examples
scalafinagle

how to get this finagle example to work?


I have been looking at some finagle tutorials. I found this code for a simple server, but I can not get it to work. Should Service be specified with type so that it is possible to find the type of serve?

Code:

import com.twitter.finagle.{Http, Service}
import org.jboss.netty.handler.codec.http.{HttpRequest, HttpResponse, DefaultHttpResponse}
import org.jboss.netty.handler.codec.http.HttpVersion._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import com.twitter.util.{Future, Await}
object Server{
  def main(args: Array[String]) {
    val service = new Service[HttpRequest, HttpResponse] {
    def apply(req: HttpRequest) =
        Future.value(new DefaultHttpResponse(HTTP_1_1, OK))
    }
    val server = Http.serve(":8080", service)
    Await.ready(server)
 }
}  

Error:

Error message:
Error:(25, 23) overloaded method value serve with alternatives:
  (addr: java.net.SocketAddress,service: com.twitter.finagle.ServiceFactory[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response])com.twitter.finagle.ListeningServer <and>
  (addr: String,service: com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response])com.twitter.finagle.ListeningServer <and>
  (addr: String,service: com.twitter.finagle.ServiceFactory[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response])com.twitter.finagle.ListeningServer <and>
  (addr: java.net.SocketAddress,service: com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response])com.twitter.finagle.ListeningServer
 cannot be applied to (String, com.twitter.finagle.Service[org.jboss.netty.handler.codec.http.HttpRequest,org.jboss.netty.handler.codec.http.HttpResponse]{def apply(req: org.jboss.netty.handler.codec.http.HttpRequest): com.twitter.util.Future[org.jboss.netty.handler.codec.http.DefaultHttpResponse]})
    val server = Http.serve(":8080", service)
                      ^

link: https://twitter.github.io/finagle/docs/#com.twitter.finagle.package

link to scala school code as mentioned in my comment: https://twitter.github.io/scala_school/finagle.html#server

Here is my build.sbt:

name := "finagle"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++=  Seq(
  "com.twitter" %% "finagle-http" % "6.31.0",
)

Solution

  • The problem is that in finagle 6.30.x the Httpx package was renamed back to Http and the original Http (which contained netty http types) was removed. you should build the service with c.t.f.h.Request c.t.f.h.Response types