eu.lucaventuri.fibry.CustomActor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fibry Show documentation
Show all versions of fibry Show documentation
The first Java Actor System supporting fibers from Project Loom
package eu.lucaventuri.fibry;
import eu.lucaventuri.functional.Either3;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class CustomActor extends BaseActor {
protected final Consumer> actorLogicReturn;
protected CustomActor(MiniQueue>, T, MessageWithAnswer>> queue, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
super(queue, finalizer, closeStrategy, pollTimeoutMs, null, null);
Function tmpLogicReturn = ActorUtils.discardingToReturning(this::onMessage);
this.actorLogicReturn = mwr -> mwr.answer.complete(tmpLogicReturn.apply(mwr.message));
}
protected CustomActor(MiniQueue>, T, MessageWithAnswer>> queue, Consumer finalizer, int pollTimeoutMs) {
this(queue, finalizer, null, pollTimeoutMs);
}
protected abstract void onMessage(T message);
protected void onNoMessages() { }
protected void takeAndProcessSingleMessage() throws InterruptedException {
Either3>, T, MessageWithAnswer> message = queue.take();
message.ifEither(cns -> cns.accept(this), this::onMessage, actorLogicReturn::accept);
}
@Override
protected void takeAndProcessSingleMessageTimeout() throws InterruptedException {
Either3>, T, MessageWithAnswer> message = queue.poll(pollTimeoutMs, TimeUnit.MILLISECONDS);
if (message != null)
message.ifEither(cns -> cns.accept(this), this::onMessage, actorLogicReturn::accept);
else
onNoMessages();
}
@Override
protected BaseActor recreate() {
throw new UnsupportedOperationException("Receiving actors do not support auto healing");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy