Page History
...
Expand | ||||||
---|---|---|---|---|---|---|
| ||||||
|
심플 라우팅 워커 설계
draw.io Diagram | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Wokers구현
Expand | ||
---|---|---|
| ||
akka { |
Code Block | ||||
---|---|---|---|---|
| ||||
@Component
@Scope("prototype")
public class Workers extends AbstractActor {
private final LoggingAdapter log = Logging
.getLogger(getContext().system(), "Workers");
@Autowired
private ApplicationContext context;
private ActorRef roundrobinpool = null;
@Override public void preStart() {
log.info("Workers::preStart");
SpringExtension ext = context.getBean(SpringExtension.class);
roundrobinpool = getContext().actorOf( FromConfig.getInstance().props( ext.props("testActor") ) , "router1"); //액터설정에 의해 5개의 라우티가 생성됨
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchEquals("hi1",s->{ //hi1 메시지를 받으면 라운드로빈처리를함
log.info("Workers::Received String message: {}", s);
roundrobinpool.tell(s, getSender() ); // 입력된 잡(hi1)을 라운드 로빈라우터에게 메시지전송
})
.matchAny(o -> log.info("received unknown message"))
.build();
}
} |
테스트코드
Code Block | ||||
---|---|---|---|---|
| ||||
protected void routerTest(ActorSystem system,SpringExtension ext) {
new TestKit(system) {{
final ActorRef workers = system.actorOf( ext.props("workers"),"workers"); // workers 액터생성
workers.tell( "hi1", getRef() );
// await the correct response
expectMsg(java.time.Duration.ofSeconds(1), "hi too");
workers.tell( "hi1", getRef() );
// await the correct response
expectMsg(java.time.Duration.ofSeconds(1), "hi too");
}};
} |
Expand | ||
---|---|---|
| ||
[INFO] [05/17/2018 20:14:32.578] [AkkaTestApp-akka.actor.default-dispatcher-4] [Workers] Workers::preStart |