-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working for H2 and event source, snapshot, durable state
- Loading branch information
1 parent
12456d6
commit c003f78
Showing
4 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
native-image-tests/src/main/scala/com/lightbend/DurableStateTester.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2009-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package com.lightbend | ||
|
||
import akka.actor.typed.ActorRef | ||
import akka.actor.typed.Behavior | ||
import akka.actor.typed.scaladsl.Behaviors | ||
import akka.persistence.typed.PersistenceId | ||
import akka.persistence.typed.state.scaladsl.DurableStateBehavior | ||
import akka.persistence.typed.state.scaladsl.Effect | ||
import akka.serialization.jackson.JsonSerializable | ||
import com.fasterxml.jackson.annotation.JsonCreator | ||
|
||
import scala.concurrent.duration.DurationInt | ||
|
||
object DurableStateCounter { | ||
sealed trait Command extends JsonSerializable | ||
final case class Increase(amount: Int, replyTo: ActorRef[Increased]) extends Command | ||
|
||
// FIXME why doesn't @JsonCreator work as usual? is it something missing from the jackson feature? | ||
final case class GetState @JsonCreator() (replyTo: ActorRef[State]) extends Command | ||
|
||
final case class Increased @JsonCreator() (newValue: Int) extends JsonSerializable | ||
|
||
final case class State @JsonCreator() (value: Int) extends JsonSerializable | ||
def apply(id: String): Behavior[Command] = | ||
DurableStateBehavior[Command, State]( | ||
PersistenceId("DSCounter", id), | ||
State(0), | ||
{ | ||
case (state, Increase(amount, replyTo)) => | ||
Effect.persist(State(state.value + amount)).thenReply(replyTo)(newState => Increased(newState.value)) | ||
case (state, GetState(replyTo)) => | ||
Effect.reply(replyTo)(state) | ||
}) | ||
} | ||
|
||
object DurableStateTester { | ||
|
||
def apply(whenDone: ActorRef[String]): Behavior[AnyRef] = Behaviors.setup { context => | ||
Behaviors.withTimers { timers => | ||
timers.startSingleTimer("Timeout", 10.seconds) | ||
|
||
var durableActor = context.spawn(DurableStateCounter("one"), "DurableOne") | ||
context.watchWith(durableActor, "DurableOneStopped") | ||
|
||
def messageOrTimeout(step: String)(partial: PartialFunction[AnyRef, Behavior[AnyRef]]): Behavior[AnyRef] = { | ||
context.log.info("On {}", step) | ||
Behaviors.receiveMessage(message => | ||
partial.orElse[AnyRef, Behavior[AnyRef]] { | ||
case "Timeout" => | ||
context.log.error(s"Durable state checks timed out in {}", step) | ||
System.exit(1) | ||
Behaviors.same | ||
|
||
case other => | ||
context.log.warn("Unexpected message in {}: {}", step, other) | ||
Behaviors.same | ||
}(message)) | ||
} | ||
|
||
durableActor ! DurableStateCounter.Increase(1, context.self) | ||
|
||
def step1() = messageOrTimeout("step1") { case DurableStateCounter.Increased(1) => | ||
// write works | ||
context.stop(durableActor) | ||
step2() | ||
} | ||
|
||
def step2() = messageOrTimeout("step2") { case "DurableOneStopped" => | ||
durableActor = context.spawn(DurableStateCounter("one"), "DurableOneIncarnation2") | ||
durableActor ! DurableStateCounter.GetState(context.self) | ||
step3() | ||
} | ||
|
||
def step3() = messageOrTimeout("step3") { case DurableStateCounter.State(1) => | ||
whenDone ! "Durable State works" | ||
Behaviors.stopped | ||
} | ||
|
||
step1() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters