given this code
@Bean
open fun exchange(): TopicExchange {
return TopicExchange("amq.topic")
}
@RabbitListener(queues = [Amqp.BODY_WEIGHT_NEW])
open fun record(entity: Collection<BodyWeight>) {
log.trace("saving: {}", entity)
}
@Bean
open fun weight(): Queue {
return Queue(Amqp.BODY_WEIGHT_NEW)
}
@Bean
open fun bindWeight(): Binding {
return BindingBuilder.bind(weight()).to(exchange).with(Amqp.BODY_WEIGHT_NEW)
}
is it possible to reduce my @Bean declarations using @RabbitListener
? I know I can list the queue as a queue to declare, but I'm not sure on what the bindings would look like.
tried this
@RabbitListener(bindings = [QueueBinding(value = Queue(Amqp.BODY_WEIGHT_NEW), exchange = Exchange("amqp.topic"))])
but it doesn't seem to bind to the exchange in the same way, as my tests aren't passing.
You need to add the routing key to the @QueueBinding
...
key = Amqp.BODY_WEIGHT_NEW
(that's what the with
clause on the BindingBuilder
does).
EDIT
Works fine for me...
@SpringBootApplication
open class So55928905Application {
@RabbitListener(bindings = [QueueBinding(value = Queue("foo"),
exchange = Exchange("amqp.topic"),
key = ["foo"])])
fun `in`(`in`: String) {
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(So55928905Application::class.java, *args)
}
}
}
EDIT2
The problem is your exchange name MQTT publishes to the standard amq.topic
not amqp.topic
.
@SpringBootApplication
open class So55928905Application {
@RabbitListener(bindings = [QueueBinding(value = Queue("foo"),
exchange = Exchange(name = "amq.topic", type = "topic"),
key = ["foo"])])
fun listen(string: String) {
println(string)
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(So55928905Application::class.java, *args)
}
}
}