Skip to content

Instantly share code, notes, and snippets.

@aleksandara
Forked from minosiants/gist:3163791
Created May 2, 2013 20:34
Show Gist options
  • Save aleksandara/5505195 to your computer and use it in GitHub Desktop.
Save aleksandara/5505195 to your computer and use it in GitHub Desktop.
package com.minosiants
import akka.event.ActorEventBus
import akka.event.LookupClassification
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.Actor
import java.util.Date
import java.util.UUID
case class Message(val id:String,val timestamp: Long)
case class PostMessage( override val id:String=UUID.randomUUID().toString(),
override val timestamp: Long=new Date().getTime(),
text:String)extends Message(id, timestamp)
case class MessageEvent(val channel:String, val message:Message)
class AppActorEventBus extends ActorEventBus with LookupClassification{
type Event = MessageEvent
type Classifier=String
protected def mapSize(): Int={
10
}
protected def classify(event: Event): Classifier={
event.channel
}
protected def publish(event: Event, subscriber: Subscriber): Unit={
subscriber ! event
}
}
object TryAkka extends App{
val system = ActorSystem("MySystem")
val appActorEventBus=new AppActorEventBus
val NEW_POST_CHANNEL="/posts/new"
val subscriber = system.actorOf(Props(new Actor {
def receive = {
case d: MessageEvent => println(d)
}
}))
appActorEventBus.subscribe(subscriber, NEW_POST_CHANNEL)
appActorEventBus.publish(MessageEvent(NEW_POST_CHANNEL,PostMessage(text="hello world")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment