ScalaPegasusAsyncTable
ScalaPegasusAsyncTable接口提供的方法均为异步API,封装了java client的异步接口。对应API功能可参考ScalaPegasusClient接口和Java客户端文档#pegasusTableInterface,接口封装形式如:
- @throws[PException] def multiGetH, S (implicit hSer: SER[H], sSer: SER[S]): Future[MultiGetResult[S, Array[Byte]]] = { val result = table.asyncMultiGet(hashKey, sortKeys, maxFetchCount, maxFetchSize, timeout) toScala(result)(convertMultiGetResult[S]) }
其中table.asyncMultiGet(hashKey, sortKeys, maxFetchCount, maxFetchSize, timeout)
即Java client接口,参数传递原理参见实现原理,toScala(result)(convertMultiGetResult[S])
的完整形式如下:
- implicit private [scalaclient] def toScala[A, B](future: NFuture[A])(implicit f: A => B): Future[B] = {
- val promise = Promise[B]()
- future.addListener(new GenericFutureListener[NFuture[_ >: A]] {
- override def operationComplete(future: NFuture[_ >: A]): Unit = {
- if (future.isSuccess) {
- promise.success(f(future.get.asInstanceOf[A]))
- } else {
- promise.failure(future.cause())
- }
- }
- })
- promise.future
- }
使用隐式转换
实现Java的异步编程到Scala的异步编程变换,该函数利用io.netty.util.concurrent.{GenericFutureListener, Future => NFuture}
实现异步编程