eu.lucaventuri.fibry.SynchronousActor 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;
/**
* Actor that runs in the same thread as the caller; this is achieved with the Actor being the queue as well.
* THis can be useful in particular cases.
*/
public class SynchronousActor extends Actor implements MiniFibryQueue> {
public SynchronousActor(Consumer actorLogic, S initialState, Consumer initializer, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
super(actorLogic, new MiniFibryQueue.MiniFibryQueueDelegate<>(), initialState, initializer, finalizer, closeStrategy, pollTimeoutMs);
sendPoisonPillWhenExiting = false;
((MiniFibryQueue.MiniFibryQueueDelegate) queue).setQueue(this);
}
SynchronousActor(Function actorLogicReturn, S initialState, Consumer initializer, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
super(actorLogicReturn, new MiniFibryQueue.MiniFibryQueueDelegate<>(), initialState, initializer, finalizer, closeStrategy, pollTimeoutMs);
sendPoisonPillWhenExiting = false;
((MiniFibryQueue.MiniFibryQueueDelegate) queue).setQueue(this);
}
@Override
protected void takeAndProcessSingleMessage() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
protected void takeAndProcessSingleMessageTimeout() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public boolean add(Either3>, T, MessageWithAnswer> message) {
message.ifEither(cns -> cns.accept(this), actorLogic::accept, actorLogicReturn::accept);
return true;
}
@Override
public Either3>, T, MessageWithAnswer> take() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public Either3>, T, MessageWithAnswer> poll(long timeout, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
}
@Override
public int size() {
return 0;
}
@Override
public Either3>, T, MessageWithAnswer> peek() {
throw new UnsupportedOperationException();
}
@Override
public boolean sendPoisonPill() {
sendPoisonPillWhenExiting = false;
askExit();
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy