Search code examples
javaspring-bootjmsspring-jmsspring-annotations

How to add different destination dynamically to JMSListener Annotation in Spring boot?


I working on an application which reads message from Azure service bus. This application was created using spring boot, Spring jms and Qpid jms client. I am able to read the message properly from Queue without any issues. PFB My code which I am using to read message.

@Service
public class QueueReceiver {
@JmsListener(destination = "testing")
public void onMessage(String message) {
    if (null != message) {
        System.out.println("Received message from Queue: " + message);
    }
}}

Issue is we have different destinations for different environemnts, like testing for dev, testing-qa for qa and testing-prod for production, all these values are provided as azure.queueName in different application-(ENV).proerpties respectively. I want to pass these destinations dynamically to the destination in JmsListener Annotation. When i try using

@Value("${azure.queueName}")
private String dest;

and passing dest to annotation like @JmsListener(destination = dest)

I am getting The value for annotation attribute JmsListener.destination must be a constant expression Error. After googling with this Error i found that we cannot pass dynamic value to Annotation. Please help me how to resolve this issue or any other solution for this.


Solution

  • Use

    destination="${azure.queueName}"
    

    i.e. put the placeholder in the annotation directly.