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 Example
Date Fri, 16 Dec 2011 07:19: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/CXF+Example">CXF
Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Updated the snippet url<br />
    </div>
        <br/>
                         <h4>Changes (4)</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" >We start with creating a [CamelContext]
- which is a container for [Components], [Routes] etc: <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=e1|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/<span
class="diff-added-chars"style="background-color: #dfd;">httptojms/</span>CamelCxfExample.java}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Here is code for starting
the JMS broker and the CXF server:  <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=e2|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/<span
class="diff-added-chars"style="background-color: #dfd;">httptojms/</span>CamelCxfExample.java}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Now we setup the router
for the two endpoint URIs which we mentioned before, ROUTER_ENDPOINT_URI is the endpoint for
consuming the request of SOAP over HTTP, SERVICE_ENDPOINT_URI is the endpoint for providing
the request for the SOAP over JMS service. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=e3|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/<span
class="diff-added-chars"style="background-color: #dfd;">httptojms/</span>CamelCxfExample.java}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=e4|lang=java|url=camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/<span
class="diff-added-chars"style="background-color: #dfd;">httptojms/</span>CamelCxfExample.java}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Then we start up the camel
context and create the CXF client to kick off the request. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="CXFExample-CXFExample"></a>CXF Example</h1>

<h2><a name="CXFExample-CXFexampleforroutingmessageswithdifferenttransports"></a>CXF
example for routing messages with different transports</h2>

<p>The <a href="http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/CamelCxfExample.java?view=markup"
class="external-link" rel="nofollow">Camel CXF example</a> is a demo of the camel-cxf
component to show how to route messages between CXF endpoints, with one endpoint consuming
a SOAP over HTTP request while the other providing a SOAP over JMS request for the actual
CXF Service endpoint. The Camel router just routs the SOAP over HTTP CXF client request to
the SOAP over JMS CXF service.</p>

<p>To run the example see instructions in the <tt>README.txt</tt> file in
the <tt>camel-example-cxf</tt> directory under <tt>examples</tt>.</p>


<p>We start with creating a <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
- which is a container for <a href="/confluence/display/CAMEL/Components" title="Components">Components</a>,
<a href="/confluence/display/CAMEL/Routes" title="Routes">Routes</a> etc:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">CamelContext context = <span class="code-keyword">new</span>
DefaultCamelContext();
</pre>
</div></div>

<p>Here is code for starting the JMS broker and the CXF server: </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">JmsBroker broker = <span class="code-keyword">new</span>
JmsBroker();
Server server = <span class="code-keyword">new</span> Server();
<span class="code-keyword">try</span> {
    broker.start();
    server.start();
</pre>
</div></div>

<p>Now we setup the router for the two endpoint URIs which we mentioned before, ROUTER_ENDPOINT_URI
is the endpoint for consuming the request of SOAP over HTTP, SERVICE_ENDPOINT_URI is the endpoint
for providing the request for the SOAP over JMS service.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">context.addRoutes(<span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
        <span class="code-comment">// Here we just pass the exception back, don't need
to use errorHandler
</span>        errorHandler(noErrorHandler());
        from(ROUTER_ENDPOINT_URI).to(SERVICE_ENDPOINT_URI);
    }
});
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">context.start();
Client client = <span class="code-keyword">new</span> Client(ROUTER_ADDRESS +
<span class="code-quote">"?wsdl"</span>);
</pre>
</div></div>

<p>Then we start up the camel context and create the CXF client to kick off the request.</p>

<h2><a name="CXFExample-CXFexampleforusingCameltransport"></a>CXF example
for using Camel transport</h2>

<p>Since Camel has lots of components and is a great mediation rule engine, we can leverage
these Camel features in CXF by using the Camel transport. Here is an example to show how to
use the Camel transport in CXF.</p>

<p>In this example, we setup a load balance Camel context in the Camel destination.
</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:camel</span>=<span class="code-quote">"http://cxf.apache.org/transports/camel"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/transports/camel http://cxf.apache.org/transports/camel.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/cxfEndpoint.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    "&gt;

   <span class="code-tag">&lt;bean id = <span class="code-quote">"roundRobinRef"</span>
class=<span class="code-quote">"org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer"</span>
/&gt;</span>

   <span class="code-tag">&lt;camelContext id=<span class="code-quote">"dest_context"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
       <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"jetty:http://localhost:9091/GreeterContext/GreeterPort"</span>/&gt;</span>
            <span class="code-tag">&lt;loadBalance ref=<span class="code-quote">"roundRobinRef"</span>&gt;</span>
              <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:EndpointA"</span>/&gt;</span>
              <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:EndpointB"</span>/&gt;</span>
            <span class="code-tag">&lt;/loadBalance&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
   <span class="code-tag">&lt;/camelContext&gt;</span>

   <span class="code-tag"><span class="code-comment">&lt;!-- Inject the camel
context to the Camel transport's destination --&gt;</span></span>
   <span class="code-tag">&lt;camel:destination name=<span class="code-quote">"{http://apache.org/hello_world_soap_http}CamelPort.camel-destination"</span>&gt;</span>
        <span class="code-tag">&lt;camel:camelContextRef&gt;</span>dest_context<span
class="code-tag">&lt;/camel:camelContextRef&gt;</span>
   <span class="code-tag">&lt;/camel:destination&gt;</span>

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

<p>When CXF loads this configuration, it will publish the CXF endpoint to the Camel
endpoint that is found in the Camel context.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">SpringBusFactory bf = <span class="code-keyword">new</span>
SpringBusFactory();
BusFactory.setDefaultBus(<span class="code-keyword">null</span>);
Bus bus = bf.createBus(<span class="code-quote">"/org/apache/camel/example/camel/transport/CamelDestination.xml"</span>);
BusFactory.setDefaultBus(bus);
</pre>
</div></div>

<p>The code below shows how to publish the endpoint to different Camel context endpoints.
 You can specify the Camel endpoint URI as the endpoint address parameter.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">GreeterImpl implementor = <span class="code-keyword">new</span>
GreeterImpl();
implementor.setSuffix(<span class="code-quote">"EndpointA"</span>);
<span class="code-object">String</span> address = <span class="code-quote">"camel:<span
class="code-comment">//direct:EndpointA"</span>;
</span>endpointA = Endpoint.publish(address, implementor);

implementor = <span class="code-keyword">new</span> GreeterImpl();
implementor.setSuffix(<span class="code-quote">"EndpointB"</span>);
address = <span class="code-quote">"camel:<span class="code-comment">//direct:EndpointB"</span>;
</span>endpointB = Endpoint.publish(address, implementor);
</pre>
</div></div>

<h2><a name="CXFExample-CXFexampleforusingtheWebServiceProviderAPI"></a>CXF
example for using the WebServiceProvider API</h2>

<p>JAX-WS provides the WebServiceProvider API to meet the requirements of XML-level
message handling. This example shows how to route a SOAP Message from a WebServiceProvider
implementation to bean endpoint in the Camel context. Basically, we still pass the WebServiceProvider
implementor as SEI, and the CxfConsumer will pass the SOAP Message in a list of parameters
to the Camel router for further processing.</p>

<p>First, let's have a look at the Camel context's configuration. Here we start a CxfConsumer
to listen to the address and pass SOAP messages to the test bean.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;!--
If you want to run this example in a Tomcat container which needs the servlet transport,
please replace the cxf-extension-http-jetty.xml with cxf-servlet.xml
--&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;bean id=<span class="code-quote">"testBean"</span>
class=<span class="code-quote">"org.apache.camel.example.cxf.provider.TesterBean"</span>/&gt;</span>

&lt;cxf:cxfEndpoint id=<span class="code-quote">"soapMessageEndpoint"</span>
                 serviceClass=<span class="code-quote">"org.apache.camel.example.cxf.provider.GreeterProvider"</span>
                 address=<span class="code-quote">"http://localhost:9000/GreeterContext/SOAPMessageService"</span>
                 wsdlURL=<span class="code-quote">"wsdl/hello_world.wsdl"</span>
                 endpointName=<span class="code-quote">"s:SoapOverHttpRouter"</span>
                 serviceName=<span class="code-quote">"s:SOAPService"</span>
                 <span class="code-keyword">xmlns:s</span>=<span class="code-quote">"http://apache.org/hello_world_soap_http"</span>/&gt;


<span class="code-tag">&lt;camelContext id=<span class="code-quote">"test_context"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"cxf:bean:soapMessageEndpoint"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:testBean?method=processSOAP"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

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

<p>Below is the WebServiceProvider implementor's annotation.  In this code, we just
want to handle SOAPMessages in the Message mode:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@WebServiceProvider()
@ServiceMode(Mode.MESSAGE)
</pre>
</div></div>

<p>Since the CXF-Camel component will replace the CXF invoker to call the processor
when starting a CXF endpoint, the CXF-Camel consumer will never call the provider implementor's
invoke method here.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class GreeterProvider
<span class="code-keyword">implements</span> Provider&lt;SOAPMessage&gt;
{

    <span class="code-keyword">public</span> SOAPMessage invoke(SOAPMessage message)
{
        <span class="code-comment">// Requests should not come here as the Camel route
will
</span>        <span class="code-comment">// intercept the call before <span
class="code-keyword">this</span> is invoked.
</span>        <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
UnsupportedOperationException(<span class="code-quote">"Placeholder method"</span>);
    }

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

<p>Now we can implement the bean for handling the invocation of the SOAP message:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class TesterBean
{

    <span class="code-keyword">public</span> SOAPMessage processSOAP(Exchange
exchange) {
        <span class="code-comment">// Since the Camel-CXF endpoint uses a list to store
the parameters
</span>        <span class="code-comment">// and bean component uses the bodyAs
expression to get the value
</span>        <span class="code-comment">// we'll need to deal with the parameters
ourself
</span>        SOAPMessage soapMessage = (SOAPMessage)exchange.getIn().getBody(List.class).get(0);
        <span class="code-keyword">if</span> (soapMessage == <span class="code-keyword">null</span>)
{
            <span class="code-object">System</span>.out.println(<span class="code-quote">"Incoming
<span class="code-keyword">null</span> message detected..."</span>);
            <span class="code-keyword">return</span> createDefaultSoapMessage(<span
class="code-quote">"Greetings from Apache Camel!!!!"</span>, <span class="code-quote">"<span
class="code-keyword">null</span>"</span>);
        }

        <span class="code-keyword">try</span> {
            SOAPPart sp = soapMessage.getSOAPPart();
            SOAPEnvelope se = sp.getEnvelope();
            SOAPBody sb = se.getBody();
            <span class="code-object">String</span> requestText = sb.getFirstChild().getTextContent();
            <span class="code-object">System</span>.out.println(requestText);
            <span class="code-keyword">return</span> createDefaultSoapMessage(<span
class="code-quote">"Greetings from Apache Camel!!!!"</span>, requestText);
        } <span class="code-keyword">catch</span> (Exception e) {
            e.printStackTrace();
            <span class="code-keyword">return</span> createDefaultSoapMessage(<span
class="code-quote">"Greetings from Apache Camel!!!!"</span>, e.getClass().getName());
        }
    }

    <span class="code-keyword">public</span> <span class="code-keyword">static</span>
SOAPMessage createDefaultSoapMessage(<span class="code-object">String</span> responseMessage,
<span class="code-object">String</span> requestMessage) {
        <span class="code-keyword">try</span> {
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
            SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();

            QName payloadName = <span class="code-keyword">new</span> QName(<span
class="code-quote">"http:<span class="code-comment">//apache.org/hello_world_soap_http/types"</span>,
<span class="code-quote">"greetMeResponse"</span>, <span class="code-quote">"ns1"</span>);
</span>
            SOAPBodyElement payload = body.addBodyElement(payloadName);

            SOAPElement message = payload.addChildElement(<span class="code-quote">"responseType"</span>);

            message.addTextNode(responseMessage + <span class="code-quote">" Request
was  "</span> + requestMessage);
            <span class="code-keyword">return</span> soapMessage;
        } <span class="code-keyword">catch</span> (SOAPException e) {
            e.printStackTrace();
            <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
RuntimeException(e);
        }
    }

}
</pre>
</div></div>
    </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+Example">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=68003&revisedVersion=17&originalVersion=16">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXF+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message