Akka: Cleanup of dynamically created actors necessary when they have finished?

后端 未结 4 1850
甜味超标
甜味超标 2021-02-07 01:33

I have implemented an Actor system using Akka and its Java API UntypedActor. In it, one actor (type A) starts other actors (type B) dynamically on demand, using getContext

4条回答
  •  广开言路
    2021-02-07 02:25

    I was profiling(visualvm) one of the sample cluster application from AKKA documentation and I see garbage collection cleaning up the per request actors during every GC. Unable to completely understand the recommendation of explicitly killing the actor after use. My actorsystem and actors are managed by SPRING IOC container and I use spring extension in-direct actor-producer to create actors. The "aggregator" actor is getting garbage collected on every GC, i did monitor the # of instances in visual VM.

    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class StatsService extends AbstractActor {
    
        private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
    
        @Autowired
        private ActorSystem actorSystem;
    
        private ActorRef workerRouter;
    
        @Override
        public void preStart() throws Exception {
            System.out.println("Creating Router" + this.getClass().getCanonicalName());
           workerRouter = getContext().actorOf(SPRING_PRO.get(actorSystem)
                .props("statsWorker").withRouter(new FromConfig()), "workerRouter"); 
            super.preStart();
        }
    
        @Override
        public Receive createReceive() {
            return receiveBuilder()
                .match(StatsJob.class, job -> !job.getText().isEmpty(), job -> {
                    final String[] words = job.getText().split(" ");
                    final ActorRef replyTo = sender();
                    final ActorRef aggregator = getContext().actorOf(SPRING_PRO.get(actorSystem)
                        .props("statsAggregator", words.length, replyTo));
    
                    for (final String word : words) {
                        workerRouter.tell(new ConsistentHashableEnvelope(word, word),
                            aggregator);
                    }
                })
                .build();
        }
    }
    

提交回复
热议问题