How to set the consumer-tag value in spring-amqp

天大地大妈咪最大 提交于 2019-12-10 15:46:07

问题


I am trying to update the consumer-tag to be more informative than the randomly generated string. We have a pattern which we use that includes hostname + identifier + randomized string. This works fine in our other services (ie: NodeJS with ampqlib) because they provide a mechanism to pass in this value.

However, for our Java services, we use spring-amqp and it looks like there is no way to pass in a consumer-tag value. I took a look at BlockingQueueConsumer and it is currently hard-coded to an empty string:

String consumerTag = this.channel.basicConsume(queue, this.acknowledgeMode.isAutoAck(), "", false, this.exclusive,
            this.consumerArgs, this.consumer);

Is there any way to get it not to be an empty string (which will result in a randomly generated one) besides creating our own type of consumer?

Thanks!


回答1:


You are correct; it's not currently configurable; please open an Improvement JIRA and we'll take a look at adding it. It shouldn't take much effort.

EDIT

If using @RabbitListener, simply add the strategy implementation to the listener container factory; it's a @FunctionalInterface so you can use a lambda, for example, with Spring Boot:

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
        SimpleRabbitListenerContainerFactoryConfigurer configurer,
        ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    factory.setConsumerTagStrategy(q -> "myConsumerFor." + q);
    return factory;
}

@RabbitListener(queues = "foo")
public void listen(String in) {
    System.out.println(in);
}

and

If wiring up the container directly, simply add it to the container.

Docs here.



来源:https://stackoverflow.com/questions/29801873/how-to-set-the-consumer-tag-value-in-spring-amqp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!