SpinGo / op-rabbit

The Opinionated RabbitMQ Library for Scala and Akka
Other
232 stars 73 forks source link

Consume message from exchange using op rabbit? #136

Closed falguniChitkara closed 6 years ago

falguniChitkara commented 6 years ago

We have topic/direct exchange created named "abcExchange". A queue "abcQueue" is binded to the above exchange with routing key "abcRoutingKey".

Can we consume the message from the above queue ,using "abcExchange" and "abcRoutingKey"? If yes then how?

DStranger commented 6 years ago

@falguniChitkara something like this should work for a direct exchange:

import com.spingo.op_rabbit._
import Directives._

Subscription.run(rabbitControl) {
  channel() {
    consume(Binding.direct(queue("abcQueue"), Exchange.direct("abcExchange"), List("abcRoutingKey"))) {
      body(as[String]) { _ => ack }
    }
  }
}
falguniChitkara commented 6 years ago

@DStranger the exchange i am using is topic exchange,and the queue name is not known to me. Is it possible for me to connect to rabbit mq without knowing queue name i.e. connect via exchange name and routing key?

falguniChitkara commented 6 years ago

@DStranger I am able to connect to topic ,with routing key using below code ,

 val subscriptionRef = Subscription.run(rabbitControl) {
       // A qos of 3 will cause up to 3 concurrent messages to be processed at any given time.

  channel(qos = 3) {
   consume(topic(Queue.passive("abcQueue), List("abcRoutingKey") , Exchange.topic("abcExchange", false, false, Seq())))
    {
      (body(as[String])) { (item) =>
        /* do work; this body is executed in a separate thread, as
           provided by the implicit execution context */
           println(item)
        ack
      }
    }
  }