Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PersistenceExample to use the latest Akka.NET version #6213

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Akka.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
6 changes: 0 additions & 6 deletions src/examples/PersistenceExample/App.config

This file was deleted.

136 changes: 71 additions & 65 deletions src/examples/PersistenceExample/AtLeastOnceDeliveryExampleActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,86 +15,89 @@ 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;
}
}
}

/// <summary>
/// AtLeastOnceDelivery will repeat sending messages, unless confirmed by deliveryId
///
/// By default, in-memory Journal is used, so this won't survive system restarts.
/// </summary>
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
Expand All @@ -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;
}
}
}
66 changes: 38 additions & 28 deletions src/examples/PersistenceExample/ExamplePersistentActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -49,7 +45,7 @@ public ExampleState(List<string> events = null)
Events = events ?? new List<string>();
}

public IEnumerable<string> Events { get; private set; }
public IEnumerable<string> Events { get; }

public ExampleState Update(Event evt)
{
Expand All @@ -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;
}
}
}
Expand Down