使用Spring Boot设置Camel和Activemq

12

我注意到在几个例子中,配置activemq与camel的常用方法是使用以下bean。 我想知道Spring Boot是否默认配置了这些bean中的任何一个。 我知道如果activemq jars在类路径上,将创建默认连接工厂,但下面的所有内容呢?

<bean id="jmsConnectionFactory"
        class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616"/>
  </bean>

  <bean id="pooledConnectionFactory"
        class="org.apache.activemq.pool.PooledConnectionFactory"
        init-method="start" destroy-method="stop">
    <property name="maxConnections" value="8"/>
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
  </bean>

  <bean id="jmsConfig"
        class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory"/>
    <property name="concurrentConsumers" value="10"/>
  </bean>

  <bean id="jms"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig"/>
    <property name="transacted" value="true"/>
    <property name="cacheLevelName" value="CACHE_CONSUMER"/>
  </bean>
或者
@Bean
    public ActiveMQConnectionFactory getConnectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerURL);
        return connectionFactory;
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public PooledConnectionFactory getPooledConnectionFactory() {
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setMaxConnections(maxConnections);
        pooledConnectionFactory.setConnectionFactory(getConnectionFactory());
        return pooledConnectionFactory;
    }

    @Bean
    public JmsConfiguration getJmsConfiguration() {
        JmsConfiguration jmsConfiguration = new JmsConfiguration();
        jmsConfiguration.setConnectionFactory(getPooledConnectionFactory());
        return jmsConfiguration;
    }

    @Bean
    public JmsConfiguration getJmsHighPriorityConfiguration() {
        JmsConfiguration jmsConfiguration = new JmsConfiguration();
        jmsConfiguration.setConnectionFactory(getPooledConnectionFactory());
        jmsConfiguration.setPriority(8);
        return jmsConfiguration;
    }

    @Override
    protected void setupCamelContext(CamelContext camelContext) throws Exception {
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConfiguration(getJmsConfiguration());
        camelContext.addComponent("activemq", activeMQComponent);

        ActiveMQComponent activeMQHighPriorityComponent = new ActiveMQComponent();
        activeMQHighPriorityComponent.setConfiguration(getJmsHighPriorityConfiguration());
        camelContext.addComponent("activemq-high-priority", activeMQHighPriorityComponent);
    }
1个回答

3

同时,有一些 spring-boot-starters 可以用来在 Spring Boot 中运行 ActiveMQ 和 Camel。

ActiveMQ

在 pom.xml 文件中加入 spring-boot-starter-activemq 即可开始使用:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

配置

首先,请参考附录A.公共应用程序属性(搜索“activemq”和“jms”),查看可以通过配置修改哪些内容。

另一种方法是查看他们的auto-configuration机制,并确定哪些内容在Sprint Boot中可配置,哪些不可配置:

Camel

Apache Camel提供了自己的Spring Boot集成。基本上你只需要添加camel-spring-boot-starter即可:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>2.17.3</version>
</dependency>

配置

我没有找到一个好的示例配置文件,因此,建议查看CamelConfigurationProperties类来了解配置是如何公开的。

总的来说 - 如你所提到的 - 如果你在这个配置中没有发现所有属性,可能需要手动注册自己的某些bean。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接