Skip to content

Rx

actor4j-rx makes received messages available as an RxJava Flowable<ActorMessage<?>>. Instead of handling each message in receive, the messages can be filtered, mapped and combined with the operators of RxJava. The module provides three classes:

Class Base class (Actor4j - Core) Flowable over
PseudoActorWithRx PseudoActor the message queue of the pseudo actor (runWithRx())
ConcurrentPseudoActorWithRx ConcurrentPseudoActor the thread-safe outer queue of the pseudo actor (runWithRx())
ActorWithRxStash Actor the stash of the actor (rxStash)

The helper ActorMessageFlowable.getMessages(queue) creates such a flowable for any Queue<ActorMessage<?>>.

A PseudoActor mediates between the outside world and the actor system. It has its own message queue, and the received messages are processed by calling run, runOnce or runAll, which pass them to receive. PseudoActorWithRx and ConcurrentPseudoActorWithRx add runWithRx() as a further way to process them. The rest of the API (send, tell, await, getId, …) stays the same.

The flowables are cold and synchronous. A subscription polls the queue until it is empty and then completes. Each polled message is removed from the queue, even if an operator like filter drops it later. Messages taken by a flowable are not passed to receive. A later subscription only sees messages that arrived in the meantime.

actor4j-rx is independent of actor4j-streams. That module also uses RxJava, but for data-parallel processing of lists with actors (ActorStream), not for message queues.

<dependency>
<groupId>io.actor4j</groupId>
<artifactId>actor4j-rx</artifactId>
<version>2.5.0</version>
</dependency>

The module depends on actor4j-core-runtime (the default runtime) and on RxJava (io.reactivex.rxjava3:rxjava, version 3.1.12).

runWithRx() returns a flowable over the received messages. Like run(), it takes at most bufferQueueSize (configuration) messages per call from the incoming queue. receive has to be implemented, but it is only called by run, runOnce and runAll.

ActorSystem system = ActorSystem.create(ActorRuntime.factory());
PseudoActorWithRx main = new PseudoActorWithRx(system, false) {
@Override
public void receive(ActorMessage<?> message) {
// only used by run(), runOnce() and runAll()
}
};
ActorId doubler = system.addActor(() -> new Actor("doubler") {
@Override
public void receive(ActorMessage<?> message) {
tell(message.valueAsInt()*2, 0, message.source());
}
});
system.start();
for (int i=0; i<5; i++)
main.send(ActorMessage.create(i, 0, main.getId(), doubler));
// later, e.g. in the loop of the application
main.runWithRx()
.filter(msg -> msg.valueAsInt() > 2)
.map(msg -> "main received " + msg.valueAsInt())
.subscribe(System.out::println);

The second constructor argument blocking selects a blocking incoming queue, which is needed for await. PseudoActorWithRx should be processed by one thread only, because the processed messages are buffered in a non-thread-safe queue.

ConcurrentPseudoActorWithRx is used in the same way. Its runWithRx() polls the thread-safe outer queue of the pseudo actor directly, without a limit per call. Constructors: (name, system), (system, blocking) and (name, system, blocking).

ConcurrentPseudoActorWithRx main = new ConcurrentPseudoActorWithRx(system, false) {
@Override
public void receive(ActorMessage<?> message) {
// only used by run(), runOnce() and runAll()
}
};
main.runWithRx()
.take(2)
.subscribe(msg -> System.out.println(msg.value()));

In Actor, the stash has to be initialized by hand. ActorWithRxStash initializes it (LinkedList) and offers the field rxStash, a flowable over the stash. Operators can be applied to rxStash once, e.g. in preStart. Each subscription then processes all stashed messages in the thread of the actor. unstash() still takes single messages.

ActorId receiver = system.addActor(() -> new ActorWithRxStash("receiver") {
@Override
public void preStart() {
rxStash = rxStash
.filter(msg -> msg.valueAsInt() > 50)
.map(msg -> ActorMessage.create(msg.valueAsInt(), msg.tag()+100, msg.source(), msg.dest()));
}
@Override
public void receive(ActorMessage<?> message) {
if (message.tag()==0)
stash.offer(message);
else if (message.tag()==1)
rxStash.subscribe(msg -> System.out.println(msg.valueAsInt()));
}
});

Messages with tag 0 are stashed. A message with tag 1 processes the stash: values up to 50 are dropped and removed from the stash, the others are printed. As the stash belongs to the actor, schedulers like subscribeOn or observeOn should not be used here.