You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

액터는 양방향 메시지를 주고받을수 있는 객체이며 하나의 장비에 수십만객체 생성이가능합니다.

네트워크에 있어서도 체계적이고 효과적인 패킷전송을위해 라우팅설계가 중요하며

액터를 사용하는 분산 어플리케이션에서도 이러한 전략이 필요하며 단순하게 사용이 가능합니다.


라우팅전략

Unable to render {include} The included page could not be found.



심플 라우팅 워커 설계


Wokers구현

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

@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();
	}
}


테스트코드

	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");					    
	    }};
	}

[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





  • No labels