私の知る限り、automagicThrift.newIface[Iface]
メソッドを使用してサービスを作成すると、それを閉じることはできません。これは、結果の値についてコードが知っているのは、それが に準拠しているということだけだからIface
です。閉じる必要がある場合は、クライアントを 2 つの手順でインスタンス化できます。1 つは Thrift サービスを作成し、もう 1 つはそれをインターフェイスに適応させます。
Scrooge を使用して Thrift インターフェイスを生成している場合は、次のようになります。
val serviceFactory: ServiceFactory[ThriftClientRequest,Array[Byte]] =
Thrift.newClient(s"$host:$port")
val client: MyPingService[Future] =
new MyPingService.FinagledClient(serviceFactory.toService)
doStuff(client).ensure(serviceFactory.close())
私はreplでこれを試しましたが、うまくいきました。軽く編集されたトランスクリプトを次に示します。
scala> val serviceFactory = Thrift.newClient(...)
serviceFactory: ServiceFactory[ThriftClientRequest,Array[Byte]] = <function1>
scala> val tweetService = new TweetService.FinagledClient(serviceFactory.toService)
tweetService: TweetService.FinagledClient = TweetService$FinagledClient@20ef6b76
scala> Await.result(tweetService.getTweets(GetTweetsRequest(Seq(20))))
res7: Seq[GetTweetResult] = ... "just setting up my twttr" ...
scala> serviceFactory.close
res8: Future[Unit] = ConstFuture(Return(()))
scala> Await.result(tweetService.getTweets(GetTweetsRequest(Seq(20))))
com.twitter.finagle.ServiceClosedException
これはそれほど悪くはありませんが、私がまだ知らないより良い方法があることを願っています.