Created
May 15, 2015 19:43
-
-
Save annymsMthd/d95d0a42e38da4aabfce to your computer and use it in GitHub Desktop.
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
//----------------------------------------------------------------------- | |
// <copyright file="Program.cs" company="Akka.NET Project"> | |
// Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> | |
// Copyright (C) 2013-2015 Akka.NET project <https://github.com/akkadotnet/akka.net> | |
// </copyright> | |
//----------------------------------------------------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Dynamic; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Akka.Actor; | |
using Akka.Configuration; | |
using Akka.Configuration.Hocon; | |
namespace Samples.Cluster.Simple | |
{ | |
class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
StartUp(args.Length == 0 ? new[] { "2551", "2552", "0" } : args); | |
Console.WriteLine("Press any key to exit"); | |
Console.ReadKey(); | |
} | |
public static void StartUp(string[] ports) | |
{ | |
var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka"); | |
List<IActorRef> SlowPokes = new List<IActorRef>(); | |
List<IActorRef> Responders = new List<IActorRef>(); | |
List<IActorRef> Responders2 = new List<IActorRef>(); | |
foreach (var port in ports) | |
{ | |
//Override the configuration of the port | |
var config = | |
ConfigurationFactory.ParseString("akka.remote.helios.tcp.port=" + port) | |
.WithFallback(section.AkkaConfig); | |
//create an Akka system | |
var system = ActorSystem.Create("ClusterSystem", config); | |
//create an actor that handles cluster domain events | |
system.ActorOf(Props.Create(typeof(SimpleClusterListener)), "clusterListener"); | |
var slowPokeCount = 100; | |
for (var i = 0; i < slowPokeCount; i++) | |
{ | |
SlowPokes.Add(system.ActorOf(Props.Create(() => new SlowActor()), string.Format("slowPoke:{0}", i))); | |
Responders.Add(system.ActorOf(Props.Create(() => new RespondingActor()), string.Format("responder:{0}", i))); | |
Responders2.Add(system.ActorOf(Props.Create(() => new RespondingActor2()), string.Format("responder2:{0}", i))); | |
} | |
} | |
while (true) | |
{ | |
Console.ReadLine(); | |
Task.Run(() => | |
{ | |
for (int i = 0; i < 10000; i++) | |
{ | |
IActorRef actorToAsk = i % 100 == 0 ? Responders.Last() : Responders[(i % 100) - 1]; | |
IActorRef actorToAsk2 = i % 100 == 0 ? Responders2.Last() : Responders2[(i % 100) - 1]; | |
SlowPokes.ForEach(slow => slow.Tell(new Tuple<IActorRef, IActorRef>(actorToAsk, actorToAsk2))); | |
} | |
Console.WriteLine("queued"); | |
}); | |
} | |
} | |
} | |
public class BigJob | |
{ | |
public IActorRef Origin { get; set; } | |
public IActorRef[] Participants { get; set; } | |
public string[] Results {get;set;} | |
} | |
public class SlowActor : ReceiveActor | |
{ | |
public SlowActor() | |
{ | |
Receive<Tuple<IActorRef, IActorRef>>(askActor => | |
{ | |
askActor.Item1.Tell(new BigJob | |
{ | |
Origin = Self, | |
Results = new string[2], | |
Participants = new[] { askActor.Item1, askActor.Item2} | |
}); | |
}); | |
Receive<BigJob>(result => | |
{ | |
}); | |
} | |
} | |
public class RespondingActor : ReceiveActor | |
{ | |
public RespondingActor() | |
{ | |
Receive<BigJob>(async message => | |
{ | |
await Task.Delay(1000); | |
var result = new BigJob | |
{ | |
Origin = message.Origin, | |
Results = new string[2], | |
Participants = message.Participants | |
}; | |
result.Results[0] = "no"; | |
message.Participants[1].Tell(result); | |
}); | |
} | |
} | |
public class RespondingActor2 : ReceiveActor | |
{ | |
public RespondingActor2() | |
{ | |
Receive<BigJob>(async message => | |
{ | |
await Task.Delay(1000); | |
var result = new BigJob | |
{ | |
Origin = message.Origin, | |
Results = message.Results, | |
Participants = message.Participants | |
}; | |
result.Results[1] = "yes"; | |
result.Origin.Tell(result); | |
}); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment