|
Page Edited :
CAMEL :
Tutorial-JmsRemoting
Tutorial-JmsRemoting has been edited by Claus Ibsen (Jun 08, 2008). Change summary: CAMEL-590 (work in progress)
PrefaceThis 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 TODOs
PrerequisitesThis tutorial uses Maven to setup the Camel project and for dependencies for artifacts. Create the Camel Project
mvn archetype:create -DgroupId=org.example -DartifactId=CamelWithJmsAndSpring
Run the Serverorg/example/server/CamelServer.java public class CamelServer { public static void main(final String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("camel-server.xml"); } } The main method can then be executed to start the server. Creating the ClientWe will initially create a client by directly using CamelTemplate. We will later create a client which uses Spring remoting to hide the fact that messaging is being used. camel-client.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://activemq.apache.org/camel/schema/spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring-1.3-SNAPSHOT.xsd"> <camel:camelContext id="camel" /> <camel:template id="camelTemplate" /> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> </property> </bean> </beans> The client will not use the Camel Maven Plugin so the Spring XML has been placed in src/main/resources so not to conflict with the server configs.
org/example/client/CamelClient.java public class CamelClient { public static void main(final String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml"); CamelTemplate<JmsExchange> camelTemplate = (CamelTemplate) context.getBean("camelTemplate"); int response = (Integer)camelTemplate.sendBody("jms:queue:numbers", ExchangePattern.InOut, 22 ); Assert.assertEquals(66, response); System.out.println(response); } } The CamelTemplate is retrieved from a Spring ApplicationContext and used to manually place a message on the "numbers" JMS queue. The exchange pattern (ExchangePattern.InOut) states that the call should be synchronous, and that we will receive a response. We then assert that the response is three times the value of the original. Before running the client be sure that both the ActiveMQ broker and the CamelServer are running. Using Spring RemotingSpring 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. camel-client-remoting.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://activemq.apache.org/camel/schema/spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring-1.3-SNAPSHOT.xsd"> <camel:camelContext id="camel" /> <camel:proxy id="multiplier" serviceInterface="org.example.server.Multiplier" serviceUrl="jms:queue:numbers" /> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> </property> </bean> </beans> First we create a new Spring config file. This has a few changes made from camel-client.xml. Firstly the Camel template has been removed, as it will not be used. Secondly a proxy is defined. This will create a proxy service bean for you to use to make the remote invocations. The serviceInterface property details which Java interface is to be implemented by the proxy. serviceUrl defines where messages sent to this proxy bean will be directed. Here we define the JMS endpoint with the "numbers" queue we used when working with Camel template directly. The value of the id property is the name that will be the given to the bean when it is exposed through the Spring ApplicationContext. We will use this name to retrieve the service in our client. I have named the bean multiplierProxy simply to highlight that it is not the same multiplier bean as is being used by CamelServer. They are in completely independent contexts and have no knowledge of each other. As you are trying to mask the fact that remoting is being used in a real application you would generally not include proxy in the name. org/example/client/CamelClientRemoting.java public class CamelClientRemoting { public static void main(final String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("camel-client-remoting.xml"); Multiplier multiplier = (Multiplier) context.getBean("multiplierProxy"); int response = multiplier.multiply(22); Assert.assertEquals(66, response); System.out.println(response); } } Again, the client is similar to the original client, but with some important differences.
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. pom.xml <build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
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. TODO: TestingTODO: Detail how to unit and integration test this example. See Also |
Unsubscribe or edit your notifications preferences
