From 4e210662bb1d5cf8670fad74b308cd32d3f5cc15 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Wed, 26 Oct 2022 00:02:34 +0700 Subject: [PATCH] Update PersistenceExample to use the latest Akka.NET version --- src/Akka.sln | 15 ++ src/examples/PersistenceExample/App.config | 6 - .../AtLeastOnceDeliveryExampleActor.cs | 136 +++++++++--------- .../ExamplePersistentActor.cs | 66 +++++---- .../ExamplePersistentFailingActor.cs | 31 ++-- .../PersistenceExample/ExampleView.cs | 57 -------- .../PersistenceExample.csproj | 86 ++--------- src/examples/PersistenceExample/Program.cs | 35 ++--- .../Properties/AssemblyInfo.cs | 43 ------ .../SnapshotedExampleActor.cs | 47 +++--- .../PersistenceExample/ViewExampleActor.cs | 41 ------ 11 files changed, 198 insertions(+), 365 deletions(-) delete mode 100644 src/examples/PersistenceExample/App.config delete mode 100644 src/examples/PersistenceExample/ExampleView.cs delete mode 100644 src/examples/PersistenceExample/Properties/AssemblyInfo.cs delete mode 100644 src/examples/PersistenceExample/ViewExampleActor.cs diff --git a/src/Akka.sln b/src/Akka.sln index d4f3847533a..20adb3e365d 100644 --- a/src/Akka.sln +++ b/src/Akka.sln @@ -270,6 +270,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleDestination", "exampl EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleSender", "examples\Cluster\PublishSubscribe\SampleSender\SampleSender.csproj", "{A5392607-15B8-4869-BB20-FAAD4D09E08B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersistenceExample", "examples\PersistenceExample\PersistenceExample.csproj", "{4022147A-4F95-4A04-BE09-01B7952BBDD9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1261,6 +1263,18 @@ Global {A5392607-15B8-4869-BB20-FAAD4D09E08B}.Release|x64.Build.0 = Release|Any CPU {A5392607-15B8-4869-BB20-FAAD4D09E08B}.Release|x86.ActiveCfg = Release|Any CPU {A5392607-15B8-4869-BB20-FAAD4D09E08B}.Release|x86.Build.0 = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|x64.ActiveCfg = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|x64.Build.0 = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|x86.ActiveCfg = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Debug|x86.Build.0 = Debug|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|Any CPU.Build.0 = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|x64.ActiveCfg = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|x64.Build.0 = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|x86.ActiveCfg = Release|Any CPU + {4022147A-4F95-4A04-BE09-01B7952BBDD9}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1381,6 +1395,7 @@ Global {3AF9CF2D-6C20-4CFE-BC0A-CA48F60E2724} = {51C887A7-7A69-43E2-9BE2-17016E2D8476} {09CFD060-C7DC-49CC-A6C6-D3FE341A7320} = {51C887A7-7A69-43E2-9BE2-17016E2D8476} {A5392607-15B8-4869-BB20-FAAD4D09E08B} = {51C887A7-7A69-43E2-9BE2-17016E2D8476} + {4022147A-4F95-4A04-BE09-01B7952BBDD9} = {A640E39E-F45C-4AE9-AABF-7F1432D357DA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {03AD8E21-7507-4E68-A4E9-F4A7E7273164} diff --git a/src/examples/PersistenceExample/App.config b/src/examples/PersistenceExample/App.config deleted file mode 100644 index 8e15646352e..00000000000 --- a/src/examples/PersistenceExample/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/examples/PersistenceExample/AtLeastOnceDeliveryExampleActor.cs b/src/examples/PersistenceExample/AtLeastOnceDeliveryExampleActor.cs index 4d2cb8d0f9b..9a3be7c0c5f 100644 --- a/src/examples/PersistenceExample/AtLeastOnceDeliveryExampleActor.cs +++ b/src/examples/PersistenceExample/AtLeastOnceDeliveryExampleActor.cs @@ -15,74 +15,77 @@ public class Message { public Message(string data) { - this.Data = data; + Data = data; } - public string Data { get; private set; } + public string Data { get; } } public class Confirmable { public Confirmable(long deliveryId, string data) { - this.DeliveryId = deliveryId; - this.Data = data; + DeliveryId = deliveryId; + Data = data; } + public long DeliveryId { get; } - public long DeliveryId { get; private set; } - - public string Data { get; private set; } + public string Data { get; } } + public class Confirmation { public Confirmation(long deliveryId) { - this.DeliveryId = deliveryId; + DeliveryId = deliveryId; } - public long DeliveryId { get; private set; } + public long DeliveryId { get; } } + [Serializable] public class Snap { - public Snap(Akka.Persistence.AtLeastOnceDeliverySnapshot snapshot) + public Snap(AtLeastOnceDeliverySnapshot snapshot) { - this.Snapshot = snapshot; + Snapshot = snapshot; } - public Akka.Persistence.AtLeastOnceDeliverySnapshot Snapshot { get; private set; } + public AtLeastOnceDeliverySnapshot Snapshot { get; } } public class DeliveryActor : UntypedActor { - bool Confirming = true; + private bool _confirming = true; protected override void OnReceive(object message) { - if (message as string == "start") - { - Confirming = true; - } - if (message as string == "stop") - { - Confirming = false; - } - if (message is Confirmable) + switch (message) { - var msg = message as Confirmable; - if (Confirming) - { - Console.WriteLine("Confirming delivery of message id: {0} and data: {1}", msg.DeliveryId, msg.Data); - Context.Sender.Tell(new Confirmation(msg.DeliveryId)); - } - else - { - Console.WriteLine("Ignoring message id: {0} and data: {1}", msg.DeliveryId, msg.Data); - } + case string str when str == "start": + _confirming = true; + break; + + case string str when str == "stop": + _confirming = false; + break; + + case Confirmable msg: + if (_confirming) + { + Console.WriteLine("Confirming delivery of message id: {0} and data: {1}", msg.DeliveryId, msg.Data); + Context.Sender.Tell(new Confirmation(msg.DeliveryId)); + } + else + { + Console.WriteLine("Ignoring message id: {0} and data: {1}", msg.DeliveryId, msg.Data); + } + break; } } } + /// /// AtLeastOnceDelivery will repeat sending messages, unless confirmed by deliveryId /// @@ -90,11 +93,11 @@ protected override void OnReceive(object message) /// public class AtLeastOnceDeliveryExampleActor : AtLeastOnceDeliveryActor { - public ActorPath DeliveryPath { get; private set; } + private ActorPath DeliveryPath { get; } public AtLeastOnceDeliveryExampleActor(ActorPath deliveryPath) { - this.DeliveryPath = deliveryPath; + DeliveryPath = deliveryPath; } public override string PersistenceId @@ -104,51 +107,54 @@ public override string PersistenceId protected override bool ReceiveRecover(object message) { - if (message is Message) + switch (message) { - var messageData = ((Message)message).Data; - Console.WriteLine("recovered {0}",messageData); - Deliver(DeliveryPath, + case Message msg: + var messageData = msg.Data; + Console.WriteLine("recovered {0}",messageData); + Deliver(DeliveryPath, id => { Console.WriteLine("recovered delivery task: {0}, with deliveryId: {1}", messageData, id); return new Confirmable(id, messageData); }); + return true; + case Confirmation confirm: + var deliveryId = confirm.DeliveryId; + Console.WriteLine("recovered confirmation of {0}", deliveryId); + ConfirmDelivery(deliveryId); + return true; + + default: + return false; } - else if (message is Confirmation) - { - var deliveryId = ((Confirmation)message).DeliveryId; - Console.WriteLine("recovered confirmation of {0}", deliveryId); - ConfirmDelivery(deliveryId); - } - else - return false; - return true; } protected override bool ReceiveCommand(object message) { - if (message as string == "boom") - throw new Exception("Controlled devastation"); - else if (message is Message) + switch (message) { - Persist(message as Message, m => - { - Deliver(DeliveryPath, - id => - { - Console.WriteLine("sending: {0}, with deliveryId: {1}", m.Data, id); - return new Confirmable(id, m.Data); - }); - }); - } - else if (message is Confirmation) - { - Persist(message as Confirmation, m => ConfirmDelivery(m.DeliveryId)); + case string str when str == "boom": + throw new Exception("Controlled devastation"); + + case Message msg: + Persist(msg, m => + Deliver(DeliveryPath, + id => { + Console.WriteLine("sending: {0}, with deliveryId: {1}", m.Data, id); + return new Confirmable(id, m.Data); + }) + ); + return true; + + case Confirmation confirm: + Persist(confirm, m => ConfirmDelivery(m.DeliveryId)); + return true; + + default: + return false; } - else return false; - return true; } } } diff --git a/src/examples/PersistenceExample/ExamplePersistentActor.cs b/src/examples/PersistenceExample/ExamplePersistentActor.cs index e81455b7281..a367b11cd4f 100644 --- a/src/examples/PersistenceExample/ExamplePersistentActor.cs +++ b/src/examples/PersistenceExample/ExamplePersistentActor.cs @@ -19,12 +19,10 @@ public Command(string data) Data = data; } - public string Data { get; private set; } + public string Data { get; } public override string ToString() - { - return Data; - } + => Data; } public class Event @@ -34,12 +32,10 @@ public Event(string data) Data = data; } - public string Data { get; private set; } + public string Data { get; } public override string ToString() - { - return Data; - } + => Data; } public class ExampleState @@ -49,7 +45,7 @@ public ExampleState(List events = null) Events = events ?? new List(); } - public IEnumerable Events { get; private set; } + public IEnumerable Events { get; } public ExampleState Update(Event evt) { @@ -73,38 +69,52 @@ public ExamplePersistentActor() public override string PersistenceId { get { return "sample-id-1"; }} - public ExampleState State { get; set; } - public int EventsCount { get { return State.Events.Count(); } } + private ExampleState State { get; set; } - public void UpdateState(Event evt) + private int EventsCount + { + get => State.Events.Count(); + } + + private void UpdateState(Event evt) { State = State.Update(evt); } protected override bool ReceiveRecover(object message) { - ExampleState state; - if (message is Event) - UpdateState(message as Event); - else if (message is SnapshotOffer && (state = ((SnapshotOffer) message).Snapshot as ExampleState) != null) - State = state; - else return false; - return true; + switch (message) + { + case Event @event: + UpdateState(@event); + return true; + case SnapshotOffer { Snapshot: ExampleState state }: + State = state; + return true; + default: + return false; + } } protected override bool ReceiveCommand(object message) { - if (message is Command) + switch (message) { - var cmd = message as Command; - Persist(new Event(cmd.Data + "-" + EventsCount), UpdateState); + case Command cmd: + Persist(new Event(cmd.Data + "-" + EventsCount), UpdateState); + return true; + case string msg when msg == "snap": + SaveSnapshot(State); + return true; + case string msg when msg == "print": + Console.WriteLine(State); + return true; + case SaveSnapshotSuccess _: + case SaveSnapshotFailure _: + return true; + default: + return false; } - else if (message as string == "snap") - SaveSnapshot(State); - else if (message as string == "print") - Console.WriteLine(State); - else return false; - return true; } } } diff --git a/src/examples/PersistenceExample/ExamplePersistentFailingActor.cs b/src/examples/PersistenceExample/ExamplePersistentFailingActor.cs index affd0b72aaa..de1da066aa1 100644 --- a/src/examples/PersistenceExample/ExamplePersistentFailingActor.cs +++ b/src/examples/PersistenceExample/ExamplePersistentFailingActor.cs @@ -20,26 +20,33 @@ public ExamplePersistentFailingActor() } public override string PersistenceId { get { return "sample-id-2"; } } - public LinkedList Received { get; private set; } + public LinkedList Received { get; } protected override bool ReceiveRecover(object message) { - if (message is string) - Received.AddFirst(message.ToString()); - else return false; + if (!(message is string str)) + return false; + + Received.AddFirst(str); return true; + } protected override bool ReceiveCommand(object message) { - if (message as string == "print") - Console.WriteLine("Received: " + string.Join(";, ", Enumerable.Reverse(Received))); - else if (message as string == "boom") - throw new Exception("controlled demolition"); - else if (message is string) - Persist(message.ToString(), s => Received.AddFirst(s)); - else return false; - return true; + switch (message) + { + case string str when str == "print": + Console.WriteLine("Received: " + string.Join(";, ", Enumerable.Reverse(Received))); + return true; + case string str when str == "boom": + throw new Exception("controlled demolition"); + case string str: + Persist(str, s => Received.AddFirst(s)); + return true; + default: + return false; + } } } } diff --git a/src/examples/PersistenceExample/ExampleView.cs b/src/examples/PersistenceExample/ExampleView.cs deleted file mode 100644 index 8b07b35729e..00000000000 --- a/src/examples/PersistenceExample/ExampleView.cs +++ /dev/null @@ -1,57 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (C) 2009-2022 Lightbend Inc. -// Copyright (C) 2013-2022 .NET Foundation -// -//----------------------------------------------------------------------- - -using System; -using Akka.Persistence; - -namespace PersistenceExample -{ - public class ExampleView : PersistentView - { - private int _numReplicated = 0; - - public override string PersistenceId { get { return "sample-id-4"; } } - public override string ViewId { get { return "sample-view-id-4"; } } - - protected override bool Receive(object message) - { - if (message as string == "snap") - { - Console.WriteLine("View saving snapshot"); - SaveSnapshot(_numReplicated); - } - else if (message is SnapshotOffer) - { - var offer = (SnapshotOffer) message; - _numReplicated = Convert.ToInt32(offer.Snapshot); - Console.WriteLine("View received snapshot offer {0} (metadata = {1})", _numReplicated, offer.Metadata); - } - else if (IsPersistent) - { - _numReplicated++; - Console.WriteLine("View replayed event {0} (num replicated = {1})", message, _numReplicated); - } - else if (message is SaveSnapshotSuccess) - { - var fail = (SaveSnapshotSuccess) message; - Console.WriteLine("View snapshot success (metadata = {0})", fail.Metadata); - } - else if (message is SaveSnapshotFailure) - { - var fail = (SaveSnapshotFailure) message; - Console.WriteLine("View snapshot failure (metadata = {0}), caused by {1}", fail.Metadata, fail.Cause); - } - else - { - Console.WriteLine("View received other message " + message); - } - - return true; - } - } -} - diff --git a/src/examples/PersistenceExample/PersistenceExample.csproj b/src/examples/PersistenceExample/PersistenceExample.csproj index c4892bc2d08..7f0b8c5c7a2 100644 --- a/src/examples/PersistenceExample/PersistenceExample.csproj +++ b/src/examples/PersistenceExample/PersistenceExample.csproj @@ -1,72 +1,14 @@ - - - - - Debug - AnyCPU - {4022147A-4F95-4A04-BE09-01B7952BBDD9} - Exe - Properties - PersistenceExample - PersistenceExample - v4.5 - 512 - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - {FCA84DEA-C118-424B-9EB8-34375DFEF18A} - Akka.Persistence - - - {5DEDDF90-37F0-48D3-A0B0-A5CBD8A7E377} - Akka - - - - - \ No newline at end of file + + + + + Exe + $(NetCoreTestVersion) + false + + + + + + + diff --git a/src/examples/PersistenceExample/Program.cs b/src/examples/PersistenceExample/Program.cs index 57d404aa7ba..22ae9d88b67 100644 --- a/src/examples/PersistenceExample/Program.cs +++ b/src/examples/PersistenceExample/Program.cs @@ -12,27 +12,24 @@ namespace PersistenceExample { - class Program + public static class Program { - static void Main(string[] args) + public static void Main(string[] args) { - - - using (var system = ActorSystem.Create("example")) - { - //SqlServerPersistence.Init(system); - BasicUsage(system); + using var system = ActorSystem.Create("example"); + + //SqlServerPersistence.Init(system); + //BasicUsage(system); - //FailingActorExample(system); + //FailingActorExample(system); - //SnapshotedActor(system); + SnapshotedActor(system); - //ViewExample(system); + //ViewExample(system); - AtLeastOnceDelivery(system); + AtLeastOnceDelivery(system); - Console.ReadLine(); - } + Console.ReadLine(); } private static void AtLeastOnceDelivery(ActorSystem system) @@ -63,16 +60,6 @@ private static void AtLeastOnceDelivery(ActorSystem system) } - private static void ViewExample(ActorSystem system) - { - Console.WriteLine("\n--- PERSISTENT VIEW EXAMPLE ---\n"); - var pref = system.ActorOf(Props.Create()); - var view = system.ActorOf(Props.Create()); - - system.Scheduler.ScheduleTellRepeatedly(TimeSpan.Zero, TimeSpan.FromSeconds(2), pref, "scheduled", ActorRefs.NoSender); - system.Scheduler.ScheduleTellRepeatedly(TimeSpan.Zero, TimeSpan.FromSeconds(5), view, "snap", ActorRefs.NoSender); - } - private static void SnapshotedActor(ActorSystem system) { Console.WriteLine("\n--- SNAPSHOTED ACTOR EXAMPLE ---\n"); diff --git a/src/examples/PersistenceExample/Properties/AssemblyInfo.cs b/src/examples/PersistenceExample/Properties/AssemblyInfo.cs deleted file mode 100644 index 72c58dfa2ca..00000000000 --- a/src/examples/PersistenceExample/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,43 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (C) 2009-2022 Lightbend Inc. -// Copyright (C) 2013-2022 .NET Foundation -// -//----------------------------------------------------------------------- - -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("PersistenceExample")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("PersistenceExample")] -[assembly: AssemblyCopyright("Copyright © 2014")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("7f60284f-0bdb-4a6a-8901-394074584f51")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] - diff --git a/src/examples/PersistenceExample/SnapshotedExampleActor.cs b/src/examples/PersistenceExample/SnapshotedExampleActor.cs index 36e8dfe5e4e..d0f47d4e1e7 100644 --- a/src/examples/PersistenceExample/SnapshotedExampleActor.cs +++ b/src/examples/PersistenceExample/SnapshotedExampleActor.cs @@ -23,29 +23,42 @@ public SnapshotedExampleActor() protected override bool ReceiveRecover(object message) { - if (message is SnapshotOffer) + switch (message) { - var s = ((SnapshotOffer) message).Snapshot as ExampleState; - Console.WriteLine("Offered state (from snapshot): " + s); - State = s; + case SnapshotOffer offer: + var s = (ExampleState) offer.Snapshot; + Console.WriteLine("Offered state (from snapshot): " + s); + State = s; + return true; + + case string _: + State = State.Update(new Event(message.ToString())); + return true; + + default: + return false; } - else if (message is string) - State = State.Update(new Event(message.ToString())); - else return false; - return true; } protected override bool ReceiveCommand(object message) { - if (message as string == "print") - Console.WriteLine("Current actor's state: " + State); - else if (message as string == "snap") - SaveSnapshot(State); - else if (message is SaveSnapshotFailure || message is SaveSnapshotSuccess) { } - else if (message is string) - Persist(message.ToString(), evt => State = State.Update(new Event(evt))); - else return false; - return true; + switch (message) + { + case string str when str == "print": + Console.WriteLine("Current actor's state: " + State); + return true; + case string str when str == "snap": + SaveSnapshot(State); + return true; + case string str: + Persist(str, evt => State = State.Update(new Event(evt))); + return true; + case SaveSnapshotFailure _: + case SaveSnapshotSuccess _: + return true; + default: + return false; + } } } } diff --git a/src/examples/PersistenceExample/ViewExampleActor.cs b/src/examples/PersistenceExample/ViewExampleActor.cs deleted file mode 100644 index 29591e7b1e5..00000000000 --- a/src/examples/PersistenceExample/ViewExampleActor.cs +++ /dev/null @@ -1,41 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (C) 2009-2022 Lightbend Inc. -// Copyright (C) 2013-2022 .NET Foundation -// -//----------------------------------------------------------------------- - -using System; -using Akka.Persistence; - -namespace PersistenceExample -{ - public class ViewExampleActor : PersistentActor - { - private int _count = 1; - - public ViewExampleActor() - { - } - - public override string PersistenceId { get { return "sample-id-4"; } } - - protected override bool ReceiveRecover(object message) - { - _count++; - return true; - } - - protected override bool ReceiveCommand(object message) - { - if (message is string) - { - Console.WriteLine("PersistentActor received {0} (nr = {1})", message, _count); - Persist(message.ToString() + _count, s => _count++); - } - - return false; - } - } -} -