Search code examples
javaamqpspring-amqp

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


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!


Solution

  • 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.