是否可以用RabbitListener声明这些绑定?

时间:2019-04-30 21:31:42

标签: spring-boot kotlin spring-amqp

给出此代码

List<String> words = new ArrayList<>(Arrays.asList(phrase.split("\\s+")));
Map<String, Integer> tokenMap = IntStream.range(0, words.size())
            .boxed()
            .collect(Collectors.toMap(words::get, i -> i));

    @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) } 来减少我的@Bean声明?我知道我可以将队列列为要声明的队列,但是我不确定绑定的外观。

尝试了

@RabbitListener

但是它似乎并没有以相同的方式绑定到交换,因为我的测试没有通过。

1 个答案:

答案 0 :(得分:1)

您需要将路由密钥添加到@QueueBinding ...

key = Amqp.BODY_WEIGHT_NEW

with上的BindingBuilder子句就是这样做的。)

编辑

对我来说很好...

@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)
        }
    }

}

enter image description here

EDIT2

问题是您的交换名称MQTT发布到标准amq.topic而不是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)
        }
    }

}