我一直在努力学习Akka演员,我真的需要您的帮助!
因此,我的目标基本上是写一个简单的拍卖剂。实际上,Akka有一个示例!问题是,我不知道如何运行它。现在,我花了3天的时间在寻求您的帮助之前试图让它上班,但我完全丢失了它。.:/
o,我开始首先了解打字的演员以及他们的工作方式。我设法通过创建一个我在Internet上找到的简单演员系统来实际打印在屏幕上的
//Entry point inside main(args <...>)
val orderProcessor: ActorSystem[OrderProcessor.Order] = ActorSystem(OrderProcessor(), "main")
//create a new order
orderProcessor ! Order(0, "Bananas")
//<..printing something inside the actor when receiving this message>
东西拍卖示例使用 EventsourcedBehavior ,因此我得出结论,下一步是在Akka中了解事件采购(希望到目前为止,这不会混淆,或者希望我在正确的道路上)转到了Akka的活动采购的正式文档,我以他们的例子为例:
object MyPersistentBehavior {
sealed trait Command
final case class Add(data: String) extends Command
case object Clear extends Command
sealed trait Event
final case class Added(data: String) extends Event
case object Cleared extends Event
final case class State(history: List[String] = Nil)
val eventHandler: (State, Event) => State = { (state, event) =>
event match {
case Added(data) => state.copy((data :: state.history).take(5))
case Cleared => State(Nil)
}
}
val commandHandler: (State, Command) => Effect[Event, State] = { (state, command) =>
command match {
case Add(data) => Effect.persist(Added(data))
case Clear => Effect.persist(Cleared)
}
}
def apply(id: String): Behavior[Command] =
EventSourcedBehavior[Command, Event, State](
persistenceId = PersistenceId.ofUniqueId(id),
emptyState = State(Nil),
commandHandler = commandHandler,
eventHandler = eventHandler)
}
现在很棒!非常简单。
问题是,我不明白入口点在哪里?
就像 orderprocessor (我显示的第一个示例)显然只是为了创建一个新的Actorsystem,这就是这样,但是我找不到有关此示例的任何信息。我尝试了GitHub的许多不同项目,但它们都不是很简单,无法理解。公平地说,他们中的大多数人都有测试,但是测试并没有真正帮助我。
请任何帮助,任何提示都会非常感谢,我真的很挣扎!
爱yall!&lt; 3
I've been struggling to learn akka actors and I REALLY need your help guys!
So my goal is basically to write a simple auction agent. And AKKA actually has an example on how to do it https://doc.akka.io/docs/akka/current/typed/replicated-eventsourcing-auction.html! The problem is, I have no idea how to run it. Now I've spent like 3 days on trying to get it to work before asking for your help but I've completely lost it..:/
So, I started to learn about typed actors first and how they work. I've managed to actually print something on the screen by creating a simple actor system like this that I've found on the internet where I have a simple order actor (typed) and on its apply method I can print the incoming order:
//Entry point inside main(args <...>)
val orderProcessor: ActorSystem[OrderProcessor.Order] = ActorSystem(OrderProcessor(), "main")
//create a new order
orderProcessor ! Order(0, "Bananas")
//<..printing something inside the actor when receiving this message>
Now the auction example uses EventSourcedBehavior so I came to the conclusion that next step is to learn about event sourcing in Akka (hopefully this isn't confusing so far or hopefully I'm on the right path) So I went to the official documentation of Akka's event sourcing https://doc.akka.io/docs/akka/current/typed/persistence.html#module-info and I took their example:
object MyPersistentBehavior {
sealed trait Command
final case class Add(data: String) extends Command
case object Clear extends Command
sealed trait Event
final case class Added(data: String) extends Event
case object Cleared extends Event
final case class State(history: List[String] = Nil)
val eventHandler: (State, Event) => State = { (state, event) =>
event match {
case Added(data) => state.copy((data :: state.history).take(5))
case Cleared => State(Nil)
}
}
val commandHandler: (State, Command) => Effect[Event, State] = { (state, command) =>
command match {
case Add(data) => Effect.persist(Added(data))
case Clear => Effect.persist(Cleared)
}
}
def apply(id: String): Behavior[Command] =
EventSourcedBehavior[Command, Event, State](
persistenceId = PersistenceId.ofUniqueId(id),
emptyState = State(Nil),
commandHandler = commandHandler,
eventHandler = eventHandler)
}
Now that is great! Very simple and concise.
The problem is, I don't understand where the entry point is?
Like for orderProcessor (first example that I've shown) it is obviously just to create a new ActorSystem and thats it, but I can't find any information on this example. I've tried soooo many different projects from github and none of them very simple enough for me to understand. To be fair most of them had tests, but tests didn't really help me much.
Please, any help, any tips would be SOOO much appreciated, I'm really struggling guys!
Love Yall !<3
发布评论
评论(2)
您必须获得
actorRef [messangeType]
才能向其发送messageType
。现有演员具有
上下文
(以行为
定义为参数),您可以创建一个孩子:但是您也可以从
actorsystem < /code>(然后系统直接是父),
因此,在您的情况下,可能是:
或者您可以
context.spontext.spawn
itcistion> cratedy [orderprocessor.order]
为您的ActorSystem
定义,然后您将通过ActorRef
通过System
将命令发送到持久性演员。You have to obtain
ActorRef[MessangeType]
to be able to sendMessageType
to it.Existing actor has
context
(passed as an argument withBehavior
definition) where you can create a child like:but you can also create it top-level from
ActorSystem
(then the system is the parent directly)So in your case it could be something like:
alternatively you can
context.spawn
it insideBehavior[OrderProcessor.Order]
defined for yourActorSystem
, then you'll talk toactorRef
throughsystem
which would send commands to persistent actor.我了解您的油漆,不幸的是,Akka文档仅提供摘要作为文档中的样本,并且大多数时候都不完整且难以理解。
您可以在他们的github完整中找到示例我想您可以以这种方式更好地理解事物。
如果您想与Akka一起查看完整的概念验证验证证明,我有一个博客,您可以找到在这里。
I understand your paint, unfortunately Akka Documentation give only Snippets as samples in documentation and they are most of the time not complete and hard to understand.
You can find in their Github complete examples I guess you can understands things better this way.
If you want to see a full fledged Proof of Concept application with Akka, I have a blog about it which you can find here.