Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Guice JMS example is functionally similar to the Spring Example but using Guice as the Dependency Injection framework - it is also similar to the first example to show how to use the Guice approach to working with Camel.

In this example we just write RouteBuilder implementations, then we write a Guice module MyModule to create the CamelContext, bind any RouteBuilder instances and configure any components and endpoints, then we create a jndi.properties file to bootstrap Guice and Camel.

To run the example we currently use the Camel Maven Pluginmaven exec plugin. For example from the source or binary distribution the following should work

Code Block
cd examples/camel-example-guice-springjms
mvn compile camelexec:runjava

What this does is boot up the Spring ApplicationContext the Guice based JNDI provider from jndi.properties file on the classpath. This then bootstraps the Guice injector and loads whatever Guice modules are defined in the jndi.properties file META-INF/spring/camel-context.xml on the classpath. This is a regular Spring XML document which uses the Camel Xml Configuration to configure a CamelContext. Notice how the <camelContext> element is configured

Code Block

  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
   <package>org.apache.camel.example.spring</package>
     ...

The packages attribute defines the comma separated list of Java package names which are recursively searched for Java classes on the classpath which implement the RouteBuilder interface. What this means is that Camel will automatically detect our MyRouteBuilder class and automatically install its routes.

This approach, of using Java code to write the routes in the DSL and just wire together in XML what we really need to allows us to use the right language for the job and minimise the amount of XML bloat. Of course if you prefer you can create all of your routes in the Xml Configuration.

- then injects the remaining properties in the file.

Configuring Components

If you see the jms() method of the Guice MyModule you will see it is annotated with @Provides to indicate to Guice that it is a provider and it is annotated with @JndiBind("jms") to bind it to the JNDI name jms when it is created.

This method then configures the component. The provider method is parameterized by the @Named("activemq.brokerURL") property which is injected from the jndi.properties file.

So you can dependency inject whatever objects you need to create, be it an Endpoint, Component, RouteBuilder or arbitrary bean used within a route. Then you can inject any properties from the jndi.properties file easily - so that most of your configuration is all in Java code which is typesafe and easily refactorable - then leaving some properties to be environment specific (the jndi.properties file) which you can then change based on development, testing, production etcAlso note at the end of this XML example file we explicitly configure the ActiveMQ component with details of how to connect to the broker.