camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > CXF Tomcat Example
Date Thu, 21 Oct 2010 14:46:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/CXF+Tomcat+Example">CXF
Tomcat Example</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="CXFTomcatExample-CXFTomcatExample"></a>CXF Tomcat
Example</h2>
<p><b>Available as of Camel 2.5</b></p>

<p>This example is located in the <tt>examples/camel-example-cxf-tomcat</tt>
directory of the Camel distribution.<br/>
There is a <tt>README.txt</tt> file with instructions how to run it.</p>

<p>If you use maven then you can easily package the example from the command line:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn <span class="code-keyword">package</span>
</pre>
</div></div>

<h3><a name="CXFTomcatExample-About"></a>About</h3>

<p>This example demonstrates how you can use <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> to expose a web service in Camel using code first approach.</p>


<h3><a name="CXFTomcatExample-Implementation"></a>Implementation</h3>

<p>The web service we want to expose is defined as an interface which has 2 operations:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>IncidentService.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> IncidentService {

    /**
     * Operation to report an incident
     */
    OutputReportIncident reportIncident(InputReportIncident input);

    /**
     * Operation to get the status of an incident
     */
    OutputStatusIncident statusIncident(InputStatusIncident input);
}
</pre>
</div></div>

<p>In this example we are not using any JAX-WS annotations. You can use those annotations
to fine control the web service wsdl contract.</p>

<p>In the Camel route we expose this web service very easily using the Camel <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> component.<br/>
All we have to do is to define an endpoint uri in the format</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxf:/incident?serviceClass=org.apache.camel.example.cxf.incident.IncidentService
</pre>
</div></div>

<p>This means Camel will expose the web service using the relative address <tt>/incident</tt>
and the serviceClass parameter links to the interface which defines the code first approach.
</p>

<p>In this example we want to be flexible, so if we add a 3rd operation to the web service
we want it to be easily to add a route to handle this operation. Therefore we use the <a
href="/confluence/display/CAMEL/Recipient+List" title="Recipient List">Recipient List</a>
EIP pattern to route to the route which handles the given operation. Notice how we use a <a
href="/confluence/display/CAMEL/Direct" title="Direct">Direct</a> endpoint to link
the routes.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>CamelRoute.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class CamelRoute
<span class="code-keyword">extends</span> RouteBuilder {

    <span class="code-comment">// CXF webservice using code first approach
</span>    <span class="code-keyword">private</span> <span class="code-object">String</span>
uri = <span class="code-quote">"cxf:/incident?serviceClass="</span> + IncidentService.class.getName();

    @Override
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
        from(uri)
            .to(<span class="code-quote">"log:input"</span>)
            <span class="code-comment">// send the request to the route to handle the
operation
</span>            <span class="code-comment">// the name of the operation is
in that header
</span>            .recipientList(simple(<span class="code-quote">"direct:${header.operationName}"</span>));

        <span class="code-comment">// report incident
</span>        from(<span class="code-quote">"direct:reportIncident"</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">// get the id of the input
</span>                    <span class="code-object">String</span> id =
exchange.getIn().getBody(InputReportIncident.class).getIncidentId();

                    <span class="code-comment">// set reply including the id
</span>                    OutputReportIncident output = <span class="code-keyword">new</span>
OutputReportIncident();
                    output.setCode(<span class="code-quote">"OK;"</span> + id);
                    exchange.getOut().setBody(output);
                }
            })
            .to(<span class="code-quote">"log:output"</span>);

        <span class="code-comment">// status incident
</span>        from(<span class="code-quote">"direct:statusIncident"</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">// set reply
</span>                    OutputStatusIncident output = <span class="code-keyword">new</span>
OutputStatusIncident();
                    output.setStatus(<span class="code-quote">"IN PROGRESS"</span>);
                    exchange.getOut().setBody(output);
                }
            })
            .to(<span class="code-quote">"log:output"</span>);
    }
}
</pre>
</div></div>

<h4><a name="CXFTomcatExample-SpringXML"></a>Spring XML</h4>

<p>In the Spring XML file we have to import some <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> mandatory imports. Notice we use the <tt>cxf-servlet</tt>
to leverage HTTP Servlet with <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>camel-config.xml</b></div><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:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
       <span class="code-keyword">xmlns:cxf</span>=<span class="code-quote">"http://camel.apache.org/schema/cxf"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- needed cxf imports
--&gt;</span></span>
    <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"><span class="code-comment">&lt;!-- use the CXF
servlet --&gt;</span></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">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- let Camel
automatic find the route in this java package --&gt;</span></span>
        <span class="code-tag">&lt;package&gt;</span>org.apache.camel.example.cxf<span
class="code-tag">&lt;/package&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>

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

<h3><a name="CXFTomcatExample-Runningtheexample"></a>Running the example</h3>
<p>TODO: How to deploy in Tomcat</p>

<p>You can then use SoapUI or another web service client and send a request to the <tt><a
href="http://localhost:9080/camel-example-cxf-proxy/webservices/incident" class="external-link"
rel="nofollow">http://localhost:9080/camel-example-cxf-proxy/webservices/incident</a></tt>
url. The wsdl is located at: <tt><a href="http://localhost:9080/camel-example-cxf-proxy/webservices/incident?wsdl"
class="external-link" rel="nofollow">http://localhost:9080/camel-example-cxf-proxy/webservices/incident?wsdl</a></tt>.</p>

<p>The console should then output progress.</p>

<h3><a name="CXFTomcatExample-Sampleoutput"></a>Sample output</h3>
<p>TOOD: sample output</p>

<p>Here is some sample output from the console:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
</pre>
</div></div>

<h3><a name="CXFTomcatExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a></li>
	<li><a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</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/CXF+Tomcat+Example">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXF+Tomcat+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message