Spring-动态创建JMSTemplates [英] Spring - Dynamically create JMSTemplates

查看:194
本文介绍了Spring-动态创建JMSTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring boot,并且我想动态创建多个JMS模板,因为我想连接到不同的JMS实例.我知道使用注释的标准方法,该方法将ConnectionFactory链接到JMSTemplate.我希望每个不同的JMS实例都有一个不同的JMSTemplate.

I am using Spring boot, and I would like to dynamically create multiple JMS Templates, as I would like to connect to different JMS instances. I am aware of the standard approach of using annotations, linking the ConnectionFactory to the JMSTemplate. I would expect a different JMSTemplate to each different JMS Instance.

我已将连接详细信息当前加载到Map中,我想为每个键动态创建一个JMSTemplate.

I have the connection details currently loaded into a Map, I would like to dynamically create a JMSTemplate for each key.

主要目的是基于地图查找将消息发布到主题目的地.但是对于测试来说,也可以有听众.

The main purpose is to publish messages to Topic destinations based on a map lookup. But for testing, it would be could to have listeners as well.

可能的方法:

1)将JMSTemplates添加到与Spring Component关联的映射,并在spring组件Map上查找JMSTemplate.

1) add the JMSTemplates to a map that is associated with a Spring Component, and lookup the JMSTemplate on spring component map.

2)使用以下方法动态注册多个jmsTemplate bean:

2) what about dynamically registering multiple jmsTemplate beans using:

BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JmsTemplate.class); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JmsTemplate.class); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

         builder.addPropertyValue("connectionFactory", cachingConnectionFactory);      // set property value
        DefaultListableBeanFactory factory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
        factory.registerBeanDefinition("jmsTemplateName", builder.getBeanDefinition());

然后查找jms bean-> ctx.getBean("jmsTemplateName");

Then lookup the jms bean -> ctx.getBean("jmsTemplateName");

哪种方法比较有利?

请告知.

B

推荐答案

因此,如果地图中有ConnectionFactory个对象,则可以创建JmsTemplate s的相似地图.使用此 JmsTempalte构造器,并且在一个循环(或流)之后,您可以具有JmsTemplate s的映射.因此发送很容易.

So if you have ConnectionFactory objects in a map, you can create similar map of JmsTemplates. Use this JmsTempalte contructor and after one loop (or stream) and you can have map of JmsTemplates. So sending is easy.

更难的部分将是听众.如果您有动态目标,则需要忽略侦听器注释(@JmsListener).您可以通过创建DefaultMessageListenerContainer的地图来使其正常工作.

Harder part would be listeners. If you have dynamic destinations, you need to forget about listener annotations (@JmsListener). You can make it working by creating map of DefaultMessageListenerContainer.

每个容器都将通过以下方式创建:

Each container will be created by something like:

DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setDestination(topic);
container.setMessageListener(listenerInstance);
container.start();

您还可以通过停止,关闭...来控制每个侦听器容器的生命周期.

You can also control lifecycle of each listener container via stop, shutdown, ....

但是请记住,以这种方式处理许多队列可能会占用大量资源.另外,您可能需要小心自己关闭资源(Spring不会为您这样做).

But bear in mind that handling a lot of queues this way may be resource intensive. Also you will need to take care about closing resources yourself probably (Spring wouldn't do it for you).

这篇关于Spring-动态创建JMSTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆