Skip to content

Instantly share code, notes, and snippets.

@sstone
Created April 3, 2014 16:02
Show Gist options
  • Save sstone/9957280 to your computer and use it in GitHub Desktop.
Save sstone/9957280 to your computer and use it in GitHub Desktop.
Basic status listener sample
package com.github.sstone.amqp
import akka.actor.{Props, Actor, ActorSystem}
import com.rabbitmq.client.ConnectionFactory
import com.github.sstone.amqp.Amqp.{Publish, AddStatusListener}
import com.github.sstone.amqp.ChannelOwner.{Disconnected, Connected}
import scala.concurrent.duration._
object StatusListeners extends App {
import scala.concurrent.ExecutionContext.Implicits.global
implicit val system = ActorSystem("mySystem")
// create an AMQP connection
val connFactory = new ConnectionFactory()
connFactory.setUri("amqp://guest:guest@localhost/%2F")
val conn = system.actorOf(ConnectionOwner.props(connFactory, 1 second))
val producer = ConnectionOwner.createChildActor(conn, ChannelOwner.props())
class Root extends Actor {
context.system.scheduler.schedule(5 milliseconds, 1 seconds, self, 'publish)
producer ! AddStatusListener(self)
def receive = disconnected
def disconnected: Receive = {
case Connected => context.become(connected)
case 'publish => println("cannot publish message in disconnected state")
}
def connected: Receive = {
case Disconnected => context.become(disconnected)
case 'publish => producer ! Publish("", "my_queue", "test".getBytes("UTF-8"))
}
}
val root = system.actorOf(Props[Root])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment