[英]How to add different destination dynamically to JMSListener Annotation in Spring boot?
I working on an application which reads message from Azure service bus.我正在开发一个从 Azure 服务总线读取消息的应用程序。 This application was created using spring boot, Spring jms and Qpid jms client.该应用程序是使用 spring boot、Spring jms 和 Qpid jms 客户端创建的。 I am able to read the message properly from Queue without any issues.我能够从 Queue 正确读取消息,没有任何问题。 PFB My code which I am using to read message. PFB 我用来阅读消息的代码。
@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.问题是我们对不同的环境有不同的目的地,比如testing
dev
, testing-qa
qa
和testing-prod
production
,所有这些值都分别作为azure.queueName
在不同的 application-(ENV).properpties 中提供。 I want to pass these destinations dynamically to the destination in JmsListener Annotation.我想将这些目的地动态传递到 JmsListener Annotation 中的目的地。 When i try using当我尝试使用
@Value("${azure.queueName}")
private String dest;
and passing dest to annotation like @JmsListener(destination = dest)
并将 dest 传递给像@JmsListener(destination = dest)
这样的注释
I am getting The value for annotation attribute JmsListener.destination must be a constant expression
Error.我得到The value for annotation attribute JmsListener.destination must be a constant expression
错误。 After googling with this Error i found that we cannot pass dynamic value to Annotation.在谷歌搜索此错误后,我发现我们无法将动态值传递给 Annotation。 Please help me how to resolve this issue or any other solution for this.请帮助我如何解决此问题或任何其他解决方案。
Use用
destination="${azure.queueName}"
ie put the placeholder in the annotation directly.即直接将占位符放在注释中。
You can use a dynamic name as defined in the application.properties file.For Example:您可以使用 application.properties 文件中定义的动态名称。例如:
@JmsListener(destination = "${queue.name}")
Since you can't access any class variables here so this is the best option available.由于您无法在此处访问任何类变量,因此这是可用的最佳选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.