2018-03-13 23:45:55 +09:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
|
2018-03-13 23:45:55 +09:00
|
|
|
*/
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.cluster;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
import static jdocs.cluster.TransformationMessages.BACKEND_REGISTRATION;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.cluster.TransformationMessages.JobFailed;
|
|
|
|
|
import jdocs.cluster.TransformationMessages.TransformationJob;
|
2017-02-14 13:10:23 +02:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Terminated;
|
2017-02-23 00:50:07 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #frontend
|
2017-02-23 00:50:07 +05:00
|
|
|
public class TransformationFrontend extends AbstractActor {
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
List<ActorRef> backends = new ArrayList<ActorRef>();
|
|
|
|
|
int jobCounter = 0;
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-02-23 00:50:07 +05:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
.match(
|
|
|
|
|
TransformationJob.class,
|
|
|
|
|
job -> backends.isEmpty(),
|
|
|
|
|
job -> {
|
|
|
|
|
getSender()
|
|
|
|
|
.tell(new JobFailed("Service unavailable, try again later", job), getSender());
|
|
|
|
|
})
|
|
|
|
|
.match(
|
|
|
|
|
TransformationJob.class,
|
|
|
|
|
job -> {
|
|
|
|
|
jobCounter++;
|
|
|
|
|
backends.get(jobCounter % backends.size()).forward(job, getContext());
|
|
|
|
|
})
|
|
|
|
|
.matchEquals(
|
|
|
|
|
BACKEND_REGISTRATION,
|
|
|
|
|
x -> {
|
|
|
|
|
getContext().watch(getSender());
|
|
|
|
|
backends.add(getSender());
|
|
|
|
|
})
|
|
|
|
|
.match(
|
|
|
|
|
Terminated.class,
|
|
|
|
|
terminated -> {
|
|
|
|
|
backends.remove(terminated.getActor());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2017-02-14 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #frontend
|