How to start Apache Camel: Message Router with Spring Boot

Apache Camel supports most of the Enterprise Integration Pattern, an engine for routing message. The tutorial will guide you how to start with Apache Camel by Spring Boot.

Related Articles:
How to start Spring Apache Kafka Application with SpringBoot
How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot

I. Technologies for Apache Camel tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE
– ActiveMQ 5.8.0 Release

II. Overviews
1. Structure of project

apache camel message router - structure of project

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Configure main run controller for Camel
– Create ActiveMQ Connection bean
– Create Router class
– Run & check result

3. Demo video
III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu choose File->New->Spring Starter Project, input project info as below:

spring apache router - create spring boot project

Press Next then Finish. Spring Boot project is created successfully.

2. Add needed dependencies

3. Configure main run controller for Camel

Config Run Controller for Camel, open application .properties , add configuration:

4. Create ActiveMQ Connection bean

Create ActiveMQComponent for connection factory

Import org.apache.activemq.camel.component.ActiveMQComponent in main class:

5. Create Router class

– Create Camel Router class, routing message from IN queue to “OUT” queue.
if: message in IN queue contains o1, message is routed to OUT_1 queue, else if message contains o2, it will be routed to OUT_2 queue. Otherwise, it will be routed to OUT_3.

6. Download & Run ActiveMQ

Download ActiveMQ from: activemq.apache.org

http://activemq.apache.org/activemq-580-release.html

Go to bin folder, run wrapper

activemq

CMD:

active mq start

7. Run & check result

Maven build:
clean install

Maven Run mode Spring Boot App

– Send a message to IN queue with content: Hello World

Send message spring apache camel router

Result:

Send message spring apache camel router

– Send a message to IN queue with content: o1: Welcome to Apache Router

Send message spring apache camel router - o1

Result:

Send message spring apache camel router - o1 - result

– Send a message to IN queue with content: o2: Have a nice day!

Send message spring apache camel router - o2

Result:

Send message spring apache camel router - o2 -result

IV. Source code

apache-router



By grokonez | September 3, 2016.

Last updated on June 8, 2017.



Related Posts


3 thoughts on “How to start Apache Camel: Message Router with Spring Boot”

  1. Hi, i got error when follow your this tutorial.
    This is what I got from console:

    . ____ _ __ _ _
    /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
    ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
    \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    ‘ |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot :: (v1.5.9.RELEASE)

    2018-01-19 16:25:37.107 INFO 12684 — [ main] c.r.s.m.SpringMessageRouterApplication : Starting SpringMessageRouterApplication on roelee with PID 12684 (started by Roe Lee in C:\Users\Roe Lee\Documents\workspace-sts-3.9.1.RELEASE\spring-message-router)
    2018-01-19 16:25:37.115 INFO 12684 — [ main] c.r.s.m.SpringMessageRouterApplication : No active profile set, falling back to default profiles: default
    2018-01-19 16:25:37.278 INFO 12684 — [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@17baae6e: startup date [Fri Jan 19 16:25:37 ICT 2018]; root of context hierarchy
    2018-01-19 16:25:38.073 INFO 12684 — [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [Beans.xml]
    2018-01-19 16:25:39.035 WARN 12684 — [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jms’: Failed to introspect bean class [org.apache.activemq.camel.component.ActiveMQComponent] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/apache/activemq/Service
    2018-01-19 16:25:39.055 INFO 12684 — [ main] utoConfigurationReportLoggingInitializer :

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with ‘debug’ enabled.
    2018-01-19 16:25:39.079 ERROR 12684 — [ main] o.s.boot.SpringApplication : Application startup failed

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jms’: Failed to introspect bean class [org.apache.activemq.camel.component.ActiveMQComponent] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/apache/activemq/Service
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:269) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1118) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1091) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at com.roelee.springboot.messagerouter.SpringMessageRouterApplication.main(SpringMessageRouterApplication.java:12) [classes/:na]
    Caused by: java.lang.NoClassDefFoundError: org/apache/activemq/Service
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_40]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_40]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_40]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    … 17 common frames omitted
    Caused by: java.lang.ClassNotFoundException: org.apache.activemq.Service
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_40]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_40]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_40]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_40]
    … 24 common frames omitted

  2. The “userName” attribute in the beans.xml is causing some problems after running the spring application.
    After changing it to “username”, the application started responding.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*