22

I have application.properties file for spring app which contains some simple properties:

queue=my.test.q

in the java code i need to specify the queue to @RabbitListener:

@Component
public class Handler {    
    @RabbitListener(queues = "my.test.q")
    public void handleMessage(Message message) {
       ...
    }

that would work, but i want to pass parameter to the annotation, i've tried the following but none of them worked.

    @Component
    public class Handler {
        @Value("${queue}")
        private String queueName;

        @RabbitListener(queues = @Value("${queue}")  <-- not working
        @RabbitListener(queues = queueName))     <--- not working
        public void handleMessage(Message message) {
           ...
        }    

is it possible to that?

1 Answer 1

21

As you can see in the javadoc for the @RabbitListener annotation, the queues attribute is a table of Strings, so you cannot assign an annotation to it. You also cannot assign variables to annotation attributes at all in Java, as they are required to be compile constants.

I cannot test it right now, but the javadoc seems to suggest that this should work (note, that it says it may return SpEL expressions):

@RabbitListener(queues = "${queue}")     
public void handleMessage(Message message) {
    ...
}    
4
  • 3
    @RabbitListener(queues = "${queue}")) have extra closing bracket! Please remove, I dont have edit permission :) Sep 16, 2017 at 19:15
  • Good catch, removed :) Sep 17, 2017 at 5:56
  • This suggestion works indeed. You just put in the brackets an application property (works with dots also, e.g. my.application.property) and it works as a placeholder Apr 14, 2020 at 19:34
  • This method also worked for spring kafka. @KafkaListener(topics = "${application.topic}", groupId = "${spring.kafka.consumer.group-id}") public void listenWithHeaders(...)
    – asokan
    Jan 22, 2023 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.