Created
April 3, 2014 16:02
-
-
Save sstone/9957280 to your computer and use it in GitHub Desktop.
Basic status listener sample
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 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