0

I am learning twitter's RPC asynchronous system and it's important use filter and service deal with task logical.I have read the paper of https://twitter.github.io/finagle/guide/ServicesAndFilters.html and try write a simple code to make true I knows. But there some problem:

import com.twitter.util.Future

trait Service[Req, Rep] extends (Req => Future[Rep])

abstract class Filter[-ReqIn, +RepOut, +ReqOut, -RepIn]
  extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut])

trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

class DoubleFilter(times: Int) extends SimpleFilter[(Int,Int), Int] {
  def apply(request: (Int,Int), service: Service[(Int,Int), Int]): Future[Int] = {
    service((request._1 * times, request._2 * times))
  }
}

class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
  def apply(request: (Int, Int), service: Service[(Int, Int),Int]): Future[Int] = {
    service((request._1 + count, request._2 + count))
  }
}

object Test extends App {
  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)

  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int,Int), Int] {
    def apply(req: (Int,Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }

  val serviceWithDoble = doubleFilter andThen addELemsService

  val doubleWithAddFilter = doubleFilter andThen addFilter
}

Result with two compile error: value andThen is not a member of DoubleFilter!
What I miss understand? I'm straight think doc ignore something but I mislead.

LoranceChen
  • 2,453
  • 2
  • 22
  • 48
  • There should use com.twitter.finagle.Filter.andThen rather than scala's andThen, I will add explain it later. I'm see the source code.(I'm fresh and little tutorials) – LoranceChen Jan 05 '16 at 08:07

1 Answers1

0

I think You are importing wrong trait/class. I tried compiling following code with proper imports and it compiled without errors.

import com.twitter.finagle.{Service, Filter}
import com.twitter.util.Future


object Test extends App {

  trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

  class DoubleFilter(times: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 * times, request._2 * times))
    }
  }

  class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 + count, request._2 + count))
    }
  }

  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)
  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int, Int), Int] {
    def apply(req: (Int, Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }
  val serviceWithDoble = doubleFilter andThen addELemsService
  val doubleWithAddFilter = doubleFilter andThen addFilter

}
M Kumar
  • 566
  • 1
  • 4
  • 10