我正在学习twitter的RPC异步系统,重要的是使用过滤器和服务来处理任务逻辑。我已经阅读了https://twitter.github.io/finagle/guide/ServicesAndFilters.html的文章,并尝试编写一个简单的代码来使我知道。但是有一些问题:
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
}
有两个编译错误的结果:value而且它不是DoubleFilter的成员!
我想念的是什么?我直接认为doc忽略了一些东西,但我误导了。
答案 0 :(得分:0)
我认为你输入了错误的特质/类。我尝试使用正确的导入编译以下代码,并且编译时没有错误。
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
}