6

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!

Giovanni Di Toro
  • 797
  • 1
  • 14
  • 34
Mig
  • 63
  • 1
  • 3

2 Answers2

11

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

ScreenShot

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

Docs here.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks. If anyone else is interested, here is the ticket: https://jira.spring.io/browse/AMQP-493 – Mig Apr 22 '15 at 20:21
  • 1
    With the ticket closed, could you please complete your answer with a small example? – watery Nov 02 '18 at 11:45
  • What if you have multiple consumers and you want unique tags for each, let's say "consumer.foo.1, consumer.foo.2..."(not the default ones)? – B. Bal Dec 09 '20 at 10:12
  • `q -> "myConsumerFor." + q + "." + counter.incrementAndGet())` where `counter` is an `AtomicInteger`. – Gary Russell Dec 09 '20 at 14:30
  • Check my alternative which is consider better in the other [thread](https://stackoverflow.com/questions/53119596/how-to-set-amqp-rabbitmq-consumer-tag-in-spring-boot/75784413#75784413) – Majlanky Mar 19 '23 at 19:20
0

It works for the basic amqp java library as well (for those without spring-amqp). When calling the basicConsume method, it can receive a String parameter that will represent the consumer tag to be used. If it's empty or null, it will use a server generated one. You should be careful to use server-wide unique tags, as cancelling a consumer with a duplicate tag might have unwanted behavior.

channel.basicConsume(queueName, autoAck, "specific.tag."+System.currentTimeMillis(), deliverCallback);
Adrian B.
  • 1,592
  • 1
  • 20
  • 38