Created
August 19, 2012 17:52
-
-
Save ArvinH/3396716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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