You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by zpyoung <zp...@gmail.com> on 2015/11/29 19:01:46 UTC

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);
    }



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-and-Activemq-setup-with-Spring-Boot-tp5774544.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel and Activemq setup with Spring Boot

Posted by Henryk Konsek <he...@gmail.com>.
Hi,

Spring Boot creates connectionFactory bean for you. It is named
"jmsConnectionfactory" [1]. In order to wire Camel with Spring Boot AMQ
support, you have to refer that bean from JMS component URI:

  from("jms:queue?connectionFactory=#jmsConnectionfactory").to(...);

Simple as that :)

Cheers!

[1]
https://github.com/spring-projects/spring-boot/blob/90f7bc03216c709634ea7ffd3832482e25e0ace3/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java#L45

sob., 5.12.2015 o 15:01 użytkownik zpyoung <zp...@gmail.com> napisał:

> Really just trying to make sure I understand how the configuration works.
> If
> I auto configure activemq with Spring Boot, I have noticed that an activemq
> component is also created in the camel context.  Is Spring Boot creating
> this component or the camel context?
>
> So when I need to have a component configured differently, should I make
> the
> configuration changes on the endpoint or just create a different component?
>
> Are there really any differences between these two configurations?
>
> -----------------------------------
> create new component to handle transaction.
>    @Bean
>     public ActiveMQComponent activemqtx(PooledConnectionFactory
> pooledConnectionFactory) {
>         ActiveMQComponent activeMQComponent = new ActiveMQComponent();
>         activeMQComponent.setTransacted(true);
>         activeMQComponent.setTransactionManager(new
> JmsTransactionManager(pooledConnectionFactory));
>         return activeMQComponent;
>     }
>
>    from("activemq:TRIGGER").routeId("Trigger")
>                 .onException(Exception.class)
>                     .maximumRedeliveries(2)
>                     .to("activemq:EXCEPTION")
>                     .markRollbackOnlyLast().end()
>                 .transacted()
>                 .bean("jpaRepository")
>                 .to("log:out");
>
> Or just configure the enpoint.
>
>    from("activemq:TRIGGER?transacted=true").routeId("Trigger")
>                 .onException(Exception.class)
>                     .maximumRedeliveries(7)
>                     .to("activemq:EXCEPTION")
>                     .markRollbackOnlyLast().end()
>                 .transacted()
>                 .bean("jpaRepository")
>                 .to("log:out");
>
>
> I'm guessing that creating a component allows for better reuse if you have
> several routes using the same configuration.
>
> Sample code I have been testing with
> https://github.com/zachariahyoung/docker-camel
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-and-Activemq-setup-with-Spring-Boot-tp5774544p5774728.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
-- 
Henryk Konsek
http://about.me/hekonsek

Re: Camel and Activemq setup with Spring Boot

Posted by zpyoung <zp...@gmail.com>.
Really just trying to make sure I understand how the configuration works.  If
I auto configure activemq with Spring Boot, I have noticed that an activemq
component is also created in the camel context.  Is Spring Boot creating
this component or the camel context?

So when I need to have a component configured differently, should I make the
configuration changes on the endpoint or just create a different component?

Are there really any differences between these two configurations?

-----------------------------------
create new component to handle transaction.
   @Bean
    public ActiveMQComponent activemqtx(PooledConnectionFactory
pooledConnectionFactory) {
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setTransacted(true);
        activeMQComponent.setTransactionManager(new
JmsTransactionManager(pooledConnectionFactory));
        return activeMQComponent;
    }

   from("activemq:TRIGGER").routeId("Trigger")
                .onException(Exception.class)
                    .maximumRedeliveries(2)
                    .to("activemq:EXCEPTION")
                    .markRollbackOnlyLast().end()
                .transacted()
                .bean("jpaRepository")
                .to("log:out");

Or just configure the enpoint.

   from("activemq:TRIGGER?transacted=true").routeId("Trigger")
                .onException(Exception.class)
                    .maximumRedeliveries(7)
                    .to("activemq:EXCEPTION")
                    .markRollbackOnlyLast().end()
                .transacted()
                .bean("jpaRepository")
                .to("log:out");


I'm guessing that creating a component allows for better reuse if you have
several routes using the same configuration.

Sample code I have been testing with
https://github.com/zachariahyoung/docker-camel




--
View this message in context: http://camel.465427.n5.nabble.com/Camel-and-Activemq-setup-with-Spring-Boot-tp5774544p5774728.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel and Activemq setup with Spring Boot

Posted by Joakim Bjørnstad <jo...@gmail.com>.
Hello,

I'd check out the source of JmsAutoConfiguration and
ActiveMQAutoConfiguration to see what Spring Boot autoconfigures.
Essentially everything under the
org.springframework.boot.autoconfigure package.

For example ActiveMQ can be configured by supplying ActiveMQProperties
through Spring Boot externalized configuration.
spring.activemq.brokerUrl
spring.activemq.inMemory
spring.activemq.pooled
spring.activemq.user
spring.activemq.password

So you can probably cut a bean or two if you go that way. Also IMO not
necessary to explicitly add components to the camel context. Register
a Component bean in Spring and you can refer to it by methodname in
the camel URI. It gets automatically registered.

e.g.:

@Bean
@Inject
public JmsComponent myjms(ConnectionFactory myConnectionFactory) {
  JmsComponent jmsComponent = new JmsComponent();
  jmsComponent.setConnectionFactory(myConnectionFactory);
  return jmsComponent;
}

The component should automatically be available in Camel (same should
go for ActiveMQComponent): myjms:MY_QUEUE?concurrentConsumers=10

But what is the issue really, just clean up Spring config or is there
something not working with your config ?


On Fri, Dec 4, 2015 at 5:12 AM, zpyoung <zp...@gmail.com> wrote:
> Any help on this?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-and-Activemq-setup-with-Spring-Boot-tp5774544p5774651.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Kind regards
Joakim Bjørnstad

Re: Camel and Activemq setup with Spring Boot

Posted by zpyoung <zp...@gmail.com>.
Any help on this?



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-and-Activemq-setup-with-Spring-Boot-tp5774544p5774651.html
Sent from the Camel - Users mailing list archive at Nabble.com.