1

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",
)
stian
  • 1,947
  • 5
  • 25
  • 47
  • I am having problems with several of the codes available on finagle, for instance the server code on scala school, which gives me an error based on overloaded method value build with alternatives. – stian Feb 03 '16 at 12:48
  • Is there any particular reason you're using `netty` packages? Could you provide a link to the tutorial? – TNW Feb 03 '16 at 12:51
  • @TNW. no, just copying what they are showing. By the way, I am using sbt to include libraryDependencies. – stian Feb 03 '16 at 12:59
  • is this exactly the code of the example (I can't find it on the page)? Because your `service` is a different (netty rather than finagle) type from the what the `Http.serve` call is expecting. I suspect the import statements are causing things like Service to resolve to something different – The Archetypal Paul Feb 03 '16 at 13:34
  • @Paul. The code that I show here in the thread should be copied and pasted from the top link. Clicking the link I come to a view immediately seeing the code snipped ("Thus a simple HTTP server is built like this:..). The only thing I have done differently is to wrap it into a singleton (object with a main). – stian Feb 03 '16 at 13:41
  • You should not define a Main method. Instead call it something else and let your server object extend `App` – Kao Feb 03 '16 at 13:45
  • @kao. I have successfully used main on objects before instead of extending from App. However, I tried your suggestion (called object S (extends App (removing the main)) and it doesn't resolve my issues. – stian Feb 03 '16 at 13:50
  • Which finagle version are you using? – Kao Feb 03 '16 at 13:51
  • @kao. I decided to add my whole build.sbt content just incase there is something missing. – stian Feb 03 '16 at 13:55
  • This finc-core looks weird to me. Did you mean finch-core? And if so, Finch is an abstraction layer on top of finagle, not actual Finagle core. – Kao Feb 03 '16 at 13:58
  • @kao. That was just redundant code (now removed) since I also tried some finch stuff. having those references in the build.sbt has no impact on the code in this thread – stian Feb 03 '16 at 14:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102485/discussion-between-kao-and-stian). – Kao Feb 03 '16 at 14:09

1 Answers1

1

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

teodimoff
  • 133
  • 5