Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleConcept > 라우팅전략

Include Page
라우팅 전략
라우팅 전략



심플 라우팅 워커 설계

draw.io Diagram
bordertrue
viewerToolbartrue
fitWindowfalse
diagramNamewokersactor
simpleViewerfalse
width
diagramWidth613
revision2


Wokers구현

Expand
title라운드로빈설정

akka {
actor.deployment {
/workers/router1 {
router = round-robin-pool
nr-of-instances = 5
}
}
}

Code Block
languagejava
themeEmacs
@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
languagejava
themeEmacs
	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
title수행결과

[INFO] [05/17/2018 20:14:32.578] [AkkaTestApp-akka.actor.default-dispatcher-4] [Workers] Workers::preStart
[INFO] [05/17/2018 20:14:32.590] [AkkaTestApp-akka.actor.default-dispatcher-4] [Workers] Workers::Received String message: hi
[INFO] [05/17/2018 20:14:32.596] [AkkaTestApp-akka.actor.default-dispatcher-3] [TestActor] akka://AkkaTestApp/user/workers/router1/$a::Incommessage hi
[INFO] [05/17/2018 20:14:32.603] [AkkaTestApp-akka.actor.default-dispatcher-3] [Workers] Workers::Received String message: hi
[INFO] [05/17/2018 20:14:32.603] [AkkaTestApp-akka.actor.default-dispatcher-6] [TestActor] akka://AkkaTestApp/user/workers/router1/$b::Incommessage hi