AkkaSystem 사용은 다양한 방식으로 실행화 할수 있습니다.
Akka.net 설치
akka 설치
PM> install-package Akka PM> install-package Akka.Remote
Console 실행 시나리오
using Akka; using Akka.Actor; using Akka.Configuration; namespace Foo.Bar { class Program { static void Main(string[] args) { //configure remoting for localhost:8081 var fluentConfig = FluentConfig.Begin() .StartRemotingOn("localhost", 8081) .Build(); using (var system = ActorSystem.Create("my-actor-server", fluentConfig)) { //start two services var service1= system.ActorOf<Service1>("service1"); var service2 = system.ActorOf<Service2>("service2"); Console.ReadKey(); } } } } ASP.NET
ASP.NET ( IIS 내부실행 전략 )
public class MvcApplication : System.Web.HttpApplication { protected static ActorSystem ActorSystem; //here you would store your toplevel actor-refs protected static IActorRef MyActor; protected void Application_Start() { //your mvc config. Does not really matter if you initialise //your actor system before or after ActorSystem = ActorSystem.Create("app"); //here you would register your toplevel actors MyActor = ActorSystem.ActorOf<MyActor>(); } }
Interaction between Controllers and Akka.NET ( 특정 API에 Actor의 기능 수행을 연결한 케이스 )
public class SomeController : ApiController { //expose your endpoint as async public async Task<SomeResult> Post(SomeRequest someRequest) { //send a message based on your incoming arguments to one of the actors you created earlier //and await the result by sending the message to `Ask` var result = await MvcApplication.MyActor.Ask<SomeResult>(new SomeMessage(someRequest.SomeArg1,someRequest.SomeArg2)); return result; } }
WindowsService ( Topshelf를 이용한 윈도우 서비스화)
using Akka.Actor; using Topshelf; class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<MyActorService>(s => { s.ConstructUsing(n => new MyActorService()); s.WhenStarted(service => service.Start()); s.WhenStopped(service => service.Stop()); //continue and restart directives are also available }); x.RunAsLocalSystem(); x.UseAssemblyInfoForServiceInfo(); }); } } /// <summary> /// This class acts as an interface between your application and TopShelf /// </summary> public class MyActorService { private ActorSystem mySystem; public void Start() { //this is where you setup your actor system and other things mySystem = ActorSystem.Create("MySystem"); } public async void Stop() { //this is where you stop your actor system await mySystem.Terminate(); } }
Azure PaaS Worker Role ( MS-Azure 클라우드 환경에서의 실행연동 )
namespace MyActorWorkerRole { public class WorkerRole : RoleEntryPoint { private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false); private ActorSystem _actorSystem; public override bool OnStart() { // Setup the Actor System _actorSystem = ActorSystem.Create("MySystem"); return (base.OnStart()); } public override void OnStop() { this.cancellationTokenSource.Cancel(); this.runCompleteEvent.WaitOne(); // Shutdown the Actor System _actorSystem.Shutdown(); base.OnStop(); } public override void Run() { try { this.RunAsync(this.cancellationTokenSource.Token).Wait(); } finally { this.runCompleteEvent.Set(); } } private async Task RunAsync(CancellationToken cancellationToken) { // Create an instance to the top-level user Actor var workerRoleActor = _actorSystem.ActorOf<WorkerRoleActor>("WorkerRole"); // Send a message to the Actor workerRoleActor.Tell(new WorkerRoleMessage("Hello World!")); while (!cancellationToken.IsCancellationRequested) { await Task.Delay(1000); } } } }