필요 네임스페이스
필요한것만 설치
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Akka; using Akka.Actor; using Akka.Event; using Akka.Routing; using CommonActor; using Akka.Configuration; using Akka.Cluster; using Akka.Streams; using Akka.Streams.Dsl; using System.IO; using Akka.IO; using Akka.Streams.IO;
액터 시스템 설정
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="akka" type="Akka.Configuration.Hocon.AkkaConfigurationSection, Akka"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/> </startup> <akka> <hocon> <![CDATA[ akka { loglevel = DEBUG actor { provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote" #provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster" } remote { log-remote-lifecycle-events = DEBUG log-received-messages = on helios.tcp { port = 8002 #bound to a specific port hostname = 127.0.0.1 } } } ]]> </hocon> </akka> </configuration>
액터 시스템 생성
string strCfg = @"akka { actor{ provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote"" deployment = { /workers{ router = round-robin-group routees.paths = [""akka.tcp://ANODE@127.0.0.1:7000/user/h1"",""akka.tcp://ANODE@127.0.0.1:7000/user/h2"",""akka.tcp://ANODE@127.0.0.1:7000/user/h3""] } } } remote { helios.tcp { port = " + port + @" #bound to a specific port hostname = 127.0.0.1 } } }"; var baseConfig = ConfigurationFactory.ParseString(strCfg); actorSystem = ActorSystem.Create(systemName, baseConfig); //baseConfig 생략시 기본 설정에서 로드 app.config
액터 설계
public class Hello { public Hello(string message) { Message = message; } public string Message { get; private set; } } public class HelloReq { public HelloReq(string message) { Message = message; } public string Message { get; private set; } } public class EchoActor2 : ReceiveActor { protected int recCnt = 0; protected bool isLog() { if (recCnt < 10 || recCnt % 100 == 0) return true; else return false; } public EchoActor2() { Receive<Hello>(hello => { recCnt++; if(isLog()) Console.WriteLine("Log: [{0} -> {1} ]: {2}", Sender,Self.Path , hello.Message); Sender.Tell(new HelloReq(hello.Message)); }); Receive<HelloReq>(hello => { recCnt++; if (isLog()) Console.WriteLine("Log: [{0} -> {1} ]: {2}", Sender, Self.Path, hello.Message); }); } }
액터 생성
// 기본 액터 actorSystem.ActorOf<EchoActor2>("hello"); // 라우터 액터 var props = Props.Create<EchoActor2>().WithRouter(FromConfig.Instance); var routerActor = actorSystem.ActorOf(props, "workers");
액터 선택
var remoteHello = actorSystem.ActorSelection("akka.tcp://ANODE@127.0.0.1:7000/user/hello");
메시지 전송
var receivedActor = actorSystem.ActorOf<EchoActor2>("hello"); routerActor.Tell(new Hello("i'am BNODE idx:" + i), receivedActor/* 응답처리가 필요할시 수신액터를 지정함 */);