camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Tutorial-Example-ReportIncident-Part5
Date Wed, 29 Aug 2012 16:24:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part5">Tutorial-Example-ReportIncident-Part5</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >In the next part&#39;s look at using XML to create the route instead of Java code. Then it might be even more readable by non developers.  <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h2. [#Resources] <br>* {attachments:patterns=part-five.zip} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Links <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Tutorial-Example-ReportIncident-Part5-Part5"></a>Part 5</h2>

<h3><a name="Tutorial-Example-ReportIncident-Part5-...ContinuedfromPart4"></a>... Continued from Part 4</h3>
<p>We continue from part 4 where we have the routing in place. However as you might have noticed we aren't quiet there yet with a nice solution, we are still coding to much. In this part we will look into to address these two concerns:</p>
<ul class="alternate" type="square">
	<li>Starting Camel automatically</li>
	<li>Using CXF directly</li>
</ul>


<h2><a name="Tutorial-Example-ReportIncident-Part5-StartingCamelautomatically"></a>Starting Camel automatically</h2>
<p>Our current deployment model is as a war and we have the <tt>web.xml</tt> to help start things. Well in fact we will leverage Spring to start the world <img class="emoticon" src="/confluence/images/icons/emoticons/wink.gif" height="20" width="20" align="absmiddle" alt="" border="0"/>. We use it's ContextListener</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
	<span class="code-tag"><span class="code-comment">&lt;!-- the listener that kick-starts Spring --&gt;</span></span>
	<span class="code-tag">&lt;listener&gt;</span>
		<span class="code-tag">&lt;listener-class&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="code-tag">&lt;/listener-class&gt;</span>
	<span class="code-tag">&lt;/listener&gt;</span>
</pre>
</div></div>

<p>Then we need a standard Spring XML file so we create a new file in <tt>src/main/resources</tt> and name it <b>camel-config.xml</b>. Before we start editing this XML file we need to link to it from the web.xml file. So we add this snippet to the web.xml:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
	<span class="code-tag"><span class="code-comment">&lt;!-- location of spring xml files --&gt;</span></span>
	<span class="code-tag">&lt;context-param&gt;</span>
	    <span class="code-tag">&lt;param-name&gt;</span>contextConfigLocation<span class="code-tag">&lt;/param-name&gt;</span>
            <span class="code-tag">&lt;param-value&gt;</span>classpath:camel-config.xml<span class="code-tag">&lt;/param-value&gt;</span>
	<span class="code-tag">&lt;/context-param&gt;</span>
</pre>
</div></div>

<p>Now we are ready to edit the <b>camel-config.xml</b> file that is a standard Spring XML bean file. So you can add standard spring beans and whatnot you like to do and can do with Spring.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>Now we are nearly there, we just need to add Camel to the Spring XML file, so Spring knows Camel exists and can start it. First we need to add Camel to the schema location in the top of the XML file.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
       ...
       xsi:schemaLocation="
            http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
</pre>
</div></div>

<p>Now we are ready to let <a href="/confluence/display/CAMEL/Spring" title="Spring">Spring and Camel</a> work together. What we need to do is adding a <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a> to the Spring XML file. Camel ships with a CamelContextFactoryBean that is a Spring factory bean we should use for creating and initializing the SpringCamelContext. SpringCamelContext is extending CamelContext to be Spring aware so Camel and Spring can work nicely together. For instance the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> will now use Spring bean lookup. So any spring bean can now easily be lookup and used from Camel. Well back to today's lesson. So we can create a SpringCamelContext using the factory bean as illustrated below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag">&lt;bean id=<span class="code-quote">"camel"</span> class=<span class="code-quote">"org.apache.camel.spring.CamelContextFactoryBean"</span>/&gt;</span>
</pre>
</div></div>

<p>However this is not used very often as Spring has support for custom namespace, so Camel has a CamelNamespaceHandler so we can create Camel using nice XML syntax as:</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">"camel"</span> xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
       ...
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="Tutorial-Example-ReportIncident-Part5-Addingroutebuilder"></a>Adding route builder</h3>
<p>Now we have Camel integrated but we still need to add our route bulder that we did manually from the javacode as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-comment">// append the routes to the context
</span>    context.addRoutes(<span class="code-keyword">new</span> ReportIncidentRoutes());
</pre>
</div></div>

<p>There are two solutions to this</p>
<ul class="alternate" type="square">
	<li>using spring bean</li>
	<li>package scanning</li>
</ul>


<p>Using a spring bean we just declare the route builder using a regular spring bean:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag">&lt;bean id=<span class="code-quote">"myrouter"</span> class=<span class="code-quote">"org.apache.camel.example.reportincident.ReportIncidentRoutes"</span>/&gt;</span>
</pre>
</div></div>

<p>And then we can refer to it from our CamelContext:</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">"camel"</span> xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
       <span class="code-tag">&lt;routeBuilderRef ref=<span class="code-quote">"myrouter"</span>/&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>So now when Spring start's it will read the <b>camel-context.xml</b> file and thus also start Camel as well. As SpringCamelContext is spring lifecycle event aware, Camel will also shutdown when Spring is shutting down. So when you stop the web application Spring will notify this and Camel is also shutdown nice and properly. So as an end user no need to worry. </p>

<p>The package scanning solution is for convenience to refer to a java package and Camel will scan all classes within this package for RouteBuilder classes. If using this then you dont need to declare your route builder as a Spring bean. So the XML can be reduced to.</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">"camel"</span> xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
       <span class="code-tag">&lt;package&gt;</span>org.apache.camel.example.reportincident<span class="code-tag">&lt;/package&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h2><a name="Tutorial-Example-ReportIncident-Part5-UsingCXFdirectly"></a>Using CXF directly</h2>
<p>Now we have seen how you can leverage <a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a> to start Camel, in fact it handles the lifecycle of Camel, so you can say Camel is embedded with Spring in your application.</p>

<p>From the very start of this tutorial we have used CXF as the webservice framework and we haven't integrated it directly with Camel as it can do out-of-the-box. Camel ships with a <a href="/confluence/display/CAMEL/CXF" title="CXF"><b>camel-cxf</b></a> component for integrating CXF directly within Camel routing. In our tutorial we are exposing a webservice so we want continue to do this. Before we continue let's recap at what the webservice implementation we have from part 4</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/**
 * The webservice we have implemented.
 */
<span class="code-keyword">public</span> class ReportIncidentEndpointImpl <span class="code-keyword">implements</span> ReportIncidentEndpoint {

    <span class="code-keyword">private</span> CamelContext context;

    <span class="code-keyword">public</span> ReportIncidentEndpointImpl() <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// create the context
</span>        context = <span class="code-keyword">new</span> DefaultCamelContext();

        <span class="code-comment">// append the routes to the context
</span>        context.addRoutes(<span class="code-keyword">new</span> ReportIncidentRoutes());

        <span class="code-comment">// at the end start the camel context
</span>        context.start();
    }

    <span class="code-keyword">public</span> OutputReportIncident reportIncident(InputReportIncident parameters) {
        <span class="code-comment">// create the producer template to use <span class="code-keyword">for</span> sending messages
</span>        ProducerTemplate producer = context.createProducerTemplate();
        <span class="code-comment">// send the body and the filename defined with the special header key
</span>        <span class="code-object">Object</span> mailBody = producer.sendBody(<span class="code-quote">"direct:start"</span>, parameters);
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Body:"</span> + mailBody);

        <span class="code-comment">// <span class="code-keyword">return</span> an OK reply
</span>        OutputReportIncident out = <span class="code-keyword">new</span> OutputReportIncident();
        out.setCode(<span class="code-quote">"OK"</span>);
        <span class="code-keyword">return</span> out;
    }

}
</pre>
</div></div>

<p>We have already seen how we can get Spring starting Camel so the constructor method can be removed. What next is that the CamelContext needed in this code should be the one from our <b>camel-context.xml</b> file. So we change the code to use a plain setter injection (we can use Spring annotations and whatelse but we keep it simple with a setter):</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/**
 * The webservice we have implemented.
 */
<span class="code-keyword">public</span> class ReportIncidentEndpointImpl <span class="code-keyword">implements</span> ReportIncidentEndpoint {

    <span class="code-keyword">private</span> CamelContext context;

    <span class="code-keyword">public</span> void setCamelContext(CamelContext context) {
        <span class="code-keyword">this</span>.context = context;
    }

    <span class="code-keyword">public</span> OutputReportIncident reportIncident(InputReportIncident parameters) {
        <span class="code-comment">// create the producer template to use <span class="code-keyword">for</span> sending messages
</span>        ProducerTemplate producer = context.createProducerTemplate();
        <span class="code-comment">// send the body and the filename defined with the special header key
</span>        <span class="code-object">Object</span> mailBody = producer.sendBody(<span class="code-quote">"direct:start"</span>, parameters);
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Body:"</span> + mailBody);

        <span class="code-comment">// <span class="code-keyword">return</span> an OK reply
</span>        OutputReportIncident out = <span class="code-keyword">new</span> OutputReportIncident();
        out.setCode(<span class="code-quote">"OK"</span>);
        <span class="code-keyword">return</span> out;
    }

}
</pre>
</div></div>

<p>And then we need to instruct Spring to set this property. Turning back to cxf-config.xml from part 4 we can add a reference to our camel context</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:jaxws</span>=<span class="code-quote">"http://cxf.apache.org/jaxws"</span>
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"&gt;

    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span>/&gt;</span>
    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-extension-soap.xml"</span>/&gt;</span>
    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- implementation of the webservice, and we refer to our camel context with the id = camel from camel-context.xml --&gt;</span></span>
    <span class="code-tag">&lt;bean id=<span class="code-quote">"reportIncidentEndpoint"</span> class=<span class="code-quote">"org.apache.camel.example.reportincident.ReportIncidentEndpointImpl"</span>&gt;</span>
       <span class="code-tag">&lt;property name=<span class="code-quote">"context"</span> ref=<span class="code-quote">"camel"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- export the webservice using jaxws --&gt;</span></span>
    &lt;jaxws:endpoint id=<span class="code-quote">"reportIncident"</span>
                    implementor=<span class="code-quote">"#reportIncidentEndpoint"</span>
                    address=<span class="code-quote">"/incident"</span>
                    wsdlLocation=<span class="code-quote">"/WEB-INF/wsdl/report_incident.xml"</span>
                    endpointName=<span class="code-quote">"s:ReportIncidentPort"</span>
                    serviceName=<span class="code-quote">"s:ReportIncidentService"</span>
                    <span class="code-keyword">xmlns:s</span>=<span class="code-quote">"http://reportincident.example.camel.apache.org"</span>/&gt;
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>So now we have two spring XML files</p>
<ul class="alternate" type="square">
	<li>cxf-config.xml</li>
	<li>camel-config.xml</li>
</ul>


<p>And since <b>cxf-config.xml</b> is dependent on <b>camel-config.xml</b> we need to have correct ordering in our web.xml where we have defined the XML files to load by Spring. So we set the <b>camel-config.xml</b> before the <b>cxf-config.xml</b> so Spring have created the SpringCamelContext and registered it in its registry with the id = camel.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag"><span class="code-comment">&lt;!-- location of spring xml files --&gt;</span></span>
    <span class="code-tag">&lt;context-param&gt;</span>
        <span class="code-tag">&lt;param-name&gt;</span>contextConfigLocation<span class="code-tag">&lt;/param-name&gt;</span>
        <span class="code-tag">&lt;param-value&gt;</span>classpath:camel-config.xml<span class="code-tag">&lt;/param-value&gt;</span>
        <span class="code-tag">&lt;param-value&gt;</span>classpath:cxf-config.xml<span class="code-tag">&lt;/param-value&gt;</span>
    <span class="code-tag">&lt;/context-param&gt;</span>
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Sidenote on spring XML files</b><br />The solution presented here with two spring XML files (cxf-config and camel-config) that is pendent on each other and thus has to be ordered can also be done using a different solution. You can for instance add an <b>import</b> in cxf-config and only have the cxf-config listed in web.xml. Another solution is to merge the two files into one combined file. Yes you can add the camelContext in the cxf-config file.</td></tr></table></div> 

<p>But hey this isn't using <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> directly in the routing? Yes it's not but I wanted to show the halfway solution as well. What we have now is having Spring creating and handling lifecycle of Camel and showing how you can inject CamelContext using standard Spring into whatever code you have. This is very powerful as you can use the solution that you (or your team) already master and is familiar with. If they have Spring experience then the IoC principle of injecting resources is of course also possible with Camel as well. In fact it's a best practice principle. Later you will learn that you can inject other Camel types such as <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a>, <a href="/confluence/display/CAMEL/ProducerTemplate" title="ProducerTemplate">ProducerTemplate</a> as well.</p>

<h3><a name="Tutorial-Example-ReportIncident-Part5-Usingthecamelcxfcomponent"></a>Using the camel-cxf component</h3>
<p>Okay let's continue and try to integrate <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> directly into our routing in Camel. This can be a bit more tricky than at first sight. Our goal is to avoid implementing the <tt>ReportIncidentEndpoint</tt> as we did with our code in <tt>ReportIncidentEndpointImpl</tt> (see above). Camel should be able to handle this automatically and integrate directly within our route. </p>

<p>Before we started our routing using the <a href="/confluence/display/CAMEL/Direct" title="Direct">Direct</a> endpoint with <tt>"direct:start"</tt>. We should replace this with the <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoint.</p>

<p>But before going to far we have to make a few adjustments to the .wsdl file and it's location in our project folder. We move <b>report_incident.wsdl</b> from src/main/webapp/WEB-INF/wsdl* to <b>src/main/resources</b> as we want to be able to refer to it from within our route builder from Java code and thus it should be on the classpath for easy access. Secondly we have upgrade the CXF to a newer version and it identified a minor issue with the .wsdl file itself. We have to give the complexType a name otherwise we get some JAXB error. </p>

<p>So the .wsdl should be changed from:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;xs:element name=<span class="code-quote">"inputReportIncident"</span>&gt;</span>
        <span class="code-tag">&lt;xs:complexType&gt;</span>
</pre>
</div></div>

<p>To include a name attribute of the complex types:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;xs:element name=<span class="code-quote">"inputReportIncident"</span>&gt;</span>
        <span class="code-tag">&lt;xs:complexType name=<span class="code-quote">"inputReportIncident"</span>&gt;</span>
</pre>
</div></div>

<h3><a name="Tutorial-Example-ReportIncident-Part5-UsingCXFendpoint"></a>Using CXF endpoint</h3>

<p>Okay now we are ready to turn our attention to using <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> directly in Camel routing. So we zap <tt>ReportIncidentEndpointImpl</tt> as we no longer need this code. So what's left is:</p>
<ul class="alternate" type="square">
	<li><tt>FilenameGenerator.java</tt></li>
	<li><tt>ReportIncidentRoutes.java</tt><br/>
And that is all what's needed, well for now... <img class="emoticon" src="/confluence/images/icons/emoticons/wink.gif" height="20" width="20" align="absmiddle" alt="" border="0"/></li>
</ul>


<p>The goal is to replace the previous staring endpoint ("direct:start") to the new starting <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoint.</p>

<p><a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoint can be configured in either or both CXF spring configuration file or/and in the route directly. It accepts one parameter and others are optional. The parameter it <b>must</b> have is the service class. The service class is the interface for the WSDL operation's. As we have the <b>wsdl2java</b> goal to generate this class for us, we have it already as <tt>org.apache.camel.example.reportincident.ReportIncidentEndpoint</tt>. </p>

<p>The other parameter we will provide is the url to the .wsdl file. And finally we must provide the http address we expose the webservice at. The URI is therefore:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-comment">// endpoint to our CXF webservice
</span>        <span class="code-object">String</span> cxfEndpoint = <span class="code-quote">"cxf:<span class="code-comment">//http://localhost:8080/part-five/webservices/incident"</span>
</span>                + <span class="code-quote">"?serviceClass=org.apache.camel.example.reportincident.ReportIncidentEndpoint"</span>
                + <span class="code-quote">"&amp;wsdlURL=report_incident.wsdl"</span>;
</pre>
</div></div>

<p>Then we can replace "direct:start" with our cxf endpoint instead, so it's:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        from(cxfEndpoint)...
</pre>
</div></div>

<p>The next issue you might now have guessed is that before (in part 4) we did a traditional codeing style to start a task and return a response to the caller in the method:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  <span class="code-keyword">public</span> OutputReportIncident reportIncident(InputReportIncident parameters) {
        <span class="code-comment">// create the producer template to use <span class="code-keyword">for</span> sending messages
</span>        ProducerTemplate producer = context.createProducerTemplate();
        <span class="code-comment">// send the body and the filename defined with the special header key
</span>        <span class="code-object">Object</span> mailBody = producer.sendBody(<span class="code-quote">"direct:start"</span>, parameters);
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Body:"</span> + mailBody);

        <span class="code-comment">// <span class="code-keyword">return</span> an OK reply
</span>        OutputReportIncident out = <span class="code-keyword">new</span> OutputReportIncident();
        out.setCode(<span class="code-quote">"OK"</span>);
        <span class="code-keyword">return</span> out;
    }
</pre>
</div></div>
<p>As you can see the method <tt>reportIncident</tt> is invoked with the webservice input parameters and we return the response to to the webservice from the method. But our situation now is that we don't have this method anymore. So how do we return a response to the webservice?</p>

<p>Houston we have a problem! Well of course not but the mindset have to be changed slightly to understand the routing concept, and how it works. So let's step back a bit. What we have here is a webservice that we expose. And our webservice is synchronous request/response based so the caller waits for a response. This is a InOut Message Exchange Pattern. Camel will default use InOut for webservices so we don't need to specify this explicitly. When we have a InOut pattern then Camel will return the response to the original caller when the routes ends. Looking at our route we have:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        from(cxfEndpoint)
            <span class="code-comment">// then set the file name using the FilenameGenerator bean
</span>            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, <span class="code-quote">"generateFilename"</span>))
            <span class="code-comment">// transform the message using velocity to generate the mail message
</span>            .to(<span class="code-quote">"velocity:MailBody.vm"</span>)
            <span class="code-comment">// and store the file    
</span>            .to(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)</span>
</pre>
</div></div>
<p>When the route ends after the file endpoint has been processed Camel will return the OUT message to the original caller (the caller of the webservice). However our route currently as it stands have not set any OUT message, so this is what we need to do, <a href="/confluence/display/CAMEL/Message+Translator" title="Message Translator">transforming (Message Translator EIP)</a> the message into the response. We will therefore use a processor where we have 100% control in the Java code to set the response.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// the response we want to send
</span>            OutputReportIncident OK = <span class="code-keyword">new</span> OutputReportIncident();
            OK.setCode(<span class="code-quote">"0"</span>);

            <span class="code-comment">// set the response on the OUT message as we use InOut
</span>            exchange.getOut().setBody(OK);
       }
</pre>
</div></div>

<p>And with the route:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-comment">// first part from the webservice -&gt; file backup
</span>        from(cxfEndpoint)
            <span class="code-comment">// then set the file name using the FilenameGenerator bean
</span>            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, <span class="code-quote">"generateFilename"</span>))
            <span class="code-comment">// transform the message using velocity to generate the mail message
</span>            .to(<span class="code-quote">"velocity:MailBody.vm"</span>)
            <span class="code-comment">// and store the file    
</span>            .to(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)
</span>            <span class="code-comment">// <span class="code-keyword">return</span> OK as response
</span>            .process(<span class="code-keyword">new</span> Processor() {
                <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
                    <span class="code-comment">// the response we want to send
</span>                    OutputReportIncident OK = <span class="code-keyword">new</span> OutputReportIncident();
                    OK.setCode(<span class="code-quote">"0"</span>);

                    <span class="code-comment">// set the response on the OUT message as we use InOut
</span>                    exchange.getOut().setBody(OK);
                }
            });
</pre>
</div></div>

<p>The route using the inlined processor is a bit ugly as we have high level routing logic combined with low level java code. First of all I wanted to show how flexible Camel is, allowing use as a developer to always be in control and can use Java code for whatever you needs is. First of all we could move the code into a inner class and just refer to it:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-keyword">private</span> <span class="code-keyword">static</span> class OKResponseProcessor <span class="code-keyword">implements</span> Processor {
        <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// the response we want to send
</span>            OutputReportIncident OK = <span class="code-keyword">new</span> OutputReportIncident();
            OK.setCode(<span class="code-quote">"0"</span>);

            <span class="code-comment">// set the response on the OUT message as we use InOut
</span>            exchange.getOut().setBody(OK);
        }
    }
</pre>
</div></div>

<p>And then out route is much nicer:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-comment">// first part from the webservice -&gt; file backup
</span>        from(cxfEndpoint)
            <span class="code-comment">// then set the file name using the FilenameGenerator bean
</span>            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, <span class="code-quote">"generateFilename"</span>))
            <span class="code-comment">// transform the message using velocity to generate the mail message
</span>            .to(<span class="code-quote">"velocity:MailBody.vm"</span>)
            <span class="code-comment">// and store the file    
</span>            .to(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)
</span>            <span class="code-comment">// <span class="code-keyword">return</span> OK as response
</span>            .process(<span class="code-keyword">new</span> OKResponseProcessor());
</pre>
</div></div>

<p>Since our response is static and we don't need to any code logic to set it we can use the <b>transform</b> DSL in the route to set a constant OUT message. So we refactor the code a bit to loose the processor. First we define the OK response as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-comment">// webservice response <span class="code-keyword">for</span> OK 
</span>        OutputReportIncident OK = <span class="code-keyword">new</span> OutputReportIncident();
        OK.setCode(<span class="code-quote">"0"</span>);
</pre>
</div></div>

<p>And then we can refer to it in the route as a constant expression:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-comment">// <span class="code-keyword">return</span> OK as response
</span>    .transform(constant(OK));
</pre>
</div></div>

<h3><a name="Tutorial-Example-ReportIncident-Part5-ImportantissueregardingusingCXFendpointsinCamel"></a>Important issue regarding using <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoints in Camel</h3>
<p>Now we are nearly there, there is an important issue left with using <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoints in Camel. In part 4 we started the route by sending the InputReportIncident object containing the webservice input. Now we are using <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> endpoints directly in our routing so its a CxfExchange that is created and passed in the routing. CxfExchange stores the payload in a CXF holder class <tt>org.apache.cxf.message.MessageContentsList</tt>. So to be able to get our InputReportIncident class we need to get this object from the holder class. For this we show how it's done in Java using a processor, then later we show a nicer solution.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> void process(<span class="code-keyword">final</span> Exchange exchange) {
    <span class="code-comment">// Get the parameter list
</span>    List parameter = exchange.getIn().getBody(List.class);
    <span class="code-comment">// Get the first object in the list that is our InputReportIncident
</span>    <span class="code-object">Object</span> input = parameter.get(0);
    <span class="code-comment">// replace with our input
</span>    exchange.getOut().setBody(input);
}
</pre>
</div></div>

<p>Well this isn't the nicest code, but again we want to show how it's done using plain Java, that is actually how Camel also can assist you in this nicer solution - we simply convert the body to the expected type using <b>convertBodyTo</b>. This is an important feature in Camel and you can use it for other situations as well.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        <span class="code-comment">// first part from the webservice -&gt; file backup
</span>        from(cxfEndpoint)
            <span class="code-comment">// we need to convert the CXF payload to InputReportIncident that FilenameGenerator and velocity expects
</span>            .convertBodyTo(InputReportIncident.class)
            <span class="code-comment">// then set the file name using the FilenameGenerator bean
</span>            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, <span class="code-quote">"generateFilename"</span>))
            <span class="code-comment">// transform the message using velocity to generate the mail message
</span>            .to(<span class="code-quote">"velocity:MailBody.vm"</span>)
            <span class="code-comment">// and store the file    
</span>            .to(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)
</span>            <span class="code-comment">// <span class="code-keyword">return</span> OK as response
</span>            .transform(constant(OK));
</pre>
</div></div>

<p>Now the route is nice and simple.</p>

<h2><a name="Tutorial-Example-ReportIncident-Part5-Unittesting"></a>Unit testing</h2>
<p>Now lets turn our attention to unit testing it. From part 4 we have an unit test that is capable of exposing a webservice and send a test request and assert a mail is received. We will refactor this unit test to start up Camel, as it's Camel that should expose the webservice.</p>

<p>As Camel is very flexible we can create a camel context, add the routes and start it in 3 lines of code so we do it:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-keyword">protected</span> void startCamel() <span class="code-keyword">throws</span> Exception {
    	camel = <span class="code-keyword">new</span> DefaultCamelContext();
        camel.addRoutes(<span class="code-keyword">new</span> ReportIncidentRoutes());
        camel.start();
</pre>
</div></div>

<p>And the rest of the unit test is quite self documenting so we print it here in full:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/**
 * Unit test of our routes
 */
<span class="code-keyword">public</span> class ReportIncidentRoutesTest <span class="code-keyword">extends</span> TestCase {

    <span class="code-keyword">private</span> CamelContext camel;

    <span class="code-comment">// should be the same address as we have in our route
</span>    <span class="code-keyword">private</span> <span class="code-keyword">static</span> <span class="code-object">String</span> ADDRESS = <span class="code-quote">"http:<span class="code-comment">//localhost:8080/part-five/webservices/incident"</span>;
</span>
    <span class="code-keyword">protected</span> void startCamel() <span class="code-keyword">throws</span> Exception {
    	camel = <span class="code-keyword">new</span> DefaultCamelContext();
        camel.addRoutes(<span class="code-keyword">new</span> ReportIncidentRoutes());
        camel.start();
    }

    <span class="code-keyword">protected</span> <span class="code-keyword">static</span> ReportIncidentEndpoint createCXFClient() {
        <span class="code-comment">// we use CXF to create a client <span class="code-keyword">for</span> us as its easier than JAXWS and works
</span>        JaxWsProxyFactoryBean factory = <span class="code-keyword">new</span> JaxWsProxyFactoryBean();
        factory.setServiceClass(ReportIncidentEndpoint.class);
        factory.setAddress(ADDRESS);
        <span class="code-keyword">return</span> (ReportIncidentEndpoint) factory.create();
    }

    <span class="code-keyword">public</span> void testRendportIncident() <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// start camel
</span>        startCamel();

        <span class="code-comment">// <span class="code-keyword">assert</span> mailbox is empty before starting
</span>        Mailbox inbox = Mailbox.get(<span class="code-quote">"incident@mycompany.com"</span>);
        assertEquals(<span class="code-quote">"Should not have mails"</span>, 0, inbox.size());

        <span class="code-comment">// create input parameter
</span>        InputReportIncident input = <span class="code-keyword">new</span> InputReportIncident();
        input.setIncidentId(<span class="code-quote">"123"</span>);
        input.setIncidentDate(<span class="code-quote">"2008-08-18"</span>);
        input.setGivenName(<span class="code-quote">"Claus"</span>);
        input.setFamilyName(<span class="code-quote">"Ibsen"</span>);
        input.setSummary(<span class="code-quote">"Bla"</span>);
        input.setDetails(<span class="code-quote">"Bla bla"</span>);
        input.setEmail(<span class="code-quote">"davsclaus@apache.org"</span>);
        input.setPhone(<span class="code-quote">"0045 2962 7576"</span>);

        <span class="code-comment">// create the webservice client and send the request
</span>        ReportIncidentEndpoint client = createCXFClient();
        OutputReportIncident out = client.reportIncident(input);

        <span class="code-comment">// <span class="code-keyword">assert</span> we got a OK back
</span>        assertEquals(<span class="code-quote">"0"</span>, out.getCode());

        <span class="code-comment">// let some time pass to allow Camel to pickup the file and send it as an email
</span>        <span class="code-object">Thread</span>.sleep(3000);

        <span class="code-comment">// <span class="code-keyword">assert</span> mail box
</span>        assertEquals(<span class="code-quote">"Should have got 1 mail"</span>, 1, inbox.size());

        <span class="code-comment">// stop camel
</span>        camel.stop();
    }
}
</pre>
</div></div>

<h2><a name="Tutorial-Example-ReportIncident-Part5-Conclusion"></a>Conclusion</h2>
<p>We have now seen how we have created a much nicer solution leveraging Camel's powerful routing capabilities.</p>

<p>What we have here is routing logic with the help of the code comments could be understood by non developers. This is very powerful. In a later part we will look at some of the tools that Camel provides, for instance a tool to <a href="/confluence/display/CAMEL/Visualisation" title="Visualisation">generate a nice diagrams</a> of your routes <img class="emoticon" src="/confluence/images/icons/emoticons/wink.gif" height="20" width="20" align="absmiddle" alt="" border="0"/></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// webservice response <span class="code-keyword">for</span> OK
</span>        OutputReportIncident OK = <span class="code-keyword">new</span> OutputReportIncident();
        OK.setCode(<span class="code-quote">"0"</span>);

        <span class="code-comment">// endpoint to our CXF webservice
</span>        <span class="code-object">String</span> cxfEndpoint = <span class="code-quote">"cxf:<span class="code-comment">//http://localhost:8080/part-five/webservices/incident"</span>
</span>                + <span class="code-quote">"?serviceClass=org.apache.camel.example.reportincident.ReportIncidentEndpoint"</span>
                + <span class="code-quote">"&amp;wsdlURL=report_incident.wsdl"</span>;

        <span class="code-comment">// first part from the webservice -&gt; file backup
</span>        from(cxfEndpoint)
            <span class="code-comment">// we need to convert the CXF payload to InputReportIncident that FilenameGenerator and velocity expects
</span>            .convertBodyTo(InputReportIncident.class)
            <span class="code-comment">// then set the file name using the FilenameGenerator bean
</span>            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, <span class="code-quote">"generateFilename"</span>))
            <span class="code-comment">// and create the mail body using velocity templating
</span>            .to(<span class="code-quote">"velocity:MailBody.vm"</span>)
            <span class="code-comment">// and store the file
</span>            .to(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)
</span>            <span class="code-comment">// <span class="code-keyword">return</span> OK as response
</span>            .transform(constant(OK));

        <span class="code-comment">// second part from the file backup -&gt; send email
</span>        from(<span class="code-quote">"file:<span class="code-comment">//target/subfolder"</span>)
</span>            <span class="code-comment">// set the subject of the email
</span>            .setHeader(<span class="code-quote">"subject"</span>, constant(<span class="code-quote">"<span class="code-keyword">new</span> incident reported"</span>))
            <span class="code-comment">// send the email
</span>            .to(<span class="code-quote">"smtp:<span class="code-comment">//someone@localhost?password=secret&amp;to=incident@mycompany.com"</span>);
</span>    }
</pre>
</div></div>

<p>In the next part's look at using XML to create the route instead of Java code. Then it might be even more readable by non developers. </p>


<h2><a name="Tutorial-Example-ReportIncident-Part5-Links"></a>Links</h2>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident" title="Tutorial-Example-ReportIncident">Introduction</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part1" title="Tutorial-Example-ReportIncident-Part1">Part 1</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part2" title="Tutorial-Example-ReportIncident-Part2">Part 2</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part3" title="Tutorial-Example-ReportIncident-Part3">Part 3</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part4" title="Tutorial-Example-ReportIncident-Part4">Part 4</a></li>
	<li><a href="/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part5" title="Tutorial-Example-ReportIncident-Part5">Part 5</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part5">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=96177&revisedVersion=23&originalVersion=22">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Tutorial-Example-ReportIncident-Part5?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message