다음과 같은 간단한 액트를 설계해보자

  • 인사를 받아주는 액트(Greeter)
  • 인사에 반응하여 로그를 찍는 액트( Printer)

Greeter
package com.psmon.akka.study;

import com.psmon.akka.study.Printer.Greeting;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;

//#greeter-messages
public class Greeter extends AbstractActor {
//#greeter-messages
  static public Props props(String message, ActorRef printerActor) {
    return Props.create(Greeter.class, () -> new Greeter(message, printerActor));
  }

  //#greeter-messages
  static public class WhoToGreet {
    public final String who;

    public WhoToGreet(String who) {
        this.who = who;
    }
  }

  static public class Greet {
    public Greet() {
    }
  }
  //#greeter-messages

  private final String message;
  private final ActorRef printerActor;
  private String greeting = "";

  public Greeter(String message, ActorRef printerActor) {
    this.message = message;
    this.printerActor = printerActor;
  }

  @Override
  public Receive createReceive() {
    return receiveBuilder()
        .match(WhoToGreet.class, wtg -> {
          this.greeting = message + ", " + wtg.who;
        })
        .match(Greet.class, x -> {
          //#greeter-send-message
          printerActor.tell(new Greeting(greeting), getSelf());
          //#greeter-send-message
        })
        .build();
  }
//#greeter-messages
}
//#greeter-messages

메시지처리기를 잘 살펴보면, if및 switch문이 없는것을 볼수가 있으며

이러한 분기 처리기법을 패턴매칭이라고도 합니다. 모던한 언어스펙이 여러가지모습 으로

지원하는 컨셉이며  익숙하지 않는다고 하면 전통적인 방식으로 메시처리도 가능합니다.

차이점은 베이스가 되는 액터( AbstractActor or UntypedActor ) 가 누구냐에따라 메시지분기처리에대한

방법을 변경할수가 있습니다.

public class TestActor extends UntypedActor {
    private final LoggingAdapter log = Logging
            .getLogger(getContext().system(), "TestActor");

    @Override
    public void onReceive(Object message) throws Exception {    	    	
    	if(message instanceof String) {	//String뿐만 아니라 모든 Java객체 통신가능
    		log.info("Incommessage {}", message);
    		sender().tell("너의 메시지에 응답을함", ActorRef.noSender());    		
    	}else {
    		log.info("Unhandle Message {}", message);    		
    	}   	
    }
}




GreeterPrinter
package com.psmon.akka.study;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.Logging;
import akka.event.LoggingAdapter;

//#printer-messages
public class Printer extends AbstractActor {
//#printer-messages
  static public Props props() {
    return Props.create(Printer.class, () -> new Printer());
  }

  //#printer-messages
  static public class Greeting {
    public final String message;

    public Greeting(String message) {
      this.message = message;
    }
  }
  //#printer-messages

  private LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);

  public Printer() {
  }

  @Override
  public Receive createReceive() {
    return receiveBuilder()
        .match(Greeting.class, greeting -> {
            log.info(greeting.message);
        })
        .build();
  }
//#printer-messages
}
//#printer-messages
수행
package com.psmon.akka.study;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;

import java.io.IOException;

import com.psmon.akka.study.Greeter.*;

public class AkkaQuickstart {
  public static void main(String[] args) {
    final ActorSystem system = ActorSystem.create("helloakka");
    try {
      //#create-actors
      final ActorRef printerActor = 
        system.actorOf(Printer.props(), "printerActor");
      final ActorRef howdyGreeter = 
        system.actorOf(Greeter.props("Howdy", printerActor), "howdyGreeter");
      final ActorRef helloGreeter = 
        system.actorOf(Greeter.props("Hello", printerActor), "helloGreeter");
      final ActorRef goodDayGreeter = 
        system.actorOf(Greeter.props("Good day", printerActor), "goodDayGreeter");
      //#create-actors

      //#main-send-messages
      howdyGreeter.tell(new WhoToGreet("Akka"), ActorRef.noSender());
      howdyGreeter.tell(new Greet(), ActorRef.noSender());

      howdyGreeter.tell(new WhoToGreet("Lightbend"), ActorRef.noSender());
      howdyGreeter.tell(new Greet(), ActorRef.noSender());

      helloGreeter.tell(new WhoToGreet("Java"), ActorRef.noSender());
      helloGreeter.tell(new Greet(), ActorRef.noSender());

      goodDayGreeter.tell(new WhoToGreet("Play"), ActorRef.noSender());
      goodDayGreeter.tell(new Greet(), ActorRef.noSender());
            
      
      //#main-send-messages

      System.out.println(">>> Press ENTER to exit <<<");
      System.in.read();
    } catch (IOException ioe) {
    } finally {
      system.terminate();
    }
  }
}


[INFO] [10/31/2017 15:33:29.692] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/printerActor] Good day, Play
[INFO] [10/31/2017 15:33:29.693] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/printerActor] Howdy, Akka
[INFO] [10/31/2017 15:33:29.693] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/printerActor] Hello, Java
[INFO] [10/31/2017 15:33:29.693] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/printerActor] Howdy, Lightbend


위 코드는 AKKA QUCIK START에 의해 자동 생성된 코드입니다.

https://developer.lightbend.com/start/?group=akka&project=akka-quickstart-java





  • No labels