...
Wiki Markup |
---|
{snippet:id=e2|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/pom.xml} |
Writing the Server
Create the Spring Service
For this example the Spring service (= our business service) on the server will be a simple multiplier which trebles in the received valueAnd dependencies for the AOP enable server example. These dependencies are of course only needed if you need full blown AOP stuff using AspejctJ with bytecode instrumentation.
Wiki Markup |
---|
{snippet:id=e3e1|lang=xmljava|url=camel/trunk/examples/camel-example-spring-jms/pom.xml} |
Writing the Server
Create the Spring Service
/src/main/java/org/apache/camel/example/server/Multiplier.java} |
And the implementation of this service is:For this example the Spring service (= our business service) on the server will be a simple multiplier which trebles in the received value.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/MultiplierTreble.java} |
...
Notice that this class has been annotated with the @Service spring annotation. This ensures that this class is registered as a bean in the registry with the given name multiplier.
Define the Camel Routes
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/TrebleServerRoutes.java} |
Notice that this class has been annotated with the @Service spring annotation. This ensures that this class is registered as a bean in the registry with the given name multiplier.
Define the Camel Routes
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/ServerRoutes.java} |
This defines a Camel route from the JMS queue named numbers to the Spring bean named multiplier. Camel will create a consumer to the JMS queue which forwards all received messages onto the the Spring bean, using the method named multiply.
Configure Spring
This defines a Camel route from the JMS queue named numbers to the Spring bean named multiplier. Camel will create a consumer to the JMS queue which forwards all received messages onto the the Spring bean, using the method named multiply.
Configure Spring
The Spring config file is placed under META-INF/spring
as this is the default location used by the Camel Maven Plugin, which we will later use to run our server.
First we need to do the standard scheme declarations in the top. In the camel-server.xml we are using spring beans as the default bean: namespace and springs context:. For configuring ActiveMQ we use broker: and for Camel we of course have camel:The Spring config file is placed under META-INF/spring
as this is the default location used by the Camel Maven Plugin, which we will later use to run our server.
First we need to do the standard scheme declarations in the top. In the camel-server.xml we are using spring beans as the default bean: namespace and springs context:. For configuring ActiveMQ we use broker: and for Camel we of course have camel:. Notice that we don't use version numbers for the camel-spring schema. At runtime the schema is resolved in the Camel bundle. If we use a specific version number such as 1.4 then its IDE friendly as it would be able to import it and provide smart completion etc. See Xml Reference for further details.
...
component-scan | Defines the package to be scanned for Spring stereotype annotations, in this case, to load the "multiplier" bean |
camel-context | Defines the package to be scanned for Camel routes. Will find the |
jms bean | Creates the Camel JMS component |
AOP Enabled Server
The example has an enhanced Server example that uses fullblown AspejctJ AOP for doing a audit tracking of invocations of the business service.
We leverage Spring AOP support in the {{camel-server-aop.xml} configuration file. First we must declare the correct XML schema's to use:
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml} |
Then we include all the existing configuration from the normal server example:
Wiki Markup |
---|
{snippet:id=e2|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml} |
Then we enable the AspejctJ AOP auto proxy feature of Spring that will scan for classes annotated with the @Aspect annotation:
Wiki Markup |
---|
{snippet:id=e3|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml} |
Then we define our Audit tracker bean that does the actual audit logging. It's also the class that is annotated with the @Aspect so Spring will pick this up, as the aspect.
Wiki Markup |
---|
{snippet:id=e4|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml} |
And the gem is that we inject the AuditTracker aspect bean with a Camel endpoint that defines where the audit should be stored. Noticed how easy it is to setup as we have just defined an endpoint URI that is file based, meaning that we stored the audit tracks as files. We can change this tore to any Camel components as we wish. To store it on a JMS queue simply change the URI to jms:queue:audit.
Wiki Markup |
---|
{snippet:id=e5|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml} |
And the full blown Aspejct for the audit tracker java code:
...
Camel routes. Will find the | |
jms bean | Creates the Camel JMS component |
...
Run the Server
The Server is started using the org.apache.camel.spring.Main
class that can start camel-spring application out-of-the-box. The Server can be started in several flavors:
...
In this sample as there are two servers (with and without AOP) we have prepared some profiles in maven to start the Server of your choice.
The server is started with:
mvn compile exec:java -PCamelServer
Or for the AOP enabled Server example:
mvn compile exec:java -PCamelServerAOP
Writing The Clients
This sample has three clients demonstrating different Camel techniques for communication
- CamelClient using the ProducerTemplate for Spring template style coding
- CamelRemoting using Spring Remoting
- CamelEndpoint using the Message Endpoint EIP pattern using a neutral Camel API
...
The ProducerTemplate
is retrieved from a Spring ApplicationContext
and used to manually place a message on the "numbers" JMS queue. The requestBody
method will use the exchange pattern (ExchangePattern. InOut) , which states that the call should be synchronous, and that we will receive the caller expects a response.
Before running the client be sure that both the ActiveMQ broker and the CamelServer
are running.
...