Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel and Activemq setup with Spring Boot

I have noticed in several examples, the common way to configure activemq with camel is with the following beans. I would like to know if Spring Boot already configures any of these beans by default. I know that if the activemq jars are on the class path a default connection factory is created, but what about everything below?

<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>

or

@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);
    }
like image 200
zachariahyoung Avatar asked Nov 26 '15 00:11

zachariahyoung


People also ask

What is ActiveMQ in Apache Camel?

The ActiveMQ component is an extension to the JMS component and has been pre-configured for using Apache ActiveMQ 5. x (not Artemis). Users of Apache ActiveMQ Artemis should use the JMS component. More documentation. See the JMS component for more documentation and examples.

What is camel spring boot starter?

Spring Boot component provides auto-configuration for Apache Camel. Our opinionated auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities (like producer template, consumer template and the type converter) as beans.

What is camel JMS?

This component allows messages to be sent to (or consumed from) a JMS Queue or Topic. It uses Spring's JMS support for declarative transactions, including Spring's JmsTemplate for sending and a MessageListenerContainer for consuming.


1 Answers

Meanwhile there are some spring-boot-starters which can be used to have ActiveMQ and Camel running within Spring Boot.

ActiveMQ

Start with spring-boot-starter-activemq in your pom:

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

Configuration

Have a look what's configurable through this at all - its documented in Appendix A. Common application properties (search for 'activemq' and 'jms').

Alternative approach: from my point of view, its best to determine what's configurable in Sprint Boot and what not is having a look at their auto-configuration mechanism:

  • ActiveMQAutoconfiguration which shows how ActiveMQ and JMS Configuration relate to each other
  • ActiveMQProperties determining ActiveMQ-specific properties which can be set

Camel

Apache Camel provides its own Spring Boot integration. Basically you also have to add camel-spring-boot-starter:

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

Configuration

I haven't found a good example configuration file, so again, have a look at configuration is exposed through CamelConfigurationProperties class.

In general - like you mentioned - you might end up in registering some of your beans manually if you don't find all properties exposed via this configuration.

like image 131
gtonic Avatar answered Oct 06 '22 00:10

gtonic