Extending on RedisSubscriberActor and overriding receive
I've tried to do the following with disastrously unpredictable behavior. It took a while to understand the problem was with overriding function receive. I would like to dynamically add and withdraw channels to a single subscriber instead of creating many subscribers and passing a final channel list to their constructor. Do you advise against this ? Do you have a suggestion on how to override receive properly to fit your design ?
case class RedisUuidSubscriberAdd(uuid: UUID)
case class RedisUuidSubscriberRemove(uuid: UUID)
// A simple Redis channel subscriber which will forward any Message to its parent
class RedisUuidSubscriber()
extends RedisSubscriberActor(new InetSocketAddress("localhost", 6379), Seq(), Seq()) {
def onMessage(message: Message) {
context.parent ! message
}
def onPMessage(pmessage: PMessage) = Unit
override def receive = {
case RedisUuidSubscriberAdd(uuid: UUID) =>
subscribe("uuid:" + uuid)
case RedisUuidSubscriberRemove(uuid: UUID) =>
unsubscribe("uuid:" + uuid)
case other =>
super.receive(other)
}
}
Best regards,
What you want, has already been done, you just have to send the right message. https://github.com/etaty/rediscala/blob/master/src/main/scala/redis/actors/RedisSubscriberActor.scala#L60-L74
I don't advise against overriding the receive, but it is a poor choice when Scala makes Partial Function composition so simple ;) See : "Extending Actors using PartialFunction chaining" http://doc.akka.io/docs/akka/snapshot/scala/actors.html
You seems new to scala && akka, i would advise you to have a quick read of the documentation of akka, there are some good practise inside.