Destinations, as ConnectionFactory instances, are JMS administered objects that you can store and retrieved in JNDI. When configuring a Spring application context, you can use the JNDI JndiObjectFactoryBean factory class or to perform dependency injection on your object’s references to JMS destinations.
However, this strategy is often cumbersome if there are a large number of destinations in the application or if there are advanced destination management features unique to the JMS provider.
问题是:
当我的申请中有大量目的地时如何继续?
使用上面提到的策略,我必须定义:
- Jndi模板
- JndiDestinationResolver
- JndiObjectFactoryBean
- 缓存连接工厂
- JmsTemplate
对于每个目的地。
所以如果我有 20 个队列,我就必须定义 100 个这样的 bean...
最佳答案
Spring 文档中的注释注释了“对目标端点使用 JNDI”与“对目标端点不使用 JNDI”。那么就您而言 - 您的目的地是否存储在 JNDI 中?如果您不需要使用它,请忘记它。仅从 JNDI 加载 ConnectionFactory(一个对象)或从头开始创建它。
这样您就不必为每个目标分配一个 Spring bean。您可以只有一个 Java“消费者 bean”,然后它使用 JmsTemplate。我猜您的连接工厂是相同的,因此只有一个 new JmsTemplate(connectionFactory)
。然后根据需要进行createSession/createConsumer等操作。
关于java - Spring JMS 定义多个目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55460921/