twitter-finagle的兩個坑

javafan發表於2012-09-11

最近在用finagle,碰到的幾個坑: 1. 如果想直接用java呼叫scala啟動httpserveradmin,service需要start

AdminHttpService admin = new AdminHttpService(
                    9090,  123, new RuntimeEnvironment(this));
            TimeSeriesCollectorFactory seriesCollectorFactory = new TimeSeriesCollectorFactory();
            seriesCollectorFactory.apply(Stats.get(""), admin).start();
            admin.start();
  1. filter new SimpleFilter().andThen.(SimpleFilter).andThen.... 這種形式連續呼叫下去是可以的。

如果想把Filter放入list,然後遍歷出來呼叫。這樣:

CopyOnWriteArrayList<SimpleFilter<HttpRequest, HttpResponse>> chain = env.getFilters();
    Filter tmp = null;
    for (SimpleFilter<HttpRequest, HttpResponse> filter : chain) {
        if (tmp != null) {
            tmp = tmp.andThen(filter);
        } else {
            tmp = filter;
        }
    }

    Server server = ServerBuilder.safeBuild(
            tmp.andThen(httpserver),
            ServerBuilder.get()........

andThen的程式碼如下:

def andThen[Req2, Rep2](next: Filter[ReqOut, RepIn, Req2, Rep2]) =
new Filter[ReqIn, RepOut, Req2, Rep2] {
  def apply(request: ReqIn, service: Service[Req2, Rep2]) = {
    Filter.this.apply(request, new Service[ReqOut, RepIn] {
      def apply(request: ReqOut): Future[RepIn] = next(request, service)
      override def release() = service.release()
      override def isAvailable = service.isAvailable
    })
  }
}

相關文章