Akka Distributed Publish Subscribe in Cluster #2.Send
Akka Distributed Publish Subscribe in Cluster #2.Send
Send
- point-point mode
- cluster 내부 node에 actor가 어디에 있는지를 모르는 경우에 사용을 한다.
- 특정 일을 하고 있는 Actor를 찾는 경우
- 채팅방에서 상대방의 Actor를 찾는 경우
- 메세지가는 path가 매칭되는 하나의 actor에게만 전달이 된다.
- 여러개인 경우에는 RoutingLogic 에 따라 하나의 actor에만 메시지가 전달이 된다.
- DistributedPubSubMediator.Put 를 이용하여 ActorRef를 Path로 registry 에 등록이 된다.
- DistributedPubSubMediator.Send를 이용하여 해당하는 Path로 메시지를 전달을 한다.
- 종료될때 자동으로 registry 에 삭제가 되지만 DistributedPubSubMediator.Remove 을 이용하여 프로그래밍적으로도 처리가 가능하다.
- DistributedPubSubMediator.SendToAll 메세지를 이용하여 Broadcast 메세지를 보낼수가 있다.
Put을 이용하여 ActorRef를 등록하기
public class Destination extends AbstractActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public Destination() {
ActorRef mediator =
DistributedPubSub.get(getContext().system()).mediator();
// register to the path
mediator.tell(new DistributedPubSubMediator.Put(getSelf()), getSelf());
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, msg ->
log.info("Got: {}", msg))
.build();
}
}
system.actorOf(Props.create(Destination.class), "destination");
//another node
system.actorOf(Props.create(Destination.class), "destination");
Send를 이용하여 메세지를 전달하기
//somewhere else
ActorRef sender = system.actorOf(Props.create(Publisher.class), "sender");
// after a while the destinations are replicated
sender.tell("hello", null);