blob: 8b093bbb1865e9742a37418e59b800770763c5ca [file] [log] [blame]
= Walk through an Example Code
This mini-guide takes you through the source code of a
https://github.com/apache/camel/blob/master/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[simple
example].
Camel can be configured either by using xref:spring.adoc[Spring] or
directly in Java - which
https://github.com/apache/camel/blob/master/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[this
example does].
We start with creating a xref:camelcontext.adoc[CamelContext] - which is
a container for xref:components::index.adoc[Components],
xref:routes.adoc[Routes]
etc:
[source,java]
----
include::{examplesdir}/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[tags=e1]
----
There is more than one way of adding a Component to the CamelContext. You can
add components implicitly - when we set up the routing - as we do here
for the
xref:components::file-component.adoc[FileComponent]:
[source,java]
----
include::{examplesdir}/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[tags=e3]
----
or explicitly - as we do here when we add the JMS Component:
[source,java]
----
include::{examplesdir}/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[tags=e2]
----
The above works with any JMS provider. If we know we are using
xref:components::activemq-component.adoc[ActiveMQ] we can use an even simpler form using the
http://activemq.apache.org/maven/5.5.0/activemq-camel/apidocs/org/apache/activemq/camel/component/ActiveMQComponent.html#activeMQComponent%28java.lang.String%29[`activeMQComponent()`
method] while specifying the
http://activemq.apache.org/configuring-transports.html[brokerURL] used
to connect to ActiveMQ
In normal use, an external system would be firing messages or events
directly into Camel through one if its xref:components::index.adoc[Components]
but we are going to use tha xref:producertemplate.adoc[ProducerTemplate]
which is a really easy way for testing your
configuration:
[source,java]
----
include::{examplesdir}/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[tags=e4]
----
Next you *must* start the camel context. If you are using
xref:spring.adoc[Spring] to configure the camel context this is
automatically done for you; though if you are using a pure Java approach
then you just need to call the start() method
[source,java]
----
camelContext.start();
----
This will start all of the configured routing rules.
So after starting the xref:camelcontext.adoc[CamelContext], we can fire
some objects into
camel:
[source,java]
----
include::{examplesdir}/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java[tags=e5]
----
== What happens?
From the
xref:producertemplate.adoc[ProducerTemplate]
- we send objects (in this case text) into the
xref:camelcontext.adoc[CamelContext] to the Component
_test-jms:queue:test.queue_. These text objects will be
xref:type-converter.adoc[converted automatically] into JMS Messages and
posted to a JMS Queue named _test.queue_. When we set up the
xref:routes.adoc[Route], we configured the
xref:components::file-component.adoc[FileComponent] to listen off the _test.queue_.
The File xref:components::file-component.adoc[FileComponent] will take messages off the
Queue, and save them to a directory named _test_. Every message will be
saved in a file that corresponds to its destination and message id.
Finally, we configured our own listener in the xref:routes.adoc[Route] -
to take notifications from the xref:components::file-component.adoc[FileComponent] and print
them out as text.
*That's it!*
If you have the time then use 5 more minutes to
xref:walk-through-another-example.adoc[Walk through another example]
that demonstrates the Spring DSL (XML based) routing.