Spring Booted Camel Ride

My Camel was feeling the pain on his legs for longer rides 😉 , so thought to give it some soothing with Spring Boot, wondering what am talking about ????, Let me get straight to the point – there are lot of cool things that has come as part of Camel 2.18, and my favourite being the *-starters to Camel Components, you can check more on Spring Boot Starters on what they mean

Lets take a simple camel example of FTP processing and pumping the message to the Messaging Queue. We use embedded ActiveMQ(AMQ) server for this example. So to do that the high level steps are:

  1. Define a FTP consumer
  2. Define a Processor – this does not do much except for doing a log of processed file name
  3. Takes the message and drops to AMQ queue called jms:queue:incomingorders

That’s all we are going to do, the example FTP/JMS router example without spring boot is available here.

So here are few things I did not find intuitive in traditional way even for this simplest example:

  • Define a bunch of beans and configurations on Camel Route/ Context XML builder to setup Broker, ConnectionFactory etc.,
  • Auto configuration of AMQ – in this case its a in-memory AMQ which has some sane defaults and still I need to define and configure it
  • I would prefer to use YAML or JSON based properties rather than stereotype properties file to provide better readability, structuring etc.,
  • Defining the beans in XML to be used on Camel Context though we can use context-scan and package-scan elements to camel Context XML

Since Spring Boot  offers lots of auto configuration1, which I can safely call as convention over configuration, I tried to move my camel application leveraging some Spring Boot and get rid of points that I had mentioned above.

Lets me do it step by step, and details some finer details en route, I will not be talking much on how to make Spring Boot applications, you can refer to the Spring Boot site for nice tutorials.

Remove camel context xml definitions

Since Spring Boot  applications has auto-configuration defined via @EnableAutoConfiguration which scans for the spring beans automatically – I should have my routes picked up automatically and ready to serve it purpose, the following code snippet shows how this is done,

@SpringBootApplication
@EnableAutoConfiguration
public class RoutingWithCamelApp {
   public static void main(String[] args) {
      SpringApplication.run(RoutingWithCamelApp.class);
   }
}

Convert my application.properties to application.(yaml|json)

Spring Boot  can also automatically convert and load your application.json/yaml as application properties ( convention of naming it as application.yaml/json helps here)

@Component annotations on Route Builders

Annotating @Component on the Route Builders, though there is nothing new with @Component annotation which is quiet normal in spring world, but how does this makes routes ready and configured ?? Its done by dependency camel-spring-boot-starter which adds the camel-spring-boot component that scans and loads all Camel Routes and Configuration using Spring Context via configuration bean. Since the camel-spring-boot adds the CamelAutoConfiguration to org.springframework.boot.autoconfigure.EnableAutoConfiguration using spring.factories1, the camel configuration becomes naturally available to Spring Boot application via @EnableAutoConfiguration

Embedded AMQ configuration

Traditionally when we want to use activemq with our Camel application, we add the camel-jms and activemq-all dependencies to our project, then define the following bean in camel context to setup the AMQ to work with Camel app,

<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost"/>
</bean>
</property>
</bean>

Ideally we don’t do any big thing here just making the broker started and available over url vm://localhost – then why this can’t be auto configured as well. Thats exactly what we will do here, adding camel-jms-starter and spring-boot-starter-activemq as dependencies will take care of doing this. The camel-jms-starter has the JmsComponentAutoConfiguration which does all the things we defined in the above xml snippet, since camel-jms-starter has added the JmsComponentAutoConfiguration to the org.springframework.boot.autoconfigure.EnableAutoConfiguration it’s again will be auto detected by  Spring Boot applications and configured via @EnableAutoConfiguration using spring.factories1. The AMQ related configuration are handled by spring-boot-starter-activemq, for our application we just need simple configuration like this in our application.yml

spring:
 activemq:
   in-memory: true
   pool:
     enabled: false

Now we are all set to run the same example old example as Spring Boot application, with lesser verbose configurations and preferring convention over configuration.

You can download the spring boot version of this simple example from here

References

  1.  http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html
Advertisements

5 Comments

  1. great post! One very very minor note: the `@EnableAutoConfiguration` annotation is redundant. Check the source and you’ll see that `@SpringBootApplication` is in turn annotated with `@EnableAutoConfiguration`, so there’s no need to specify it (unless you want to customize its behavior, of course). Keep up the great stuff! I’ve included this post in “This Week in Spring,” for January 24th, 2017 on the Spring Blog. Thanks!

  2. @starbuxman, thanks for pointing it out. I thought to show it explicitly in this case so that user can relate to what I am talking about. Since @SpringBootApplication does it implicitly most of the time developers will struggle to find the link one of the side-effects of auto config 😉 .. as you rightly said putting it in opens up the door for customisation which i personally feel matters a lot when we want to turn off few auto configuration. And thanks a ton for adding it to the “This week in Spring “

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s