如何在Akka-Stream中使用Source.Queue

时间:2017-08-17 21:44:47

标签: scala akka-stream

我正在试图使用来自Actor的source.queue。我在提议操作的结果中陷入了零件匹配

class MarcReaderActor(file: File, sourceQueue: SourceQueueWithComplete[Record])  extends Actor {

  val inStream = file.newInputStream
  val reader   = new MarcStreamReader(inStream)

  override def receive: Receive = {

    case Process => {
      if (reader.hasNext()) {
        val record = reader.next()
        pipe(sourceQueue.offer(record)) to self
      }
    }

    case f:Future[QueueOfferResult] => 
    }
  }
}

我不知道如何检查它是排队还是丢弃或失败

如果我写f:Future [QueueOfferResult.Enqueued]编译抱怨

1 个答案:

答案 0 :(得分:3)

由于您使用pipeTo,因此您无需在期货上进行匹配 - 未来的内容将在未来完成时发送给演员,而不是未来。这样做:

override def receive: Receive = {
  case Process => 
    if (reader.hasNext()) {
      val record = reader.next()
      pipe(sourceQueue.offer(record)) to self
    }

  case r: QueueOfferResult => 
    r match {
      case QueueOfferResult.Enqueued =>     // element has been consumed
      case QueueOfferResult.Dropped =>      // element has been ignored because of backpressure
      case QueueOfferResult.QueueClosed =>  // the queue upstream has terminated
      case QueueOfferResult.Failure(e) =>   // the queue upstream has failed with an exception
    }

  case Status.Failure(e) =>  // future has failed, e.g. because of invalid usage of `offer()`
}