如何在spring-amqp中设置消费者标签值 [英] How to set the consumer-tag value in spring-amqp

查看:222
本文介绍了如何在spring-amqp中设置消费者标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将消费者标签更新为比随机生成的字符串更具信息性的标签.我们使用的模式包括主机名+标识符+随机字符串.在我们的其他服务(即带有ampqlib的NodeJS)中,这可以很好地工作,因为它们提供了一种传递此值的机制.

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.

但是,对于我们的Java服务,我们使用spring-amqp,似乎无法传递消费者标签值.我看了BlockingQueueConsumer,目前它已硬编码为一个空字符串:

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?

谢谢!

推荐答案

您是正确的;目前无法配置;请打开改进JIRA ,我们将介绍添加它.不需要太多的努力.

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.

编辑

如果使用@RabbitListener,只需将策略实现添加到侦听器容器工厂;它是一个@FunctionalInterface,因此您可以在Lambda中使用它,例如,在Spring Boot中使用:

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

如果直接连接容器,只需将其添加到容器中即可.

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

此处的文档.

这篇关于如何在spring-amqp中设置消费者标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆