ScalaPegasusAsyncTable

ScalaPegasusAsyncTable接口提供的方法均为异步API,封装了java client的异步接口。对应API功能可参考ScalaPegasusClient接口Java客户端文档#pegasusTableInterface,接口封装形式如:

  1. @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])的完整形式如下:

  1. implicit private [scalaclient] def toScala[A, B](future: NFuture[A])(implicit f: A => B): Future[B] = {
  2. val promise = Promise[B]()
  3. future.addListener(new GenericFutureListener[NFuture[_ >: A]] {
  4. override def operationComplete(future: NFuture[_ >: A]): Unit = {
  5. if (future.isSuccess) {
  6. promise.success(f(future.get.asInstanceOf[A]))
  7. } else {
  8. promise.failure(future.cause())
  9. }
  10. }
  11. })
  12. promise.future
  13. }

使用隐式转换实现Java的异步编程到Scala的异步编程变换,该函数利用io.netty.util.concurrent.{GenericFutureListener, Future => NFuture}实现异步编程