Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created February 16, 2015 04:56
Show Gist options
  • Save loosechainsaw/303628b83737acc1e08c to your computer and use it in GitHub Desktop.
Save loosechainsaw/303628b83737acc1e08c to your computer and use it in GitHub Desktop.
Remote Actors Sample
using System;
using System.Threading;
using Akka;
using Akka.Actor;
namespace AkkaRemoting
{
public class RequestMessage
{
}
public class BackendActor : ReceiveActor
{
public static Random random = new Random(41);
public BackendActor()
{
Receive<RequestMessage>(x =>
{
var sender = Sender;
Console.WriteLine("The value is: " + random.Next());
});
}
}
public class FrontendActor : ReceiveActor
{
public FrontendActor()
{
Receive<RequestMessage>(x =>
{
var sender = Sender;
var selection = Context.System.ActorSelection("akka.tcp://System1@localhost:8888/user/backend");
selection.Tell(new RequestMessage());
});
}
}
class Program
{
static void Main(string[] args)
{
var system1configuration = FluentConfig.Begin().StartRemotingOn("localhost", 8888);
var system2configuration = FluentConfig.Begin().StartRemotingOn("localhost", 8889);
var system1 = ActorSystem.Create("System1", system1configuration.Build());
var system2 = ActorSystem.Create("System2", system2configuration.Build());
var backend = system1.ActorOf(Props.Create<BackendActor>(),"backend");
var frontend = system2.ActorOf(Props.Create<FrontendActor>(),"frontend");
Thread.Sleep(1000);
var result = frontend.Ask<string>(new RequestMessage());
result.ContinueWith(x => Console.WriteLine(x.Result));
Console.WriteLine("Press any key to exit....");
Console.ReadKey();
system1.Shutdown();
system2.Shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment