Modified: websites/production/camel/content/tutorial-jmsremoting.html
==============================================================================
--- websites/production/camel/content/tutorial-jmsremoting.html (original)
+++ websites/production/camel/content/tutorial-jmsremoting.html Fri Aug 25
08:22:01 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: Tutorial-JmsRemoting
@@ -86,261 +75,8 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2
id="Tutorial-JmsRemoting-TutorialonSpringRemotingwithJMS">Tutorial on Spring
Remoting with JMS</h2><p> </p><div class="confluence-information-macro
confluence-information-macro-information"><p class="title">Thanks</p><span
class="aui-icon aui-icon-small aui-iconfont-info
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>This tutorial was kindly donated
to Apache Camel by Martin Gilday.</p></div></div><h2
id="Tutorial-JmsRemoting-Preface">Preface</h2><p>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 <a shape="rect"
class="external-link" href="http://www.springramework.org"
rel="nofollow">Spring</a> service. The route works in a synchronous fashion
returning a response to the client.</p><p></p><div class="toc-macro
client-side-toc-macro" data-headerelements="H1,H2,H3,H4,H5,H6,H7"></div><h2
id="Tutorial-JmsRemoting-Prerequisites">Prerequisites</h2><p>This tutorial
uses Maven to setup the Camel project and for dependencies for
artifacts.</p><h2
id="Tutorial-JmsRemoting-Distribution">Distribution</h2><p>This sample is
distributed with the Camel distribution as
<code>examples/camel-example-spring-jms</code>.</p><h2
id="Tutorial-JmsRemoting-About">About</h2><p>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.</p><p>The Server is a JMS message broker that routes incoming
messages to a business service that does computations on the received message
and returns a response.<br clear="none"> The EIP patterns used in this sample
are:</p><div class="table-wrap"><table class="confluenceTa
ble"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Pattern</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><a shape="rect"
href="message-channel.html">Message Channel</a></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>We need a channel so the Clients can
communicate with the server.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="message.html">Message
</a></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>The
information is exchanged using the Camel Message
interface.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="message-translator.html">Message
Translator</a></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>This
is where Camel shines as the message exchange between the Server and the
Clients are text based strings with numbers. However our business servi
ce uses int for numbers. So Camel can do the message translation
automatically.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="message-endpoint.html">Message
Endpoint</a></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>It
should be easy to send messages to the Server from the the clients. This is
achieved with Camel's powerful Endpoint pattern that even can be more powerful
combined with Spring remoting. The tutorial has clients using each kind of
technique for this.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect"
href="point-to-point-channel.html">Point to Point Channel</a></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>The client and server exchange
data using point to point using a JMS queue.</p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><a shape="rect"
href="event-driven-consumer.html">Event Driven Consumer</a></p></td><td
colspan="1" rowspan="1" class
="confluenceTd"><p><span>The JMS broker is event driven and is invoked when
the client sends a message to the
server.</span></p></td></tr></tbody></table></div><p>We use the following Camel
components:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Component</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><a shape="rect"
href="activemq.html">ActiveMQ</a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>We use Apache ActiveMQ as the JMS broker on the Server
side</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><a
shape="rect" href="bean.html">Bean</a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>We use the bean binding to easily route the messages to
our business service. This is a very powerful component in
Camel.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluen
ceTd"><p><a shape="rect" href="file2.html">File</a></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>In the AOP enabled Server we store audit
trails as files.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="jms.html">JMS</a></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Used for the JMS
messaging</p></td></tr></tbody></table></div><h2
id="Tutorial-JmsRemoting-CreatetheCamelProject">Create the Camel
Project</h2><div class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>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.</p></div></div><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelConten
t pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[mvn archetype:generate -DgroupId=org.example
-DartifactId=CamelWithJmsAndSpring
-]]></script>
-</div></div><h3 id="Tutorial-JmsRemoting-UpdatethePOMwithDependencies">Update
the POM with Dependencies</h3><p>First we need to have dependencies for the
core Camel jars, spring, jms components, and finally ActiveMQ as the message
broker.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- required by both client and server -->
-<dependency>
- <groupId>org.apache.camel</groupId>
- <artifactId>camel-core</artifactId>
-</dependency>
-<dependency>
- <groupId>org.apache.camel</groupId>
- <artifactId>camel-jms</artifactId>
-</dependency>
-<dependency>
- <groupId>org.apache.camel</groupId>
- <artifactId>camel-spring</artifactId>
-</dependency>
-<dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-camel</artifactId>
-</dependency>
-<dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-pool</artifactId>
-</dependency>
-]]></script>
-</div></div>As we use spring xml configuration for the ActiveMQ JMS broker we
need this dependency:<div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- xbean is required for ActiveMQ broker configuration in the spring xml
file -->
-<dependency>
- <groupId>org.apache.xbean</groupId>
- <artifactId>xbean-spring</artifactId>
-</dependency>
-]]></script>
-</div></div><h2 id="Tutorial-JmsRemoting-WritingtheServer">Writing the
Server</h2><h3 id="Tutorial-JmsRemoting-CreatetheSpringService">Create the
Spring Service</h3><p>For this example the Spring service (our business
service) on the server will be a simple multiplier which trebles in the
received value.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-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);
-
-}
-]]></script>
-</div></div>And the implementation of this service is:<div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-@Service(value = "multiplier")
-public class Treble implements Multiplier {
-
- public int multiply(final int originalNumber) {
- return originalNumber * 3;
- }
-
-}
-]]></script>
-</div></div>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 <strong>multiplier</strong>.<h3
id="Tutorial-JmsRemoting-DefinetheCamelRoutes">Define the Camel
Routes</h3><p></p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-public class ServerRoutes extends RouteBuilder {
-
- @Override
- public void configure() throws Exception {
- // route from the numbers queue to our business that is a spring bean
registered with the id=multiplier
- // Camel will introspect the multiplier bean and find the best
candidate of the method to invoke.
- // You can add annotations etc to help Camel find the method to invoke.
- // As our multiplier bean only have one method its easy for Camel to
find the method to use.
- from("jms:queue:numbers").to("multiplier");
-
- // Camel has several ways to configure the same routing, we have
defined some of them here below
-
- // as above but with the bean: prefix
- //from("jms:queue:numbers").to("bean:multiplier");
-
- // bean is using explicit bean bindings to lookup the multiplier bean
and invoke the multiply method
- //from("jms:queue:numbers").bean("multiplier",
"multiply");
-
- // the same as above but expressed as a URI configuration
-
//from("jms:queue:numbers").to("bean:multiplier?method=multiply");
- }
-
-}
-]]></script>
-</div></div>This defines a Camel route <em>from</em> the JMS queue named
<strong>numbers</strong> <em>to</em> the Spring <a shape="rect"
href="bean.html">bean</a> named <strong>multiplier</strong>. Camel will create
a consumer to the JMS queue which forwards all received messages onto the the
Spring bean, using the method named <strong>multiply</strong>.<h3
id="Tutorial-JmsRemoting-ConfigureSpring">Configure Spring</h3><p>The Spring
config file is placed under <code>META-INF/spring</code> as this is the default
location used by the <a shape="rect" href="camel-maven-plugin.html">Camel Maven
Plugin</a>, which we will later use to run our server.<br clear="none"> 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
<strong>bean:</strong> namespace and springs <strong>context:</strong>. For
configuring ActiveMQ we use <strong>broker:</strong> and for Camel we of course
have <strong>camel:</strong>. 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 <a shape="rect" href="xml-reference.html">Xml
Reference</a> for further details.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:camel="http://camel.apache.org/schema/spring"
- xmlns:broker="http://activemq.apache.org/schema/core"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
- http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
- http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
-]]></script>
-</div></div>We use Spring annotations for doing IoC dependencies and its
component-scan features comes to the rescue as it scans for spring annotations
in the given package name:<div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- let Spring do its IoC stuff in this package -->
-<context:component-scan
base-package="org.apache.camel.example.server"/>
-]]></script>
-</div></div>Camel will of course not be less than Spring in this regard so it
supports a similar feature for scanning of Routes. This is configured as shown
below.<br clear="none"> Notice that we also have enabled the <a shape="rect"
href="camel-jmx.html">JMXAgent</a> so we will be able to introspect the Camel
Server with a JMX Console.<div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- declare a camel context that scans for classes that is RouteBuilder
- in the package org.apache.camel.example.server -->
-<camel:camelContext id="camel-server">
- <camel:package>org.apache.camel.example.server</camel:package>
- <!-- enable JMX connector so we can connect to the server and browse
mbeans -->
- <!-- Camel will log at INFO level the service URI to use for connecting
with jconsole -->
- <camel:jmxAgent id="agent" createConnector="true"/>
-</camel:camelContext>
-]]></script>
-</div></div>The ActiveMQ JMS broker is also configured in this xml file. We
set it up to listen on TCP port 61610.<div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- lets configure the ActiveMQ JMS broker server -->
-<broker:broker useJmx="true" persistent="false"
brokerName="myBroker">
- <broker:transportConnectors>
- <!-- expose a VM transport for in-JVM transport between AMQ and Camel
on the server side -->
- <broker:transportConnector name="vm"
uri="vm://myBroker"/>
- <!-- expose a TCP transport for clients to use -->
- <broker:transportConnector name="tcp"
uri="tcp://localhost:${tcp.port}"/>
- </broker:transportConnectors>
-</broker:broker>
-]]></script>
-</div></div>As this examples uses JMS then Camel needs a <a shape="rect"
href="jms.html">JMS component</a> that is connected with the ActiveMQ broker.
This is configured as shown below:<div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- lets configure the Camel ActiveMQ to use the embedded ActiveMQ broker
declared above -->
-<bean id="jms"
class="org.apache.activemq.camel.component.ActiveMQComponent">
- <property name="connectionFactory">
- <bean
class="org.apache.activemq.spring.ActiveMQConnectionFactory">
- <property name="brokerURL"
value="vm://myBroker"/>
- <property name="trustAllPackages"
value="true"/>
- </bean>
- </property>
-</bean>
-]]></script>
-</div></div><strong>Notice:</strong> The <a shape="rect" href="jms.html">JMS
component</a> is configured in standard Spring beans, but the gem is that the
bean id can be referenced from Camel routes - meaning we can do routing using
the JMS Component by just using <strong>jms:</strong> prefix in the route URI.
What happens is that Camel will find in the Spring Registry for a bean with the
id="jms". Since the bean id can have arbitrary name you could have named it
id="jmsbroker" and then referenced to it in the routing as
<code>from="jmsbroker:queue:numbers).to("multiplier");</code><br clear="none">
We use the vm protocol to connect to the ActiveMQ server as its embedded in
this application.<div class="table-wrap"><table
class="confluenceTable"><tbody><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>component-scan</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Defines the package to be scanned for Spring stereotype
annotations, in this case, to load the "multiplie
r" bean</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>camel-context</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Defines the package to be scanned for Camel routes.
Will find the <code>ServerRoutes</code> class and create the routes contained
within it</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>jms bean</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Creates the Camel JMS
component</p></td></tr></tbody></table></div><h3
id="Tutorial-JmsRemoting-RuntheServer">Run the Server</h3><p>The Server is
started using the <code>org.apache.camel.spring.Main</code> class that can
start camel-spring application out-of-the-box. The Server can be started in
several flavors:</p><ul class="alternate"><li>as a standard java main
application - just start the <code>org.apache.camel.spring.Main</code>
class</li><li>using maven jave:exec</li><li>using <a shape="rect"
href="camel-run-maven-goal.html">camel:run</a></li></ul><p>In th
is sample as there are two servers (with and without AOP) we have prepared
some profiles in maven to start the Server of your choice.<br clear="none"> The
server is started with:<br clear="none"> <code>mvn compile exec:java
-PCamelServer</code></p><h2 id="Tutorial-JmsRemoting-WritingTheClients">Writing
The Clients</h2><p>This sample has three clients demonstrating different Camel
techniques for communication</p><ul class="alternate"><li>CamelClient using the
<a shape="rect" href="producertemplate.html">ProducerTemplate</a> for Spring
template style coding</li><li>CamelRemoting using Spring
Remoting</li><li>CamelEndpoint using the Message Endpoint EIP pattern using a
neutral Camel API</li></ul><h3
id="Tutorial-JmsRemoting-ClientUsingTheProducerTemplate">Client Using The
ProducerTemplate</h3><p>We will initially create a client by directly using
<code>ProducerTemplate</code>. We will later create a client which uses Spring
remoting to hide the fact that messaging is being used.</p><di
v class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:camel="http://camel.apache.org/schema/spring"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
- http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
-]]></script>
-</div></div><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<camel:camelContext id="camel-client">
- <camel:template id="camelTemplate"/>
-</camel:camelContext>
-]]></script>
-</div></div><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- Camel JMSProducer to be able to send messages to a remote Active MQ
server -->
-<bean id="jms"
class="org.apache.activemq.camel.component.ActiveMQComponent">
- <property name="connectionFactory">
- <bean
class="org.apache.activemq.spring.ActiveMQConnectionFactory">
- <property name="brokerURL"
value="tcp://localhost:${tcp.port}"/>
- <property name="trustAllPackages"
value="true"/>
- </bean>
- </property>
-</bean>
-]]></script>
-</div></div>The client will not use the <a shape="rect"
href="camel-maven-plugin.html">Camel Maven Plugin</a> so the Spring XML has
been placed in <em>src/main/resources</em> to not conflict with the server
configs.<div class="table-wrap"><table class="confluenceTable"><tbody><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>camelContext</p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>The Camel context is defined
but does not contain any routes</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>template</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The <code>ProducerTemplate</code> is used to place
messages onto the JMS queue</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>jms bean</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>This initialises the Camel JMS component, allowing us
to place messages onto the queue</p></td></tr></tbody></table></div><p>And the
CamelClient source code:</p><div class="co
de panel pdl" style="border-width: 1px;"><div class="codeContent panelContent
pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-public static void main(final String[] args) throws Exception {
- System.out.println("Notice this client requires that the CamelServer
is already running!");
-
- AbstractApplicationContext context = new
ClassPathXmlApplicationContext("camel-client.xml");
-
- // get the camel template for Spring template style sending of messages (=
producer)
- ProducerTemplate camelTemplate =
context.getBean("camelTemplate", ProducerTemplate.class);
-
- System.out.println("Invoking the multiply with 22");
- // as opposed to the CamelClientRemoting example we need to define the
service URI in this java code
- int response =
(Integer)camelTemplate.sendBody("jms:queue:numbers",
ExchangePattern.InOut, 22);
- System.out.println("... the result is: " + response);
-
- // we're done so let's properly close the application context
- IOHelper.close(context);
-}
-]]></script>
-</div></div>The <code>ProducerTemplate</code> is retrieved from a Spring
<code>ApplicationContext</code> and used to manually place a message on the
"numbers" JMS queue. The <code>requestBody</code> method will use the exchange
pattern InOut, which states that the call should be synchronous, and that the
caller expects a response.<p>Before running the client be sure that both the
ActiveMQ broker and the <code>CamelServer</code> are running.</p><h3
id="Tutorial-JmsRemoting-ClientUsingSpringRemoting">Client Using Spring
Remoting</h3><p><a shape="rect" href="spring-remoting.html">Spring Remoting</a>
"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.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- Camel proxy for a given service, in this case the JMS queue -->
-<camel:proxy
- id="multiplierProxy"
- serviceInterface="org.apache.camel.example.server.Multiplier"
- serviceUrl="jms:queue:numbers"/>
-]]></script>
-</div></div>The snippet above only illustrates the different and how Camel
easily can setup and use Spring Remoting in one line configurations.<p>The
<strong>proxy</strong> will create a proxy service bean for you to use to make
the remote invocations. The <strong>serviceInterface</strong> property details
which Java interface is to be implemented by the proxy.
The <strong>serviceUrl</strong> 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
<strong>id</strong> property is the name that will be the given to the bean
when it is exposed through the Spring <code>ApplicationContext</code>. We will
use this name to retrieve the service in our client. I have named the bean
<em>multiplierProxy</em> simply to highlight that it is not the same multiplier
bean as is being used by <code>CamelServer</code>. They are in completely
independent contexts a
nd 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.</p><p>And the Java client source code:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-public static void main(final String[] args) {
- System.out.println("Notice this client requires that the CamelServer
is already running!");
-
- AbstractApplicationContext context = new
ClassPathXmlApplicationContext("camel-client-remoting.xml");
- // just get the proxy to the service and we as the client can use the
"proxy" as it was
- // a local object we are invoking. Camel will under the covers do the
remote communication
- // to the remote ActiveMQ server and fetch the response.
- Multiplier multiplier = context.getBean("multiplierProxy",
Multiplier.class);
-
- System.out.println("Invoking the multiply with 33");
- int response = multiplier.multiply(33);
- System.out.println("... the result is: " + response);
-
- // we're done so let's properly close the application context
- IOHelper.close(context);
-}
-]]></script>
-</div></div>Again, the client is similar to the original client, but with some
important differences.<ol><li>The Spring context is created with the new
<em>camel-client-remoting.xml</em></li><li>We retrieve the proxy bean instead
of a <code>ProducerTemplate</code>. In a non-trivial example you would have the
bean injected as in the standard Spring manner.</li><li>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.</li></ol><h3
id="Tutorial-JmsRemoting-ClientUsingMessageEndpointEIPPattern">Client Using
Message Endpoint EIP Pattern</h3><p>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 <strong>all</strong> the
c
omponents 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.</p><p>Okay enough talk, show me the code!</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-public static void main(final String[] args) throws Exception {
- System.out.println("Notice this client requires that the CamelServer
is already running!");
-
- AbstractApplicationContext context = new
ClassPathXmlApplicationContext("camel-client.xml");
- CamelContext camel = context.getBean("camel-client",
CamelContext.class);
-
- // get the endpoint from the camel context
- Endpoint endpoint = camel.getEndpoint("jms:queue:numbers");
-
- // create the exchange used for the communication
- // we use the in out pattern for a synchronized exchange where we expect a
response
- Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
- // set the input on the in body
- // must be correct type to match the expected type of an Integer object
- exchange.getIn().setBody(11);
-
- // to send the exchange we need an producer to do it for us
- Producer producer = endpoint.createProducer();
- // start the producer so it can operate
- producer.start();
-
- // let the producer process the exchange where it does all the work in
this oneline of code
- System.out.println("Invoking the multiply with 11");
- producer.process(exchange);
-
- // get the response from the out body and cast it to an integer
- int response = exchange.getOut().getBody(Integer.class);
- System.out.println("... the result is: " + response);
-
- // stopping the JMS producer has the side effect of the "ReplyTo
Queue" being properly
- // closed, making this client not to try any further reads for the replies
from the server
- producer.stop();
-
- // we're done so let's properly close the application context
- IOHelper.close(context);
-}
-]]></script>
-</div></div>Switching to a different component is just a matter of using the
correct endpoint. So if we had defined a TCP endpoint as:
<code>"mina:tcp://localhost:61610"</code> 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.<h3 id="Tutorial-JmsRemoting-RuntheClients">Run the
Clients</h3><p>The Clients is started using their main class
respectively.</p><ul class="alternate"><li>as a standard java main application
- just start their main class</li><li>using maven jave:exec</li></ul><p>In this
sample we start the clients using maven:<br clear="none"> <code>mvn compile
exec:java -PCamelClient</code><br clear="none"> <code>mvn compile exec:java
-PCamelClientRemoting</code><br clear="none"> <code>mvn compile exec:java
-PCamelClientEndpoint</code></p><p>Also see the Maven <code>pom.xml</code> file
how the profiles for the clients is defined.</p><h2
id="Tutorial-JmsRemoting-UsingtheCamelMavenPlugin">Using the
Camel Maven Plugin</h2><p>The <a shape="rect"
href="camel-maven-plugin.html">Camel Maven Plugin</a> 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.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeHeader panelHeader pdl" style="border-bottom-width:
1px;"><b>pom.xml</b></div><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<build>
+<div class="wiki-content maincontent"><h2
id="Tutorial-JmsRemoting-TutorialonSpringRemotingwithJMS">Tutorial on Spring
Remoting with JMS</h2><p> </p><parameter
ac:name="title">Thanks</parameter><rich-text-body><p>This tutorial was kindly
donated to Apache Camel by Martin Gilday.</p></rich-text-body><h2
id="Tutorial-JmsRemoting-Preface">Preface</h2><p>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 <a shape="rect"
class="external-link" href="http://www.springramework.org"
rel="nofollow">Spring</a> service. The route works in a synchronous fashion
returning a response to the client.</p><p></p><h2
id="Tutorial-JmsRemoting-Prerequisites">Prerequisites</h2><p>This tutorial uses
Maven to setup the Camel project and for dependencies for artifacts.</p><h2
id="Tutorial-JmsRemoting-Distribution">Distribution</h2><p>This sample is
distributed with the Camel distribution as <code>ex
amples/camel-example-spring-jms</code>.</p><h2
id="Tutorial-JmsRemoting-About">About</h2><p>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.</p><p>The Server is a JMS message broker that routes incoming
messages to a business service that does computations on the received message
and returns a response.<br clear="none"> The EIP patterns used in this sample
are:</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th
colspan="1" rowspan="1" class="confluenceTh"><p>Pattern</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p><a shape="rect"
href="message-channel.html">Message Channel</a></
p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>We need a channel
so the Clients can communicate with the server.</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p><a shape="rect"
href="message.html">Message </a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The information is exchanged using the Camel Message
interface.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="message-translator.html">Message
Translator</a></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>This
is where Camel shines as the message exchange between the Server and the
Clients are text based strings with numbers. However our business service uses
int for numbers. So Camel can do the message translation
automatically.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect" href="message-endpoint.html">Message
Endpoint</a></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>It shou
ld be easy to send messages to the Server from the the clients. This is
achieved with Camel's powerful Endpoint pattern that even can be more powerful
combined with Spring remoting. The tutorial has clients using each kind of
technique for this.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><a shape="rect"
href="point-to-point-channel.html">Point to Point Channel</a></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>The client and server exchange
data using point to point using a JMS queue.</p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><a shape="rect"
href="event-driven-consumer.html">Event Driven Consumer</a></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><span>The JMS broker is event
driven and is invoked when the client sends a message to the
server.</span></p></td></tr></tbody></table></div><p>We use the following Camel
components:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan
="1" rowspan="1" class="confluenceTh"><p>Component</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p><a shape="rect"
href="activemq.html">ActiveMQ</a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>We use Apache ActiveMQ as the JMS broker on the Server
side</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><a
shape="rect" href="bean.html">Bean</a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>We use the bean binding to easily route the messages to
our business service. This is a very powerful component in
Camel.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><a
shape="rect" href="file2.html">File</a></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>In the AOP enabled Server we store audit trails as
files.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><a
shape="rect" href="jms.html">JMS</a></p>
</td><td colspan="1" rowspan="1" class="confluenceTd"><p>Used for the JMS
messaging</p></td></tr></tbody></table></div><h2
id="Tutorial-JmsRemoting-CreatetheCamelProject">Create the Camel
Project</h2><rich-text-body><p>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.</p></rich-text-body><plain-text-body>mvn archetype:generate
-DgroupId=org.example -DartifactId=CamelWithJmsAndSpring
+</plain-text-body><h3
id="Tutorial-JmsRemoting-UpdatethePOMwithDependencies">Update the POM with
Dependencies</h3><p>First we need to have dependencies for the core Camel jars,
spring, jms components, and finally ActiveMQ as the message
broker.<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/pom.xml}</plain-text-body>As
we use spring xml configuration for the ActiveMQ JMS broker we need this
dependency:<plain-text-body>{snippet:id=e2|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/pom.xml}</plain-text-body></p><h2
id="Tutorial-JmsRemoting-WritingtheServer">Writing the Server</h2><h3
id="Tutorial-JmsRemoting-CreatetheSpringService">Create the Spring
Service</h3><p>For this example the Spring service (our business service) on
the server will be a simple multiplier which trebles in the received
value.<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/serv
er/Multiplier.java}</plain-text-body>And the implementation of this service
is:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/Treble.java}</plain-text-body>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 <strong>multiplier</strong>.</p><h3
id="Tutorial-JmsRemoting-DefinetheCamelRoutes">Define the Camel
Routes</h3><p><plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/server/ServerRoutes.java}</plain-text-body>This
defines a Camel route <em>from</em> the JMS queue named
<strong>numbers</strong> <em>to</em> the Spring <a shape="rect"
href="bean.html">bean</a> named <strong>multiplier</strong>. Camel will create
a consumer to the JMS queue which forwards all received messages onto the the
Spring bean,
using the method named <strong>multiply</strong>.</p><h3
id="Tutorial-JmsRemoting-ConfigureSpring">Configure Spring</h3><p>The Spring
config file is placed under <code>META-INF/spring</code> as this is the default
location used by the <a shape="rect" href="camel-maven-plugin.html">Camel Maven
Plugin</a>, which we will later use to run our server.<br clear="none"> 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
<strong>bean:</strong> namespace and springs <strong>context:</strong>. For
configuring ActiveMQ we use <strong>broker:</strong> and for Camel we of course
have <strong>camel:</strong>. 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 <a shape="rect"
href="xml-reference.html">Xml Re
ference</a> for further
details.<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}</plain-text-body>We
use Spring annotations for doing IoC dependencies and its component-scan
features comes to the rescue as it scans for spring annotations in the given
package
name:<plain-text-body>{snippet:id=e2|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}</plain-text-body>Camel
will of course not be less than Spring in this regard so it supports a similar
feature for scanning of Routes. This is configured as shown below.<br
clear="none"> Notice that we also have enabled the <a shape="rect"
href="camel-jmx.html">JMXAgent</a> so we will be able to introspect the Camel
Server with a JMX
Console.<plain-text-body>{snippet:id=e3|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}</plain-
text-body>The ActiveMQ JMS broker is also configured in this xml file. We set
it up to listen on TCP port
61610.<plain-text-body>{snippet:id=e4|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}</plain-text-body>As
this examples uses JMS then Camel needs a <a shape="rect" href="jms.html">JMS
component</a> that is connected with the ActiveMQ broker. This is configured as
shown
below:<plain-text-body>{snippet:id=e5|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}</plain-text-body><strong>Notice:</strong>
The <a shape="rect" href="jms.html">JMS component</a> is configured in
standard Spring beans, but the gem is that the bean id can be referenced from
Camel routes - meaning we can do routing using the JMS Component by just using
<strong>jms:</strong> prefix in the route URI. What happens is that Camel will
find in the Spring Registry for a bean with the id="jms".
Since the bean id can have arbitrary name you could have named it
id="jmsbroker" and then referenced to it in the routing as
<code>from="jmsbroker:queue:numbers).to("multiplier");</code><br clear="none">
We use the vm protocol to connect to the ActiveMQ server as its embedded in
this application.</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>component-scan</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Defines the package to be scanned for Spring stereotype
annotations, in this case, to load the "multiplier" bean</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>camel-context</p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Defines the package to be
scanned for Camel routes. Will find the <code>ServerRoutes</code> class and
create the routes contained within it</p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p>jms bean</p></td><td colspan="1" rowsp
an="1" class="confluenceTd"><p>Creates the Camel JMS
component</p></td></tr></tbody></table></div><h3
id="Tutorial-JmsRemoting-RuntheServer">Run the Server</h3><p>The Server is
started using the <code>org.apache.camel.spring.Main</code> class that can
start camel-spring application out-of-the-box. The Server can be started in
several flavors:</p><ul class="alternate"><li>as a standard java main
application - just start the <code>org.apache.camel.spring.Main</code>
class</li><li>using maven jave:exec</li><li>using <a shape="rect"
href="camel-run-maven-goal.html">camel:run</a></li></ul><p>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.<br clear="none"> The server is
started with:<br clear="none"> <code>mvn compile exec:java
-PCamelServer</code></p><h2 id="Tutorial-JmsRemoting-WritingTheClients">Writing
The Clients</h2><p>This sample has three clients demonstrating different Camel
techniques for
communication</p><ul class="alternate"><li>CamelClient using the <a
shape="rect" href="producertemplate.html">ProducerTemplate</a> for Spring
template style coding</li><li>CamelRemoting using Spring
Remoting</li><li>CamelEndpoint using the Message Endpoint EIP pattern using a
neutral Camel API</li></ul><h3
id="Tutorial-JmsRemoting-ClientUsingTheProducerTemplate">Client Using The
ProducerTemplate</h3><p>We will initially create a client by directly using
<code>ProducerTemplate</code>. We will later create a client which uses Spring
remoting to hide the fact that messaging is being
used.<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/camel-client.xml}</plain-text-body><plain-text-body>{snippet:id=e2|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/camel-client.xml}</plain-text-body><plain-text-body>{snippet:id=e3|lang=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/camel
-client.xml}</plain-text-body>The client will not use the <a shape="rect"
href="camel-maven-plugin.html">Camel Maven Plugin</a> so the Spring XML has
been placed in <em>src/main/resources</em> to not conflict with the server
configs.</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>camelContext</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The Camel context is defined but does not contain any
routes</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>template</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The <code>ProducerTemplate</code> is used to place
messages onto the JMS queue</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>jms bean</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>This initialises the Camel JMS component, allowing us
to place messages onto the queue</p></td></tr></tbody></table></div><p>And the
CamelClient source c
ode:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/client/CamelClient.java}</plain-text-body>The
<code>ProducerTemplate</code> is retrieved from a Spring
<code>ApplicationContext</code> and used to manually place a message on the
"numbers" JMS queue. The <code>requestBody</code> method will use the exchange
pattern InOut, which states that the call should be synchronous, and that the
caller expects a response.</p><p>Before running the client be sure that both
the ActiveMQ broker and the <code>CamelServer</code> are running.</p><h3
id="Tutorial-JmsRemoting-ClientUsingSpringRemoting">Client Using Spring
Remoting</h3><p><a shape="rect" href="spring-remoting.html">Spring Remoting</a>
"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.<plain-text-body>{snippet:id=e1|la
ng=xml|url=camel/trunk/examples/camel-example-spring-jms/src/main/resources/camel-client-remoting.xml}</plain-text-body>The
snippet above only illustrates the different and how Camel easily can setup
and use Spring Remoting in one line configurations.</p><p>The
<strong>proxy</strong> will create a proxy service bean for you to use to make
the remote invocations. The <strong>serviceInterface</strong> property details
which Java interface is to be implemented by the proxy.
The <strong>serviceUrl</strong> 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
<strong>id</strong> property is the name that will be the given to the bean
when it is exposed through the Spring <code>ApplicationContext</code>. We will
use this name to retrieve the service in our client. I have named the bean
<em>multiplierProxy</em> simply to highlight that it is not the
same multiplier bean as is being used by <code>CamelServer</code>. 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.</p><p>And the Java client source
code:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/client/CamelClientRemoting.java}</plain-text-body>Again,
the client is similar to the original client, but with some important
differences.</p><ol><li>The Spring context is created with the new
<em>camel-client-remoting.xml</em></li><li>We retrieve the proxy bean instead
of a <code>ProducerTemplate</code>. In a non-trivial example you would have the
bean injected as in the standard Spring manner.</li><li>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 insid
e our Java code.</li></ol><h3
id="Tutorial-JmsRemoting-ClientUsingMessageEndpointEIPPattern">Client Using
Message Endpoint EIP Pattern</h3><p>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 <strong>all</strong> 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.</p><p>Okay enough talk, show me the
code!<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-spring-jms/src/main/java/org/apache/camel/example/client/CamelClientEndpoint.java}</plain-text-body>Switching
to a different component is just a matter of using the correct endpoint. So if
we had defined a TCP
endpoint as: <code>"mina:tcp://localhost:61610"</code> 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.</p><h3
id="Tutorial-JmsRemoting-RuntheClients">Run the Clients</h3><p>The Clients is
started using their main class respectively.</p><ul class="alternate"><li>as a
standard java main application - just start their main class</li><li>using
maven jave:exec</li></ul><p>In this sample we start the clients using maven:<br
clear="none"> <code>mvn compile exec:java -PCamelClient</code><br clear="none">
<code>mvn compile exec:java -PCamelClientRemoting</code><br clear="none">
<code>mvn compile exec:java -PCamelClientEndpoint</code></p><p>Also see the
Maven <code>pom.xml</code> file how the profiles for the clients is
defined.</p><h2 id="Tutorial-JmsRemoting-UsingtheCamelMavenPlugin">Using the
Camel Maven Plugin</h2><p>The <a shape="rect"
href="camel-maven-plugin.html">Camel Maven Plugin</a> 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.</p><parameter
ac:name="title">pom.xml</parameter><plain-text-body><build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
@@ -348,13 +84,10 @@ public static void main(final String[] a
</plugin>
</plugins>
</build>
-]]></script>
-</div></div><p>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 <code>mvn
camel:run</code>.</p><h2 id="Tutorial-JmsRemoting-UsingCamelJMX">Using Camel
JMX</h2><p>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:
<code>service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/camel</code>. Notice
that Camel will log at INFO level the JMX Connector URI:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[...
+</plain-text-body><p>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 <code>mvn
camel:run</code>.</p><h2 id="Tutorial-JmsRemoting-UsingCamelJMX">Using Camel
JMX</h2><p>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:
<code>service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/camel</code>. Notice
that Camel will log at INFO level the JMX Connector URI:</p><plain-text-body>...
DefaultInstrumentationAgent INFO JMX connector thread started on
service:jmx:rmi:///jndi/rmi://claus-acer:1099/jmxrmi/camel
...
-]]></script>
-</div></div><p>In the screenshot below we can see the route and its
performance metrics:<br clear="none"> <span
class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image"
src="tutorial-jmsremoting.data/jconsole_jms_tutorial.PNG"
data-image-src="/confluence/download/attachments/82923/jconsole_jms_tutorial.PNG?version=1&modificationDate=1214345078000&api=v2"
data-unresolved-comment-count="0" data-linked-resource-id="59672517"
data-linked-resource-version="1" data-linked-resource-type="attachment"
data-linked-resource-default-alias="jconsole_jms_tutorial.PNG"
data-base-url="https://cwiki.apache.org/confluence"
data-linked-resource-content-type="image/png"
data-linked-resource-container-id="82923"
data-linked-resource-container-version="42"></span></p><h2
id="Tutorial-JmsRemoting-SeeAlso">See Also</h2><ul><li><a shape="rect"
class="external-link" href="http://aminsblog.wordpress.com/2008/05/06/15/"
rel="nofollow">Spring Remoting with JMS Example</a> on <a sh
ape="rect" class="external-link" href="http://aminsblog.wordpress.com/"
rel="nofollow">Amin Abbaspour's Weblog</a></li></ul></div>
+</plain-text-body><p>In the screenshot below we can see the route and its
performance metrics:<br clear="none"> <span
class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image"
src="tutorial-jmsremoting.data/jconsole_jms_tutorial.PNG"
data-image-src="/confluence/download/attachments/82923/jconsole_jms_tutorial.PNG?version=1&modificationDate=1214345078000&api=v2"
data-unresolved-comment-count="0" data-linked-resource-id="59672517"
data-linked-resource-version="1" data-linked-resource-type="attachment"
data-linked-resource-default-alias="jconsole_jms_tutorial.PNG"
data-base-url="https://cwiki.apache.org/confluence"
data-linked-resource-content-type="image/png"
data-linked-resource-container-id="82923"
data-linked-resource-container-version="43"></span></p><h2
id="Tutorial-JmsRemoting-SeeAlso">See Also</h2><ul><li><a shape="rect"
class="external-link" href="http://aminsblog.wordpress.com/2008/05/06/15/"
rel="nofollow">Spring Remoting with JMS Example</a> on
<a shape="rect" class="external-link" href="http://aminsblog.wordpress.com/"
rel="nofollow">Amin Abbaspour's Weblog</a></li></ul></div>
</td>
<td valign="top">
<div class="navigation">
Modified: websites/production/camel/content/urlrewrite.html
==============================================================================
--- websites/production/camel/content/urlrewrite.html (original)
+++ websites/production/camel/content/urlrewrite.html Fri Aug 25 08:22:01 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: UrlRewrite
@@ -86,78 +75,13 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2
id="UrlRewrite-UrlRewriteComponent">UrlRewrite
Component</h2><p><strong>Available as of Camel 2.11</strong></p><p>The
<code>camel-urlrewrite</code> component allows to plugin url rewrite
functionality to <a shape="rect" href="http.html">HTTP</a>, <a shape="rect"
href="http4.html">HTTP4</a>, <a shape="rect" href="jetty.html">Jetty</a>, or <a
shape="rect" href="ahc.html">AHC</a> components. This component integrates the
<a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project with Apache Camel. This allows you
to use the capabilities from the url rewrite project with your Camel
routes.</p><p>This component <strong>requires</strong> that your Camel routes
starts from a servlet based endpoint such as <a shape="rect"
href="jetty.html">Jetty</a> or <a shape="rect"
href="servlet.html">SERVLET</a>.</p><h3
id="UrlRewrite-Options">Options</h3><p>The <code>camel-urlrewrite</code
> component offers the following options</p><div
> class="confluenceTableSmall"><div class="table-wrap"><table
> class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
> class="confluenceTh"><p>Name</p></th><th colspan="1" rowspan="1"
> class="confluenceTh"><p>Default Value</p></th><th colspan="1" rowspan="1"
> class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
> rowspan="1" class="confluenceTd"><p><code>conf</code></p></td><td
> colspan="1" rowspan="1"
> class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
> rowspan="1" class="confluenceTd"><p>To use a custom
> <code>org.tuckey.web.filters.urlrewrite.Conf</code> as the configuration for
> the url rewrite filter.</p></td></tr><tr><td colspan="1" rowspan="1"
> class="confluenceTd"><p><code>configFile</code></p></td><td colspan="1"
> rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td
> colspan="1" rowspan="1" class="confluenceTd"><p>Refers to a file name to
> load as the configuration file. The file should be X
ML file.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>modRewriteConfFile</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Refers to a file name to load as the Apache
mod_rewrite style based configuration file. This file should be a plain text
file.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>modRewriteConfText</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Is used to quickly configure a Apache
mod_rewrite style based configuration without using a
file.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>useQueryString</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Whether to use query strings in the url
rewrite r
ules.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>useContext</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Whether to include context-path or not in
the matching. Will be default remove the context-path.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>defaultMatchType</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>"regex"</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Which match type to use. Can be
either "wildcard" or "regex".</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>decodeUsing</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>To use a specific encoding such as "utf-8"
or "iso-8859-1".</p></td></tr></tbody></table></div></div>
-
-
-<h3 id="UrlRewrite-Usage">Usage</h3><p>The following component producers
supports using together with the <code>camel-urlrewrite</code> component: <a
shape="rect" href="http.html">HTTP</a>, <a shape="rect"
href="http4.html">HTTP4</a> and <a shape="rect"
href="jetty.html">Jetty</a>.</p><div class="confluenceTableSmall"><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Component</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Class</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>camel-http</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code> <strong>C</strong></code><strong>amel 2.15
or
older:</strong><code> org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>camel-jetty</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code><code><strong>C</str
ong></code><strong>amel 2.15 or
older:</strong>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>camel-http4</code></p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p><code><code><strong>C</strong></code><strong>amel 2.15
or
older:</strong>org.apache.camel.component.urlrewrite.http4.HttpUrlRewrite</code></p></td></tr></tbody></table></div></div>
-
-
-<p> </p><p>From <strong>Camel 2.16</strong> onwards this is no
longer necessary and there is only one class, which
is <code>org.apache.camel.component.urlrewrite.HttpUrlRewrite</code> that
works with any of the components.</p><p> </p><p>You setup the url rewrite
as a bean of the type
<code>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code> (when
using <a shape="rect" href="http.html">HTTP</a> component) as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-HttpUrlRewrite myRewrite = new HttpUrlRewrite();
-myRewrite.setConfigFile("example/urlrewrite2.xml");
-]]></script>
-</div></div>And in XML DSL you can do:<div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<bean id="myRewrite"
class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
- <property name="configFile"
value="example/urlrewrite2.xml"/>
+<div class="wiki-content maincontent"><h2
id="UrlRewrite-UrlRewriteComponent">UrlRewrite
Component</h2><p><strong>Available as of Camel 2.11</strong></p><p>The
<code>camel-urlrewrite</code> component allows to plugin url rewrite
functionality to <a shape="rect" href="http.html">HTTP</a>, <a shape="rect"
href="http4.html">HTTP4</a>, <a shape="rect" href="jetty.html">Jetty</a>, or <a
shape="rect" href="ahc.html">AHC</a> components. This component integrates the
<a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project with Apache Camel. This allows you
to use the capabilities from the url rewrite project with your Camel
routes.</p><p>This component <strong>requires</strong> that your Camel routes
starts from a servlet based endpoint such as <a shape="rect"
href="jetty.html">Jetty</a> or <a shape="rect"
href="servlet.html">SERVLET</a>.</p><h3
id="UrlRewrite-Options">Options</h3><p>The <code>camel-urlrewrite</code
> component offers the following options</p><parameter
> ac:name="class">confluenceTableSmall</parameter><rich-text-body><div
> class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
> rowspan="1" class="confluenceTh"><p>Name</p></th><th colspan="1" rowspan="1"
> class="confluenceTh"><p>Default Value</p></th><th colspan="1" rowspan="1"
> class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
> rowspan="1" class="confluenceTd"><p><code>conf</code></p></td><td
> colspan="1" rowspan="1"
> class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
> rowspan="1" class="confluenceTd"><p>To use a custom
> <code>org.tuckey.web.filters.urlrewrite.Conf</code> as the configuration for
> the url rewrite filter.</p></td></tr><tr><td colspan="1" rowspan="1"
> class="confluenceTd"><p><code>configFile</code></p></td><td colspan="1"
> rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td
> colspan="1" rowspan="1" class="confluenceTd"><p>Refers to a file name to
> load as the
configuration file. The file should be XML file.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>modRewriteConfFile</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Refers to a file name to load as the Apache
mod_rewrite style based configuration file. This file should be a plain text
file.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>modRewriteConfText</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Is used to quickly configure a Apache
mod_rewrite style based configuration without using a
file.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>useQueryString</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Whether
to use query strings in the url rewrite rules.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>useContext</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Whether to include context-path or not in
the matching. Will be default remove the context-path.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>defaultMatchType</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>"regex"</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Which match type to use. Can be
either "wildcard" or "regex".</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>decodeUsing</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>To use a specific encoding such as "utf-8"
or "iso-8859-1".</p></td></tr></tbody></tabl
e></div></rich-text-body><h3 id="UrlRewrite-Usage">Usage</h3><p>The following
component producers supports using together with the
<code>camel-urlrewrite</code> component: <a shape="rect"
href="http.html">HTTP</a>, <a shape="rect" href="http4.html">HTTP4</a> and <a
shape="rect" href="jetty.html">Jetty</a>.</p><parameter
ac:name="class">confluenceTableSmall</parameter><rich-text-body><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Component</p></th><th colspan="1"
rowspan="1" class="confluenceTh"><p>Class</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>camel-http</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code> <strong>C</strong></code><strong>amel 2.15
or
older:</strong><code> org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>camel-jetty</code></p></td><td colspan=
"1" rowspan="1"
class="confluenceTd"><p><code><code><strong>C</strong></code><strong>amel 2.15
or
older:</strong>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>camel-http4</code></p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p><code><code><strong>C</strong></code><strong>amel 2.15
or
older:</strong>org.apache.camel.component.urlrewrite.http4.HttpUrlRewrite</code></p></td></tr></tbody></table></div></rich-text-body><p> </p><p>From <strong>Camel
2.16</strong> onwards this is no longer necessary and there is only one class,
which is <code>org.apache.camel.component.urlrewrite.HttpUrlRewrite</code>
that works with any of the components.</p><p> </p><p>You setup the url
rewrite as a bean of the type
<code>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</code> (when
using <a shape="rect" href="http.html">HTTP</a> component) as shown
below:<plain-text-body>{
snippet:id=e1|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteTest.java}</plain-text-body>And
in XML DSL you can do:</p><parameter
ac:name="">xml</parameter><plain-text-body><bean id="myRewrite"
class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
+ <property name="configFile" value="example/urlrewrite2.xml"/>
</bean>
-]]></script>
-</div></div><p>In the bean above we configure it with the
<code>configFile</code> option which is a XML <a shape="rect"
class="external-link" href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> configuration file.</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<urlrewrite>
-
- <!-- this is a rule where we map from /products/nnn to
- an url that is a jps page with the product id as a parameter -->
- <rule>
- <from>/products/([0-9]+)</from>
- <to>/products/index.jsp?product_id=$1</to>
- </rule>
-
-</urlrewrite>
-]]></script>
-</div></div><div class="confluence-information-macro
confluence-information-macro-tip"><span class="aui-icon aui-icon-small
aui-iconfont-approve confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>See more details at <a
shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> for how to configure the XML rules
file.</p></div></div><p>In the Camel routes, you bridge the endpoints, and
refer to the url rewrite on the producer endpoint as shown below. Notice how we
refer to our url rewrite filter using the <code>urlRewrite</code> option in the
endpoint uri.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true")
-
.to("jetty:http://localhost:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=false&urlRewrite=#myRewrite");
-]]></script>
-</div></div><h4 id="UrlRewrite-Usingloadbalancingeip">Using load balancing
eip</h4><p>You can also use Camel's <a shape="rect"
href="load-balancer.html">Load Balancer</a> together with <a shape="rect"
href="urlrewrite.html">UrlRewrite</a>, as shown below. In this code we use the
failover load balancer, to failover to the 2nd endpoint (which goes to myapp3)
in case of any exception thrown.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// we want to use the failover loadbalancer
-// to have it to react we must set throwExceptionOnFailure=true, which is also
the default value
-// so we can omit configuring this option
-from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true")
- .loadBalance().failover(Exception.class)
-
.to("jetty:http://localhost:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=true&urlRewrite=#myRewrite")
-
.to("jetty:http://localhost:{{port2}}/myapp3?bridgeEndpoint=true&throwExceptionOnFailure=true&urlRewrite=#myRewrite");
-]]></script>
-</div></div><h4 id="UrlRewrite-UsingApachemod_rewritestyleconfiguration">Using
Apache mod_rewrite style configuration</h4><p>The <a shape="rect"
class="external-link" href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project also supports Apache mod_rewrite
style configuration.</p><p>For example to configure the bean using the
modRewriteConfFile property to refer to a mod configuration file:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<bean id="myRewrite"
class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
- <property name="modRewriteConfFile"
value="example/modrewrite.cxf"/>
+</plain-text-body><p>In the bean above we configure it with the
<code>configFile</code> option which is a XML <a shape="rect"
class="external-link" href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> configuration
file.<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-urlrewrite/src/test/resources/example/urlrewrite2.xml}</plain-text-body></p><rich-text-body><p>See
more details at <a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> for how to configure the XML rules
file.</p></rich-text-body><p>In the Camel routes, you bridge the endpoints, and
refer to the url rewrite on the producer endpoint as shown below. Notice how we
refer to our url rewrite filter using the <code>urlRewrite</code> option in the
endpoint
uri.<plain-text-body>{snippet:id=e2|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty
/JettyUrlRewriteTest.java}</plain-text-body></p><h4
id="UrlRewrite-Usingloadbalancingeip">Using load balancing eip</h4><p>You can
also use Camel's <a shape="rect" href="load-balancer.html">Load Balancer</a>
together with <a shape="rect" href="urlrewrite.html">UrlRewrite</a>, as shown
below. In this code we use the failover load balancer, to failover to the 2nd
endpoint (which goes to myapp3) in case of any exception
thrown.<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteLoadBalanceFailoverTest.java}</plain-text-body></p><h4
id="UrlRewrite-UsingApachemod_rewritestyleconfiguration">Using Apache
mod_rewrite style configuration</h4><p>The <a shape="rect"
class="external-link" href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project also supports Apache mod_rewrite
style configuration.</p><p>For example to configure the bean using the modRewr
iteConfFile property to refer to a mod configuration file:</p><parameter
ac:name="">xml</parameter><plain-text-body><bean id="myRewrite"
class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
+ <property name="modRewriteConfFile" value="example/modrewrite.cxf"/>
</bean>
-]]></script>
-</div></div><p>Which then can contain mod rewrite rules and the likes:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: plain; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-# our custom mod rewrite rule
-RewriteRule page/([^/\.]+)/?$ index.php?page=$1 [L]
-
-]]></script>
-</div></div>You can have multiple RewriteRule rules and other configurations.
See more details at <a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project.<h3
id="UrlRewrite-Writingcustomurlrewriters">Writing custom url
rewriters</h3><p>You can implement your custom url rewriters by implementing
either of the following interfaces from the component of
choice:</p><p>Component(s): camel-http / camel-jetty</p><ul
class="alternate"><li><code>org.apache.camel.component.http.UrlRewrite</code></li><li><code>org.apache.camel.component.http.HttpServletUrlRewrite</code></li></ul><p>Component(s):
camel-http4</p><ul
class="alternate"><li><code>org.apache.camel.component.http4.UrlRewrite</code></li><li><code>org.apache.camel.component.http4.HttpServletUrlRewrite</code></li></ul><p>The
former is a simple and generic interface, which is not dependent on the
Servlet API.<br clear="none"> The later is servlet based which requ
ires the Camel route to start from a servlet based component such as <a
shape="rect" href="jetty.html">Jetty</a> or <a shape="rect"
href="servlet.html">SERVLET</a>. The servlet based is more feature rich and
that's the API we use to integrate with the <a shape="rect"
class="external-link" href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project in this
<code>camel-urlrewrite</code> component.</p><h4
id="UrlRewrite-Asimpleurlrewritefilter">A simple url rewrite filter</h4><p>In
this example we implement a custom
<code>org.apache.camel.component.http.UrlRewrite</code> that just replaces
yahoo with google. Mind this example is based on a unit test, and only for
demonstration purposes. As its java code you have full power to implement more
complex logic.</p><p>The url parameter contains the full url including
scheme://hostname:port/path?query. The relativeUrl parameter is the url without
the endpoint configured path. Notice this option may be null if
the url doesn't start with the endpoint configured path.</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-/**
- * A very simple url rewrite that replaces yahoo with google in the url.
- * <p/>
- * This is only used for testing purposes.
- */
-public class GoogleUrlRewrite implements UrlRewrite {
-
- @Override
- public String rewrite(String url, String relativeUrl, Producer producer) {
- return url.replaceAll("yahoo", "google");
- }
-}
-]]></script>
-</div></div><h3 id="UrlRewrite-SeeAlso">See Also</h3>
-<ul><li><a shape="rect" href="configuring-camel.html">Configuring
Camel</a></li><li><a shape="rect"
href="component.html">Component</a></li><li><a shape="rect"
href="endpoint.html">Endpoint</a></li><li><a shape="rect"
href="getting-started.html">Getting Started</a></li></ul><ul
class="alternate"><li><a shape="rect" href="jetty.html">Jetty</a></li><li><a
shape="rect" href="http4.html">HTTP4</a></li><li><a shape="rect"
href="http.html">HTTP</a></li><li><a shape="rect"
href="ahc.html">AHC</a></li></ul></div>
+</plain-text-body><p>Which then can contain mod rewrite rules and the
likes:<plain-text-body>{snippet:e1|lang=none|url=camel/trunk/components/camel-urlrewrite/src/test/resources/example/modrewrite.cfg}</plain-text-body>You
can have multiple RewriteRule rules and other configurations. See more details
at <a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project.</p><h3
id="UrlRewrite-Writingcustomurlrewriters">Writing custom url
rewriters</h3><p>You can implement your custom url rewriters by implementing
either of the following interfaces from the component of
choice:</p><p>Component(s): camel-http / camel-jetty</p><ul
class="alternate"><li><code>org.apache.camel.component.http.UrlRewrite</code></li><li><code>org.apache.camel.component.http.HttpServletUrlRewrite</code></li></ul><p>Component(s):
camel-http4</p><ul
class="alternate"><li><code>org.apache.camel.component.http4.UrlRewrite</code></li><li><code>org.ap
ache.camel.component.http4.HttpServletUrlRewrite</code></li></ul><p>The former
is a simple and generic interface, which is not dependent on the Servlet
API.<br clear="none"> The later is servlet based which requires the Camel route
to start from a servlet based component such as <a shape="rect"
href="jetty.html">Jetty</a> or <a shape="rect" href="servlet.html">SERVLET</a>.
The servlet based is more feature rich and that's the API we use to integrate
with the <a shape="rect" class="external-link"
href="http://code.google.com/p/urlrewritefilter/"
rel="nofollow">UrlRewriteFilter</a> project in this
<code>camel-urlrewrite</code> component.</p><h4
id="UrlRewrite-Asimpleurlrewritefilter">A simple url rewrite filter</h4><p>In
this example we implement a custom
<code>org.apache.camel.component.http.UrlRewrite</code> that just replaces
yahoo with google. Mind this example is based on a unit test, and only for
demonstration purposes. As its java code you have full power to implement more
comp
lex logic.</p><p>The url parameter contains the full url including
scheme://hostname:port/path?query. The relativeUrl parameter is the url without
the endpoint configured path. Notice this option may be null if the url doesn't
start with the endpoint configured
path.<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/GoogleUrlRewrite.java}</plain-text-body><parameter
ac:name=""><a shape="rect" href="endpoint-see-also.html">Endpoint See
Also</a></parameter></p><ul class="alternate"><li><a shape="rect"
href="jetty.html">Jetty</a></li><li><a shape="rect"
href="http4.html">HTTP4</a></li><li><a shape="rect"
href="http.html">HTTP</a></li><li><a shape="rect"
href="ahc.html">AHC</a></li></ul></div>
</td>
<td valign="top">
<div class="navigation">