简体   繁体   English

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

[英]How to work with Source.Queue in Akka-Stream

I am toying around trying to use a source.queue from an Actor. 我正在尝试使用来自Actor的source.queue。 I am stuck in parttern match the result of an offer operation 我陷入了要约操作的结果

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] => 
    }
  }
}

I don't know how to check if it was Enqueued or Dropped or Failure 我不知道如何检查它是否入队,掉线或失败

if i write f:Future[QueueOfferResult.Enqueued] the compile complain 如果我写f:Future [QueueOfferResult.Enqueued]编译会抱怨

Since you use pipeTo , you do no need to match on futures - the contents of the future will be sent to the actor when this future is completed, not the future itself. 由于您使用pipeTo ,因此您无需匹配期货-将来的内容将在该将来完成时发送给角色,而不是将来本身。 Do this: 做这个:

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()`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM