ޱ࠲ؒసૹͷυϝΠ ϯαʔϏεɻ w #BOL"DDPVOUؒͰ͓ ۚΛసૹ͢Δɻ w 1FSTJTUFODF'4. ෦ঢ়ଶ͕ӬଓԽՄೳ ͳ༗ݶεςʔτϚγ ϯɻൃੜ͢ΔυϝΠϯ ΠϕϯτΛӬଓԽ͢ Δɻ class TransferDomainService(id: UUID) extends PersistentFSM[State, Data, Event] {
override def persistenceId: String = id.toString
override def domainEventClassTag: ClassTag[Event] = classTag[Event]
override def applyEvent(domainEvent: Event, currentData: Data): Data =
domainEvent match {
case Transferred(id, money, from, to, ref) => TransferData(id, money, from, to, ref)
case Transferring(id, money, from, to, ref) => TransferData(id, money, from, to, ref)
}
startWith(Stopped, Empty)
when(Stopped) {
case Event(Transfer(requestId, money, from, to), _) =>
context.system.eventStream.subscribe(self, classOf[Decreased])
val ev = Transferring(UUID.randomUUID(), money, from, to, sender())
goto(Started) applying ev andThen {
case d: TransferData =>
from ! Decrease(UUID.randomUUID(), money)
sender() ! CommandSucceeded(UUID.randomUUID(), requestId)
context.system.eventStream.publish(ev)
}
}
when(Started) {
case Event(Decreased(_, _), _) =>
goto(FromDecreased) andThen {
case d@TransferData(_, money, from, to) =>
context.system.eventStream.unsubscribe(self, classOf[Decreased])
context.system.eventStream.subscribe(self, classOf[Increased])
to ! Increase(UUID.randomUUID(), money)
}
case ev@Event(CommandSucceeded(_, _),_) =>
stay
}
when(FromDecreased) {
case Event(Increased(_, _), TransferData(_, money, from, to, ref)) =>
goto(Stopped) applying Transferred(UUID.randomUUID(), money, from, to, ref) andThen {
case d: TransferData =>
context.system.eventStream.unsubscribe(self, classOf[Increased])
context.system.eventStream.publish(Transferred(UUID.randomUUID(), money, from, to))
}
case ev@Event(CommandSucceeded(_, _),_) =>
stay
}
initialize()
}