【问题标题】:Can multiple ActiveMQ instances be used with Camel and Spring boot?多个 ActiveMQ 实例可以与 Camel 和 Spring boot 一起使用吗?
【发布时间】:2021-06-11 20:52:53
【问题描述】:

我的项目有以下设置:

一个 Spring Boot 应用程序,带有 ActiveMQ 和 Apache Camel。
Camel 用于完成所有的配置和实例化。 这工作正常,但现在应该添加另一个 ActiveMQ,带有另一个队列和 url 代理(仅用作消费者)。

在配置过程中,org.apache.camel.jms.JmsComponent 通过检索 ConnectionFactory 类型的所有类来获取 JMSConfiguration。 更具体:致电applicationContext.getBeansOfType(ConnectionFactory.class);

这将返回一个 Map<String, ConnectionFactory>,其中包含 ConnectionFactories 及其名称 (=keys)。但是总是返回第一个 ConnectionFactory。 而且因为在项目中定义了2个ConnectionFactories,所以会导致报错。

有没有办法指出哪个 ConnectionFactory 应该在 Camel 中一起使用? 以下代码来自 org.apache.camel-jms.JmsComponent,以及行

ConnectionFactory cf = beansOfTypeConnectionFactory.values().iterator().next();

导致问题,因为它总是需要第一个 ConnectionFactory。

public JmsConfiguration getConfiguration() {
    if (configuration == null) {
        configuration = createConfiguration();

        // If we are being configured with spring...
        if (applicationContext != null) {

            if (isAllowAutoWiredConnectionFactory()) {
                Map<String, ConnectionFactory> beansOfTypeConnectionFactory = applicationContext.getBeansOfType(ConnectionFactory.class);
                if (!beansOfTypeConnectionFactory.isEmpty()) {
                    ConnectionFactory cf = beansOfTypeConnectionFactory.values().iterator().next();
                    configuration.setConnectionFactory(cf);
                }
            }

            if (isAllowAutoWiredDestinationResolver()) {
                Map<String, DestinationResolver> beansOfTypeDestinationResolver = applicationContext.getBeansOfType(DestinationResolver.class);
                if (!beansOfTypeDestinationResolver.isEmpty()) {
                    DestinationResolver destinationResolver = beansOfTypeDestinationResolver.values().iterator().next();
                    configuration.setDestinationResolver(destinationResolver);
                }
            }
        }
    }
    return configuration;
}

我的项目中使用 Camel 进行设置的配置:

@Configuration
public class CamelContextConfiguration extends CamelConfiguration {

    @Override
    protected void setupCamelContext(CamelContext camelContext) throws Exception {
        super.setupCamelContext(camelContext);

        MyProjectDataFormat myprojectDataFormat = new MyProjectDataFormat();
        DataFormatDefinition projectDfd = new DataFormatDefinition(myprojectDataFormat);
        camelContext.getDataFormats().put("myproject", projectDfd);
    }
}

【问题讨论】:

    标签: spring-boot apache-camel activemq


    【解决方案1】:

    您可以创建 Camel JMS 组件的两个实例,例如:

    @Bean 
    JmsComponent jms1() {
        JmsComponent component = new JmsComponent();
        component.setConnectionFactory(...));
        
        return component;
    }
    @Bean 
    JmsComponent jms2() {
        JmsComponent component = new JmsComponent();
        component.setConnectionFactory(...));
        
        return component;
    }
    

    然后在你的路由中引用新的组件,例如:

    from("jms1:...")
        .to("jms2:...")
    

    另一个选项是使用 connectionFactory 选项:

    from("jms1:...?connectionFactory=#cf1")
        .to("jms2:...?connectionFactory=#cf2")
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2018-07-16
      • 2019-02-21
      • 2019-12-21
      • 1970-01-01
      • 2015-12-31
      • 2015-09-03
      • 1970-01-01
      相关资源
      最近更新 更多