Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Walk through an Example Code

This mini-guide takes you through the source code of a simple example.

...

Camel can be configured either by using Spring or directly in Java code (- which this example does).

This example is available in the examples\camel-example-jms-file directory of the Camel distribution.

We start with creating a CamelContext - which is a container for Components, Routes etc:

...

{snippet:id=e1|lang=java|url=

...

camel/trunk/examples/camel-

...

example-jms-file/src/main/java/org/apache/camel/

...

example/jmstofile/

...

CamelJmsToFileExample.java}

...

There is more than one way of adding a Component to the CamelContext - explicitly . You can add components implicitly - when we set up the routing - as we do here when we add the JMS Component:

...

for the FileComponent:{snippet:id=

...

e3|lang=java|url=

...

camel/trunk/examples/camel-

...

example-jms-file/src/main/java/org/apache/camel/

...

example/jmstofile/

...

CamelJmsToFileExample.java}

...

or implicitly - when we set up the routing (explicitly - as we do here for the FileComponent:

...

when we add the JMS Component:{snippet:id=

...

e2|lang=java|url=

...

camel/trunk/examples/camel-

...

example-jms-file/src/main/java/org/apache/camel/

...

example/jmstofile/

...

CamelJmsToFileExample.java}The above works with any JMS provider. If we know we are using ActiveMQ we can use an even simpler form using the activeMQComponent() method while specifying the brokerURL used to connect to ActiveMQ

In normal use, an external system would be firing messages or events into directly into Camel through one if its Components or Containers but we are going to use the CamelClient ProducerTemplate which is a really nice an easy way for testing your configuration:

...

{snippet:id=e4|lang=java|url=

...

camel/trunk/examples/camel-

...

example-jms-file/src/main/java/org/apache/camel/

...

example/jmstofile/

...

CamelJmsToFileExample.java}Next you must start the camel context. If you are using 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

...

This will start all of the configured routing rules.

So after starting the CamelContext, we can fire some objects into camel:

...

{snippet:id=e5|lang=java|url=

...

camel/trunk/examples/camel-

...

example-jms-file/src/main/java/org/apache/camel/

...

example/jmstofile/

...

CamelJmsToFileExample.java}

What happens?

From the CamelClient ProducerTemplate - we send objects (in this case text) into the CamelContext to the Component test-jms:queue:test.queue. These text objects will be converted automatically into JMS Messages and posted to a JMS Queue named test.queue. When we set up the Route, we configured the FileComponent to listen of off the test.queue.

The File FileComponent will take messages of 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 Route - to take notifications from the FileComponent and print them out as text.Thats it!

That's it!

If you have the time then use 5 more minutes to Walk through another example that demonstrates the Spring DSL (XML based) routing.