...
Prerequisites
This tutorial uses ActiveMQ as the JMS broker. Download and extract it, then start the broker using script in the bin directory.
It is assumed that the reader is familiar with Spring (including v2.5 features) and Maven.Maven to setup the Camel project and for dependencies for artifacts.
Create the Camel Project
Info |
---|
For the purposes of the tutorial a single Maven project will be used for both the client and server. Ideally you would break your application down into the appropriate components. |
...
Update the POM with Dependencies
Code Block |
---|
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
|
Writing the Server
Create the Spring Service
For this example the Spring service on the server will be a simple multiplier which trebles in the received value. The classes should reside in the package org.example.server
.
Code Block | ||
---|---|---|
| ||
public interface Multiplier {
/**
* Multiplies the given number by a pre-defined constant.
*
* @param originalNumber The number to be multiplied
* @return The result of the multiplication
*/
int multiply(int originalNumber);
}
|
Code Block | ||
---|---|---|
| ||
@Service(value="multiplier")
public class Treble implements Multiplier {
/* (non-Javadoc)
* @see org.example.server.Multiplier#multiply(int)
*/
public int multiply(final int originalNumber) {
return originalNumber * 3;
}
}
|
First we need to have dependencies for the core Camel jars and its spring and jms components.
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/pom.xml} |
As we use spring xml configuration for the ActiveMQ JMS broker we need this dependency:
Wiki Markup |
---|
{snippet:id=e2|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/pom.xml} |
TODO: AOP dependencies
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 value.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/Multiplier.java} |
And the implementation of this service is:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/Treble.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 Using Spring annotations the bean is defined with the name multiplier.
Define the Camel Routes
Wiki Markup | ||
---|---|---|
{snippet:id=e1|lang=java|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/ServerRoutes.java} | ||
Code Block | ||
| ||
public class ServerRoutes extends RouteBuilder {
/* (non-Javadoc)
* @see org.apache.camel.builder.RouteBuilder#configure()
*/
@Override
public void configure() throws Exception {
from("jms:queue:numbers").beanRef("multiplier", "multiply");
}
}
|
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.
...