演员在akka-persistence中要求超时

时间:2017-01-10 02:18:47

标签: mongodb akka-persistence

我正在尝试一个'#34; hello-world" MongoDb支持的akka​​-persistence示例,使用此开源https://github.com/scullxbones/akka-persistence-mongo/tree/master/rxmongo/src。以下是我的代码。但是,当我运行应用程序时,我得到了超时问题:

akka.pattern.AskTimeoutException:在[2000 ms]之后询问[Actor [akka:// example / user / sampleActor#1876558089]]的超时时间。发件人[null]发送了类型" actors.Command"的消息。

import akka.actor.{ActorSystem, Props}
import akka.pattern.ask
import akka.persistence.{PersistentActor, RecoveryCompleted}
import akka.util.Timeout

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

object Main extends App {
  implicit val timeout = Timeout(2 seconds)
  val system = ActorSystem("example")

  var actor = system.actorOf(SampleActor.props(), "sampleActor")
  Await.result(actor ? Command("first"), Duration.Inf)
  Await.result(actor ? Command("second"), Duration.Inf)

  system.stop(actor)
  system.terminate()
}

case class Command(value: String)

case class Event(value: String)

case class SampleState(counter: Int, lastValue: Option[String])

class SampleActor extends PersistentActor {
  override def persistenceId = "id-1"

  var state = SampleState(0, None)

  def updateState(event: Event): Unit = {
    state = state.copy(counter = state.counter + 1, lastValue = Some(event.value))
  }

  override val receiveCommand: Receive = {
    case Command(value) =>
      persist(Event(value))(updateState)
  }

  override def receiveRecover: Receive = {
    case event: Event =>
      updateState(event)
    case RecoveryCompleted =>
      println("Recovery completed")
  }
}

object SampleActor {
  def props(): Props = Props(new SampleActor())
}

这是我的application.conf:

contrib {
    persistence {
      mongodb {
        mongo {
          mongouri = "mongodb://localhost:27017/akka-persistence"
          driver = "akka.contrib.persistence.mongodb.RxMongoPersistenceExtension"
        }
        rxmongo {
          failover {
            initialDelay = 750ms
            retries = 10
            growth = con
            factor = 1.5
          }
        }
      }
    }

  }

如果我使用tell(!)而不是ask(?),则没有任何反应,数据库不会创建,也不会保留任何命令。

谢谢!

1 个答案:

答案 0 :(得分:0)

在application.conf中,您应该分配日志和快照插件:

akka.persistence.journal.plugin = "akka-contrib-mongodb-persistence-journal"
akka.persistence.snapshot-store.plugin = "akka-contrib-mongodb-persistence-snapshot"

您应该回复发件人,以便收到:

override val receiveCommand: Receive = {
   case Command(value) =>
     persist(Event(value)) { persistedEvent =>
        updateState(persistedEvent)
        sender ! SomeResponse
     }
}