Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Last active December 30, 2015 13:54
Show Gist options
  • Select an option

  • Save Horusiath/08888fe69e4fe9e60525 to your computer and use it in GitHub Desktop.

Select an option

Save Horusiath/08888fe69e4fe9e60525 to your computer and use it in GitHub Desktop.
Akka.Persistence.FSharp aggregate/view cooperation
open System
open Akka
open Akka.Actor
open Akka.FSharp
open Akka.Persistence.FSharp
let system = System.create "persistence-sys" <| Configuration.parse """
akka {
persistence {
journal {
plugin = "akka.persistence.journal.sqlite"
sqlite {
class = "Akka.Persistence.Sqlite.Journal.SqliteJournal, Akka.Persistence.Sqlite"
connection-string = "Data Source=.\\store.db;Version=3;"
auto-initialize = true
}
}
snapshot-store {
plugin = "akka.persistence.snapshot-store.sqlite"
sqlite {
class = "Akka.Persistence.Sqlite.Snapshot.SqliteSnapshotStore, Akka.Persistence.Sqlite"
connection-string = "Data Source=.\\store.db;Version=3;"
auto-initialize = true
}
}
}
}
"""
let j = Akka.Persistence.Persistence.Instance.Apply(system).JournalFor(null)
let update state e = e::state
let apply _ = update
let exec (mailbox: Eventsourced<_,_,_>) state cmd =
match cmd with
| "print" -> printf "State is: %A\n" state
| s -> mailbox.PersistEvent (update state) [s]
let pref =
spawnPersist system "p1" {
state = []
apply = apply
exec = exec
} []
pref <! "a"
pref <! "b"
pref <! "c"
let viewApply (mailbox: View<_,_>) state = function
| "print" ->
mailbox.Sender() <! state
state
| e -> state + e
let vref =
spawnView system "v1" "p1" {
state = ""
apply = viewApply
} []
async {
let! resp = vref <? "print"
printfn "View is: %s" resp
} |> Async.RunSynchronously
Console.ReadLine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment