【发布时间】:2014-11-12 01:01:42
【问题描述】:
我想将 Spring 4.1 中提供的新注释和特性用于需要 JMS 侦听器的应用程序。
我已经仔细阅读了Spring 4.1 JMS improvements post 中的注释,但我仍然想念@JmsListener 和DestinationResolver 之间的关系,以及我将如何设置应用程序以指示正确的Destination 或Endpoint .
这是@JmsListener 的建议用法
@Component
public class MyService {
@JmsListener(containerFactory = "myContainerFactory", destination = "myQueue")
public void processOrder(String data) { ... }
}
现在,我不能在我的实际代码中使用它,因为需要使用 Environment.getProperty() 从配置文件中读取“myQueue”。
我可以使用DestinationResolver 设置一个适当的 myContainerFactory,但大多数情况下,如果您不需要 JNDI 在应用服务器中查找队列并且不需要做一些自定义,您似乎只需使用DynamicDestinationResolver回复逻辑。我只是想了解 Spring 如何让我使用 @JmsListener 注释以参数化方式指示队列的名称。
在博客文章的后面,我找到了对这个配置器的引用:
@Configuration
@EnableJms
public class AppConfig implements JmsListenerConfigurer {
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
registrar.setDefaultContainerFactory(defaultContainerFactory());
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setDestination("anotherQueue");
endpoint.setMessageListener(message -> {
// processing
});
registrar.registerEndpoint(endpoint);
}
现在,这有点道理,我可以看到这将允许我在运行时从一些外部字符串设置目标,但这似乎与使用 @JmsListener 冲突,因为它似乎覆盖了在上面的代码中注释有利于endpoint.setMessageListener。
关于如何使用@JmsListener 指定适当队列名称的任何提示?
【问题讨论】:
-
使用占位符。
destination="${name.of.your.property}". -
我宁愿不使用
PropertyPlaceHolderConfiguration来支持Environment.getProperty,但显然我不能将该路由作为注释的参数。 -
实际上,您将使用
PropertySourcePlaceHolderConfigurer,它与Environment.getProperty的作用基本相同(它会咨询所有PropertySources。您能否详细说明为什么您更喜欢Environment.getProperty而不是占位符这个案子? -
为什么
@PropertySource不能与占位符一起使用?它的工作原理相同。唯一的区别是您需要使用PropertySourcesPlaceHolderConfigurer。因此,使用占位符这一事实并不意味着您不能再使用@PropertySource。 -
我在这里与@M.Deinum +1,并有兴趣了解使用占位符的问题。
标签: spring spring-annotations spring-jms