Skip to content

Instantly share code, notes, and snippets.

@ArvinH
Created August 19, 2012 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArvinH/3396716 to your computer and use it in GitHub Desktop.
Save ArvinH/3396716 to your computer and use it in GitHub Desktop.
package org.sample.pubsub
import com.redis._
import akka.actor.ActorSystem
import akka.actor.Actor._
import akka.actor.Props
object Sub {
val actorSystem = ActorSystem("PubSubActorSystem")
println("starting subscription service...")
var r = new RedisClient("localhost",6379)
var s = actorSystem.actorOf(Props(new Subscriber(r)))
s ! Register(callback)
def sub(channels: String*) = {
s ! Subscribe(channels.toArray)
}
def unsub(channels: String*) = {
s ! Unsubscribe(channels.toArray)
}
def callback(pubsub: PubSubMessage) = pubsub match {
case S(channel, no) => println("subscribed to " + channel + " and count = " + no)
case U(channel, no) => println("unsubscribed from " + channel + " and count = " + no)
case M(channel, msg) =>
msg match {
// exit will unsubscribe from all channels and stop subscription service
case "exit" =>
println("unsubscribe all ..")
r.unsubscribe
// message "+x" will subscribe to channel x
case x if x startsWith "+" =>
val s: Seq[Char] = x
s match {
case Seq('+', rest @ _*) => r.subscribe(rest.toString){ m => }
}
// message "-x" will unsubscribe from channel x
case x if x startsWith "-" =>
val s: Seq[Char] = x
s match {
case Seq('-', rest @ _*) => r.unsubscribe(rest.toString)
}
// other message receive
case x =>
println("received message on channel " + channel + " as : " + x)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment