19

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?

user468587
  • 4,799
  • 24
  • 67
  • 124

1 Answers1

20

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) {
    ...
}    
Apokralipsa
  • 2,674
  • 17
  • 28
  • 3
    @RabbitListener(queues = "${queue}")) have extra closing bracket! Please remove, I dont have edit permission :) – Vijay Mohan Sep 16 '17 at 19:15
  • Good catch, removed :) – Apokralipsa Sep 17 '17 at 05: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 – Stefanos Kargas Apr 14 '20 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 '23 at 23:22