I'd like to use actors in a program where I'll have some kind of restriction around treating some of the actors as if they were queues. For example, suppose I have some external system to which change events are applied and also some cache of the external system's data. So I have 2 actors:
ChangeApplicationActorCacheActor
As part of the ChangeApplicationActor, when I apply a change to some entity X in the external system, I want to send some event to tell the CacheActor to sync:
val changeApplicationActor = actor {
loop {
react {
case ChangeInstruction(x) =>
externalSystem.applyChange(x)
cacheActor ! Sync(x)
}
}
}
But I Now have two requirements:
- The
CacheActorhas internal state and ideally I'd like it to process itsSyncinstructions sequentially - If I end up with the
CacheActor's inbox containing twoSync(x)instructions for the same value ofx, then I'd like to ignore the second one (i.e. I should only have one pendingSyncinstruction for any given value ofx)
Is there any way of forcing an actor to be single-threaded? Is there any way I can access the actor's mailbox and remove any duplicate events? Can I not avoid implementing the CacheActor as, um, not an Actor?