camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Using CamelProxy
Date Mon, 05 Apr 2010 13:47:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="">Using
     <h4>Page <b>edited</b> by             <a href="">Claus
     <div class="notificationGreySide">
         <h2><a name="UsingCamelProxy-UsingCamelProxy"></a>Using CamelProxy</h2>

<p>Camel allows you to proxy a producer sending to an <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> by a regular interface. Then when clients using this
interface can work with it as if its regular java code but in reality its proxied and does
a <a href="/confluence/display/CAMEL/Request+Reply" title="Request Reply">Request Reply</a>
to a given endpoint.</p>

<h3><a name="UsingCamelProxy-ProxyfromSpring"></a>Proxy from Spring</h3>

<p>You can define a proxy in the spring XML file as shown below</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"myCamel"</span> xmlns=<span class="code-quote">""</span>&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- create a proxy
that will route to the direct:start endpoint when invoked --&gt;</span></span>
    &lt;proxy id=<span class="code-quote">"myProxySender"</span>
           serviceInterface=<span class="code-quote">"org.apache.camel.spring.config.MyProxySender"</span>
           serviceUrl=<span class="code-quote">"direct:start"</span>/&gt;

    &lt;!-- this is the route that our proxy will routed when invoked
         and the output from this route is returned as reply on the proxy --&gt; 
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;transform&gt;</span>
            <span class="code-tag">&lt;simple&gt;</span>Bye ${body}<span
        <span class="code-tag">&lt;/transform&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>

<p>Now the client can grab this bean using regular spring bean coding and invoke it
as if its just another bean.<br/>
The code is based on an unit test but proves the point:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ApplicationContext ac = <span class="code-keyword">new</span>
ClassPathXmlApplicationContext(<span class="code-quote">"org/apache/camel/spring/config/AnotherCamelProxyTest.xml"</span>);

MyProxySender sender = (MyProxySender) ac.getBean(<span class="code-quote">"myProxySender"</span>);
<span class="code-object">String</span> reply = sender.hello(<span class="code-quote">"Camel"</span>);

assertEquals(<span class="code-quote">"Bye Camel"</span>, reply);

<p>In <b>Camel 2.3</b> you can define that proxied methods which has <tt>void</tt>
as return type, can be treated as <a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=Fire+and+Forget&amp;linkCreation=true&amp;fromPageId=5144992"
class="createlink">Event Message</a> messaging style (aka InOnly). This is done by
setting the attribute <tt>voidAsInOnly=true</tt> in the &lt;proxy/&gt;
tag. If you let it be the default value, which is <tt>InOut</tt> then a <b>void</b>
method will still wait for the message to be completely routed. This is not always what you
want and hence you can use this option to turn it onto a fire and forget style. BTW: You can
also add the <tt>@InOnly</tt> annotation to the method on the interface to tell
Camel that this method is InOnly.</p>

<h3><a name="UsingCamelProxy-ProxyfromJava"></a>Proxy from Java</h3>

<p>You can also create a proxy from regular Java using a ProxyHelper as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    Endpoint endpoint = context.getEndpoint(<span class="code-quote">"direct:start"</span>);
    MyProxySender sender = ProxyHelper.createProxy(endpoint, MyProxySender.class);

<h3><a name="UsingCamelProxy-ProxywithAnnotation"></a>Proxy with Annotation</h3>

<p>Another way to configure the proxy from java is by using the @Produce annotation.
Also see <a href="/confluence/display/CAMEL/POJO+Producing" title="POJO Producing">POJO
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Produce(uri=<span class="code-quote">"direct:start"</span>)
MyProxySender sender;

<p>This basically does the same as ProxyHelper.createProxy.</p>

<h3><a name="UsingCamelProxy-WhatissendontheMessage"></a>What is send on
the Message</h3>
<p>When using a proxy Camel will send the message payload as a <tt>org.apache.camel.component.bean.BeanInvocation</tt>
object which holds the details of which method was invoked and what the argument was.</p>

<h3><a name="UsingCamelProxy-TurningtheBeanInvocationintoafirstclasspayload"></a>Turning
the BeanInvocation into a first class payload</h3>
<p><b>Available as of Camel 2.1</b></p>

<p>If you proxied method signature only have one parameter such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">String</span> hello(<span class="code-object">String</span>

<p>Then it gives another advantage in Camel as it allows Camel to regard the value passed
in to this single parameter as the <em>real</em> payload. In other words if you
pass in <tt>Camel</tt> to the hello method, then Camel can <em>see</em>
that as a <tt>java.lang.String</tt> payload with the value of <tt>Camel</tt>.
This gives you a great advantage as you can use the proxy as first class services with Camel.</p>

<p>You can proxy Camel and let clients use the pure and clean interfaces as if Camel
newer existed. Then Camel can proxy the invocation and receive the input passed into the single
method parameter and regard that as if it was <em>just</em> the message payload.</p>

<p>Okay lets try that with an example</p>

<h4><a name="UsingCamelProxy-Examplewithproxyusingsingleparametermethods."></a>Example
with proxy using single parameter methods.</h4>

<p>At first we have the interface we wish to proxy</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> OrderService {

    <span class="code-object">String</span> submitOrderStringReturnString(<span
class="code-object">String</span> order);

    Document submitOrderStringReturnDocument(<span class="code-object">String</span>

    <span class="code-object">String</span> submitOrderDocumentReturnString(Document

    Document submitOrderDocumentReturnDocument(Document order);

    void doNothing(<span class="code-object">String</span> s);

    <span class="code-object">Integer</span> invalidReturnType(<span class="code-object">String</span>

    <span class="code-object">String</span> doAbsolutelyNothing();


<p>Notice that all methods have single parameters. The return type is optional, as you
can see one of them is void.<br/>
Also what you should know is that Camel uses its <a href="/confluence/display/CAMEL/Type+Converter"
title="Type Converter">Type Converter</a> mechanism to adapt to the types defined
on the methods.</p>

<p>This allows us to easily use <tt>org.w3c.dom.Document</tt> and <tt>String</tt>
types with no hazzle.</p>

<p>Okay then we have the following route where we route using a <a href="/confluence/display/CAMEL/Content+Based+Router"
title="Content Based Router">Content Based Router</a> that is XML based. See that
we use <a href="/confluence/display/CAMEL/XPath" title="XPath">XPath</a> in the
choices to route the message depending on its a book order or not.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
        .when(xpath(<span class="code-quote">"/order/@type = 'book'"</span>)).to(<span
        .otherwise().to(<span class="code-quote">"direct:other"</span>)

from(<span class="code-quote">"direct:book"</span>).transform(constant(<span
class="code-quote">"&lt;order id=\"</span>123\<span class="code-quote">"&gt;OK&lt;/order&gt;"</span>));

from(<span class="code-quote">"direct:other"</span>).transform(constant(<span

<p>Now there is a couple of tests that shows using the Camel Proxy how we can easily
invoke the proxy and do not know its actually Camel doing some routing underneath.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Endpoint endpoint = context.getEndpoint(<span class="code-quote">"direct:start"</span>);
OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class);

<span class="code-object">String</span> reply = service.submitOrderStringReturnString(<span
class="code-quote">"&lt;order type=\"</span>book\<span class="code-quote">"&gt;Camel
in action&lt;/order&gt;"</span>);
assertEquals(<span class="code-quote">"&lt;order id=\"</span>123\<span
class="code-quote">"&gt;OK&lt;/order&gt;"</span>, reply);

<p>And this one below shows using different types that Camel adapts to.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Endpoint endpoint = context.getEndpoint(<span class="code-quote">"direct:start"</span>);
OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class);

Document doc = context.getTypeConverter().convertTo(Document.class, <span class="code-quote">"&lt;order
type=\"</span>book\<span class="code-quote">"&gt;Camel in action&lt;/order&gt;"</span>);

Document reply = service.submitOrderDocumentReturnDocument(doc);
<span class="code-object">String</span> s = context.getTypeConverter().convertTo(<span
class="code-object">String</span>.class, reply);
assertEquals(<span class="code-quote">"&lt;order id=\"</span>123\<span
class="code-quote">"&gt;OK&lt;/order&gt;"</span>, s);

<p>Isn't this cool?</p>

<h3><a name="UsingCamelProxy-Seealso"></a>See also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/User+Guide" title="User Guide">User Guide</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-JmsRemoting" title="Tutorial-JmsRemoting">Tutorial&#45;JmsRemoting</a></li>

     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>

       <a href="">View
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message