I have an actor, which I want to perform an action with delay, i.e. something like this (pseudocode):
void onReceive(message: Message){
if (message is DelayedAction){
sleep(100);
preformSomeAction();
}
/*...*/
}
It is prudent to wait in another thread/strand while processing the other message, so I've added an ScheduledThreadPool
for an actor:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(0);
void onReceive(message: Message){
if (message is DelayedAction){
executor.schedule(sendMessageForAction, delay, TimeUnit.MILLISECONDS);
}
else if (message is MessageForAction){
preformSomeAction();
}
/*...*/
}
So, waiting is performed in executor thread pool, and after that a message sent to actor.self to actually perform a task.
But sometimes messages are lost (only increasing number of threads in the pool helps). Now I'm not quite sure that I'm doing a right thing: may be quasar is not ok with threadPoolExecutors and I should use some other approach to delay action perform?
What is the right approach to delayed action on message receiving?
UPDATE:
One other option for me is to use fibers without pool:
void onReceive(message: Message){
if (message is DelayedAction){
new Fiber<V>() {
@Override
protected V run() throws SuspendExecution, InterruptedException {
Strand.sleep(delay);
sendMessageForAction();
}
}.start();
}
else if (message is MessageForAction){
preformSomeAction();
}
/*...*/
}
I'm still not sure if it is the right way to go.