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

馋奶兔 提交于 2019-12-11 07:55:24

问题


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.


回答1:


Use

destination="${azure.queueName}"

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




回答2:


You can use a dynamic name as defined in the application.properties file.For Example:

@JmsListener(destination = "${queue.name}")

Since you can't access any class variables here so this is the best option available.



来源:https://stackoverflow.com/questions/42532613/how-to-add-different-destination-dynamically-to-jmslistener-annotation-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!