Skip to content

Instantly share code, notes, and snippets.

@NeoDarque
Created June 6, 2015 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeoDarque/e955aea9de85512d9b54 to your computer and use it in GitHub Desktop.
Save NeoDarque/e955aea9de85512d9b54 to your computer and use it in GitHub Desktop.
Message missing in PreRestart?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Routing;
using AkkaLab.Messages;
namespace AkkaLab.Publisher {
class Program {
static void Main(string[] args) {
using(var system = ActorSystem.Create("my-system")) {
var props = Props.Create<SubscriberActor>().WithRouter(new Akka.Routing.ConsistentHashingPool(5));
var subscribers = system.ActorOf(props, "subscribers");
Console.WriteLine("Enter a number please...");
while(true) {
int id = 0;
var text = Console.ReadLine();
if(String.Equals(text, "quit", StringComparison.CurrentCultureIgnoreCase)) {
return;
} else if(Int32.TryParse(text, out id)) {
var payload = new PayLoad() {Id = id, Content = text};
var msg = new ConsistentHashableEnvelope(payload, payload.Id);
subscribers.Tell(msg);
} else {
var payload = new PayLoad() { Id = -1, Content = text };
var msg = new ConsistentHashableEnvelope(payload, payload.Id);
subscribers.Tell(msg);
}
}
}
}
}
public class SubscriberActor : UntypedActor {
private Guid Guid { get; set; }
public SubscriberActor() {
this.Guid = Guid.NewGuid();
}
protected override void OnReceive(object message) {
if(message is PayLoad) {
var payload = message as PayLoad;
Console.WriteLine("({2}) Received content: {0} with Id: {1}", payload.Content, payload.Id, this.Guid.ToString());
if(payload.Id < 0)
throw new Exception("Boom!");
} else {
Console.WriteLine("Received unknown message");
}
}
protected override void PreRestart(Exception reason, object message) {
base.PreRestart(reason, message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment