Skip to content

Instantly share code, notes, and snippets.

@minosiants
Created July 23, 2012 14:06
Show Gist options
  • Save minosiants/3163791 to your computer and use it in GitHub Desktop.
Save minosiants/3163791 to your computer and use it in GitHub Desktop.
Akka EventBus simple example
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")))
}
@shirmanov
Copy link

cool sample :)

@java-artisan
Copy link

Documentation often references 'system.eventStream' as the way to subscribe and publish events. Isn't that needed any more ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment