Created
June 29, 2017 06:17
-
-
Save anonymous/1efbe25819b98d4864cd5c5d36fc2a98 to your computer and use it in GitHub Desktop.
the description for this gist
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
class ExchangeConsumer(connection: Connection, exchange: String) | |
extends Consumer[OutboundMessage, Unit] { | |
override def createSubscriber( | |
cb: Callback[Unit], | |
s: Scheduler | |
): (Subscriber[OutboundMessage], AssignableCancelable) = { | |
val ch = connection.createChannel() // (1) | |
val subscriber = | |
new Subscriber[OutboundMessage] { | |
implicit val scheduler: Scheduler = s // (2) | |
private val properties = new AMQP.BasicProperties() // (3) | |
private def publish(m: OutboundMessage): Unit = | |
ch.basicPublish(exchange, m.routingKey, properties, m.body) // (4) | |
override def onNext(m: OutboundMessage): Future[Ack] = | |
Future { | |
blocking(publish(m)) | |
Continue // (5) | |
} | |
override def onError(ex: Throwable): Unit = { | |
abort() | |
cb.onError(ex) // (6) | |
} | |
override def onComplete(): Unit = { | |
abort() | |
cb.onSuccess(()) // (6) | |
} | |
//Checks if is open to mitigate risk of killing connection | |
private def abort(): Unit = if (ch.isOpen) ch.abort() | |
} | |
(subscriber, AssignableCancelable.dummy) // (7) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment