Tutorial on Spring Remoting with JMS
Info | ||
---|---|---|
| ||
This tutorial was kindly donated to Apache Camel by Martin Gilday. |
Preface
This tutorial aims to guide the reader through the stages of creating a project which uses Camel to facilitate the routing of messages from a JMS queue to a Spring service. The route works in a synchronous fashion returning a response to the client.
Table of Contents |
---|
Prerequisites
This tutorial uses Maven to setup the Camel project and for dependencies for artifacts.
Distribution
This sample is distributed with the Camel distribution as examples/camel-example-spring-jms
.
About
This tutorial is a simple example that demonstrates more the fact how well Camel is seamless integrated with Spring to leverage the best of both worlds. This sample is client server solution using JMS messaging as the transport. The sample has two flavors of servers and also for clients demonstrating different techniques for easy communication.
...
Component | Description |
---|---|
We use Apache ActiveMQ as the JMS broker on the Server side | |
We use the bean binding to easily route the messages to our business service. This is a very powerful component in Camel. | |
In the AOP enabled Server we store audit trails as files. | |
Used for the JMS messaging |
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. |
Code Block |
---|
mvn archetype:create -DgroupId=org.example -DartifactId=CamelWithJmsAndSpring |
Update the POM with Dependencies
First we need to have dependencies for the core Camel jars, its spring, jms components and finally ActiveMQ as the message broker.
...
Wiki Markup |
---|
{snippet:id=e3|lang=xml|url=activemq/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 value.
...
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=activemq/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
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.
...
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/AuditTracker.java} |
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:
...
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
Client Using The ProducerTemplate
We will initially create a client by directly using ProducerTemplate
. We will later create a client which uses Spring remoting to hide the fact that messaging is being used.
...
Before running the client be sure that both the ActiveMQ broker and the CamelServer
are running.
Client Using Spring Remoting
Spring Remoting "eases the development of remote-enabled services". It does this by allowing you to invoke remote services through your regular Java interface, masking that a remote service is being called.
...
- The Spring context is created with the new camel-client-remoting.xml
- We retrieve the proxy bean instead of a
ProducerTemplate
. In a non-trivial example you would have the bean injected as in the standard Spring manner. - The multiply method is then called directly. In the client we are now working to an interface. There is no mention of Camel or JMS inside our Java code.
Client Using Message Endpoint EIP Pattern
This client uses the Message Endpoint EIP pattern to hide the complexity to communicate to the Server. The Client uses the same simple API to get hold of the endpoint, create an exchange that holds the message, set the payload and create a producer that does the send and receive. All done using the same neutral Camel API for all the components in Camel. So if the communication was socket TCP based you just get hold of a different endpoint and all the java code stays the same. That is really powerful.
...
Switching to a different component is just a matter of using the correct endpoint. So if we had defined a TCP endpoint as: "mina:tcp://localhost:61616"
then its just a matter of getting hold of this endpoint instead of the JMS and all the rest of the java code is exactly the same.
Run the Clients
The Clients is started using their main class respectively.
...
Also see the Maven pom.xml
file how the profiles for the clients is defined.
Using the Camel Maven Plugin
The Camel Maven Plugin allows you to run your Camel routes directly from Maven. This negates the need to create a host application, as we did with Camel server, simply to start up the container. This can be very useful during development to get Camel routes running quickly.
...
All that is required is a new plugin definition in your Maven POM. As we have already placed our Camel config in the default location (camel-server.xml has been placed in META-INF/spring/) we do not need to tell the plugin where the route definitions are located. Simply run mvn camel:run
.
Using Camel JMX
Camel has extensive support for JMX and allows us to inspect the Camel Server at runtime. As we have enabled the JMXAgent in our tutorial we can fire up the jconsole and connect to the following service URI: service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/camel
. Notice that Camel will log at INFO level the JMX Connector URI:
...
In the screenshot below we can see the route and its performance metrics: