eu.lucaventuri.fibry.CustomActorWithResult 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;
public abstract class CustomActorWithResult extends BaseActor {
protected final Consumer actorLogic;
protected CustomActorWithResult(MiniQueue>, T, MessageWithAnswer>> queue, Consumer finalizer, int pollTimeoutMs) {
this(queue, finalizer, null, pollTimeoutMs);
}
protected CustomActorWithResult(MiniQueue>, T, MessageWithAnswer>> queue, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
super(queue, finalizer, closeStrategy, pollTimeoutMs, null, null);
this.actorLogic = ActorUtils.returningToDiscarding(this::onMessage);
}
protected abstract R onMessage(T message);
protected void takeAndProcessSingleMessage() throws InterruptedException {
Either3>, T, MessageWithAnswer> message = queue.take();
message.ifEither(cns -> cns.accept(this), actorLogic::accept, mwr -> mwr.answer.complete(onMessage(mwr.message)));
}
@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, mwr -> mwr.answer.complete(onMessage(mwr.message)));
}
@Override
protected BaseActor recreate() {
throw new UnsupportedOperationException("Receiving actors do not support auto healing");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy