Akka超时异常,但实际上已发送邮件

问题描述 投票:1回答:1

我正在使用具有以下技术的Scala 2.13堆栈:

  • 玩!框架2.8
  • akka输入2.6.3
  • alpakka kafka 2.0.3

[Akka-stream作业从Kafka读取事件,要求演员计算一些东西,并根据给定的响应,将新事件返回给Kafka。

问题是使用询问模式发送的消息似乎被QuestionActor占用(如下)仅当其邮箱收集了至少两则消息,而收到的每条消息中只有一则消息

奇怪的行为是:

t1

ref ? Question("tr1", 1, None, actorRef)
> AskTimeoutException(tr1)

t2

ref ? Question("tr2", 1, None, actorRef)
> [INFO] - Question request for tr1-1. Processing.
> AskTimeoutException(tr2)

t3

ref ? Question("tr3", 1, None, actorRef)
> [INFO] - Question request for tr2-1. Processing.
> AskTimeoutException(tr3)

然后我试图理解为什么观察到这种行为以及我做错了什么

Akka流Kafka管道是:

Consumer
  .plainSource(consumerSettings, subscription)
  .map(DeserializeEvents.fromService)
  .filter(_.eventType == classOf[Item].getName)
  .via(askFlowExplicit)
  .withAttributes(ActorAttributes.supervisionStrategy(decider()))
  .map(
    response =>
      new ProducerRecord[String, OutputItem](
        topics,
        OutputItem(response.getClass.getName, response)
      )
  )
  .log("Kafka Pipeline")
  .runWith(Producer.plainSink(producerSettings))

决策者是一种监督策略,根据SerialisationTimeout例外情况恢复工作; askFlowExplicit向外部演员声明了一个询问请求,并且-在此-我碰到了我的问题。

val askFlowExplicit =
  ActorFlow.ask[OutputItem, Question, Answer](askTarget) {
    case (envelope, replyTo) =>
      val item = Serdes.deserialize[Item](envelope.payload)
      Question(item.trID, item.id, item.user, replyTo)
  }

[管道在Play上启动!应用程序引导程序

@Singleton
class ApplicationStart @Inject()(
    configuration: Configuration,
    questionActor: ActorRef[QuestionActor.Question]
) {
  private implicit val logger = Logger.apply(getClass)
  implicit val mat            = context
  AlpakkaPipeline.run(configuration, questionActor)
}

actor是属于同一个actor系统的简单类型的actor,并且-现在-它仅将来自流的请求转发到另一个服务。

class QuestionActor(
    configuration: Configuration,
    context: ActorContext[Question],
    itemService: ItemService
) extends AbstractBehavior[Question](context) {
  import QuestionActor._

  implicit val ec: ExecutionContextExecutor = context.executionContext
  private implicit val timeout: Timeout = ...

  override def onMessage(msg: Question): Behavior[Question] = Behaviors.receive[Question] {
    case (context, Question(trID, id, user, sender)) =>
      log.info(s"Question request for ${msg.trID}-${msg.id}. Processing.")
        itemService
          .action(id, user)
          .onComplete {
            case Success(result) if result.isEmpty =>
              log.info("Action executed")
              msg.replyTo ! NothingHappened(trID, id)
            case Failure(e) =>
              log.error("Action failed.", e)
              msg.replyTo ! FailedAction(trID, id, user, e.getMessage)
          }
      Behaviors.same
  }
}

object QuestionActor {
  final case class Question(
      trID: String,
      id: Int,
      user: Option[UUID],
      replyTo: ActorRef[Answer]
  )

  def apply(itemService: ItemService, configuration: Configuration): Behavior[Question] =
    Behaviors.setup { context =>
      context.setLoggerName(classOf[QuestionActor])
      implicit val log: Logger = context.log
      new QuestionActor(configuration, context)
    }
}

它是使用运行时DI和Play构建的!

class BootstrapModule(environment: Environment, configuration: Configuration)
    extends AbstractModule
    with AkkaGuiceSupport {

  override def configure(): Unit = {
    bind(new TypeLiteral[ActorRef[CloneWithSender]]() {})
      .toProvider(classOf[QuestionActorProvider])
      .asEagerSingleton()
    bind(classOf[ApplicationStart]).asEagerSingleton()
  }
}

private class Question @Inject()(
    actorSystem: ActorSystem,
    itemService: ItemService,
    configuration: Configuration
) extends Provider[ActorRef[Question]] {
  def get(): ActorRef[Question] = {
    val behavior = QuestionActor(itemService, configuration)
    actorSystem.spawn(behavior, "question-actor")
  }
}

我尝试了什么

  • 将调度程序更改为QuestionActor
  • 将邮箱更改为QuestionActor
  • QuestionActor内部运行管道
  • 通过从actor构造函数发送同一消息(到self),观察到相同的行为:另一条消息将触发actor消耗前者,要求后者超时。

我没做什么

  • 将调度程序更改为Akka流管道

在我看来,这现在是一个线程问题,但是我不知道从这里去哪里。任何帮助都非常感谢。预先谢谢你。

scala playframework akka akka-stream alpakka
1个回答
0
投票

问题是您要合并提供AbstractBehavioronMessage,然后在其中定义新的Behaviors.receive[Question]行为。您必须使用其中之一。

如下删除Behaviors.receive

  override def onMessage(msg: Question): Behavior[Question] = {
      log.info(s"Question request for ${msg.trID}-${msg.id}. Processing.")
        itemService
          .action(id, user)
          .onComplete {
            case Success(result) if result.isEmpty =>
              log.info("Action executed")
              msg.replyTo ! NothingHappened(trID, id)
            case Failure(e) =>
              log.error("Action failed.", e)
              msg.replyTo ! FailedAction(trID, id, user, e.getMessage)
          }
      Behaviors.same
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.