我尝试了一个使用MongoDb支持的akka持久化的"hello-world“示例,使用这个开源的https://github.com/scullxbones/akka-persistence-mongo/tree/master/rxmongo/src。下面是我的密码。但是,当我运行应用程序时,我得到了ask超时:
akka.pattern.AskTimeoutException:要求在2000年后在[Actorakka://example/user/sampleActor#1876558089]上超时。发送"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 (?)不同,什么都没有发生,数据库没有创建,也没有持久的命令。
谢谢!
发布于 2017-01-11 08:15:57
在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
}
}https://stackoverflow.com/questions/41560107
复制相似问题