-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
2 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
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,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Interfaced; | ||
using Akka.Interfaced.Persistence; | ||
using Akka.Persistence; | ||
|
||
namespace Manual | ||
{ | ||
public class DemoPersistentActor | ||
{ | ||
private ActorSystem _system; | ||
|
||
public DemoPersistentActor(ActorSystem system, string[] args) | ||
{ | ||
_system = system; | ||
} | ||
|
||
public class GreetEvent | ||
{ | ||
public string Name; | ||
} | ||
|
||
public class GreeterState | ||
{ | ||
public int GreetCount; | ||
public int TotalNameLength; | ||
|
||
public void OnGreet(GreetEvent e) | ||
{ | ||
GreetCount += 1; | ||
TotalNameLength += e.Name.Length; | ||
} | ||
} | ||
|
||
public class PersistentGreetingActor : InterfacedPersistentActor, IGreeter | ||
{ | ||
private GreeterState _state = new GreeterState(); | ||
|
||
public override string PersistenceId { get; } | ||
|
||
public PersistentGreetingActor(string id) | ||
{ | ||
PersistenceId = id; | ||
} | ||
|
||
[MessageHandler] | ||
private void OnRecover(SnapshotOffer snapshot) | ||
{ | ||
_state = (GreeterState)snapshot.Snapshot; | ||
} | ||
|
||
[MessageHandler] | ||
private void OnRecover(GreetEvent e) | ||
{ | ||
_state.OnGreet(e); | ||
} | ||
|
||
async Task<string> IGreeter.Greet(string name) | ||
{ | ||
var e = new GreetEvent { Name = name }; | ||
await PersistTaskAsync(e); | ||
_state.OnGreet(e); | ||
return $"Hello {name}!"; | ||
} | ||
|
||
Task<int> IGreeter.GetCount() | ||
{ | ||
return Task.FromResult(_state.GreetCount); | ||
} | ||
} | ||
|
||
private async Task DemoRun() | ||
{ | ||
// create actor, change state of it, and destroy it. | ||
var a = _system.ActorOf(Props.Create(() => new PersistentGreetingActor("greeter1"))); | ||
var g = new GreeterRef(a); | ||
await g.Greet("World"); | ||
await g.Greet("Actor"); | ||
Console.WriteLine("1st: " + await g.GetCount()); | ||
await a.GracefulStop(TimeSpan.FromMinutes(1), InterfacedPoisonPill.Instance); | ||
|
||
// create actor, and check saved state. | ||
var a2 = _system.ActorOf(Props.Create(() => new PersistentGreetingActor("greeter1"))); | ||
var g2 = new GreeterRef(a2); | ||
Console.WriteLine("2nd: " + await g2.GetCount()); | ||
await g2.Greet("More"); | ||
Console.WriteLine("3rd: " + await g2.GetCount()); | ||
await a2.GracefulStop(TimeSpan.FromMinutes(1), InterfacedPoisonPill.Instance); | ||
} | ||
} | ||
} |
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
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