Getting Started With Apache Camel and Java
Learn the basics of how to create a Maven-based standalone Apache Camel project using the Java DSL with this straightforward tutorial.
Join the DZone community and get the full member experience.
Join For Freesome days ago, a friend of mine told me it would be very hard to start with apache camel as a java developer because the documentation is missing or very hard to find.
i know there are some other tutorials out there, but some of them are outdated or contain too much content for a simple getting-started guide. therefore, i decided to write this post to help other developers starting with apache camel and java.
this post describes how to create a new maven based standalone apache camel project using the java dsl. standalone means it's a plain java application and not integrated with a spring application, an osgi bundle, or something like that.
creating a camel project via console
you can use the following snippet to generate an apache camel project with a small sample routing logic using the java dsl.
mvn archetype:generate
-dgroupid=com.schulz.bennet
-dartifactid=camel-start-java
-darchetypegroupid=org.apache.camel.archetypes
-darchetypeartifactid=camel-archetype-java
-dinteractivemode=false
creating a camel project via intellij
just in case you don't like the console that much, you can use the intellij wizard as well. open intellij and click on
file -> new -> project... -> maven
. now enable the
create from archetype
checkbox and select
org.apache.camel.archetypes:camel-archetype-java
.
in the next step, you have to type your groupid and artifactid. you can also change the version if you want to:
understanding the sample project
in the sample which comes with the maven archetype, there are two classes:
mainapp
and
myroutebuilder
. the main app looks like this:
as you can see, it's a plain java class which contains a main() method as every java se project does. in addition to a java se project, there is also a main object in the main() method. this main object is a helper class which comes with the camel core dependency (part of your maven pom.xml). it can be used to boot up camel and keep it running more easily.
note: it's also possible to create a camelcontext on your own instead of using the main object for it, but i recommend using the main object for it because you don't have to add code to keep the jvm up and running. the main object takes care of it and will run as long as you don't terminate the current jvm.
you have to use this main object to add camel routes to it. a route is a class which contains integration logic like inputs, transformations, endpoints and so on. it's comparable to a graph which has exactly one source and one sink.
the camel route in this sample is named
myroutebuilder
.
a route class has to extend the routebuilder class. in addition to that, it has to override a configure() method which contains the routing logic. in this example, it contains a logic which reads files from
src/main/data
by using the file component. (see:
apache camel file connector
).
note:
components can be configured by adding it in http query parameter style. in this example, the 'noop' configuration means that the files in the
src/main/data
directory should not be moved or deleted.
as you can see in the intellij snippet, processing steps are added to the route by using a builder api starting with a from() method call. this from() call contains the file component described before. in the next processing step there is a choice() method call followed by when() and otherwise() calls. these two method calls take the output of the file component in the processing step before and take a look into the file itself by using the xpath component. this component parses the input files and searches for
/person/city
hierarchy in the input file. if it's part of the file and the city is london, another component will log "uk message" and will write the whole file to
target/messages/uk
. otherwise it will log "other message" and write it to
target/messages/others
.
that's it. just a small tutorial on how to get started with apache camel and java and an explanation of the sample project which comes with the apache camel java dsl maven archetype. i hope it helps some developers to get started which apache camel and java more easily.
Opinions expressed by DZone contributors are their own.
Comments