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 on using Camel in a Web Application
Date Fri, 28 May 2010 11:08:00 GMT
<html>
<head>
    <base href="http://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="http://cwiki.apache.org/confluence/display/CAMEL/Tutorial+on+using+Camel+in+a+Web+Application">Tutorial
on using Camel in a Web Application</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (6)</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" >{code:xml} <br>&lt;?xml
version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;web-app <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot;</span>
<span class="diff-added-words"style="background-color: #dfd;">xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
        xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
<br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee</span>
<span class="diff-added-words"style="background-color: #dfd;">version=&quot;2.5&quot;&gt;</span>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot; <br>         version=&quot;2.4&quot;&gt;
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>  &lt;listener&gt;
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >       http://www.springframework.org/schema/context
 <br>       http://www.springframework.org/schema/context/spring-context-2.5.xsd <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://activemq.apache.org/camel/schema/spring</span>
<span class="diff-added-words"style="background-color: #dfd;">http://camel.apache.org/schema/spring</span>
<br><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://activemq.apache.org/camel/schema/spring/camel-spring.xsd&quot;&gt;</span>
<span class="diff-added-words"style="background-color: #dfd;">http://camel.apache.org/schema/spring/camel-spring.xsd&quot;&gt;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;camelContext <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">xmlns=&quot;http://activemq.apache.org/camel/schema/spring&quot;&gt;</span>
<span class="diff-added-words"style="background-color: #dfd;">xmlns=&quot;http://camel.apache.org/schema/spring&quot;&gt;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;route&gt; <br>
     &lt;from uri=&quot;seda:foo&quot;/&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="TutorialonusingCamelinaWebApplication-TutorialonusingCamelinaWebApplication"></a>Tutorial
on using Camel in a Web Application</h2>

<p>Camel has been designed to work great with the <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> framework; so if you are already a Spring user you can
think of Camel as just a framework for adding to your Spring XML files.</p>

<p>So you can follow the usual Spring approach to working with web applications; namely
to add the standard Spring hook to load a <b>/WEB-INF/applicationContext.xml</b>
file. In that file you can include your usual Camel XML configuration.</p>

<h3><a name="TutorialonusingCamelinaWebApplication-Step1%3AEdityourweb.xml"></a>Step1:
Edit your web.xml</h3>

<p>To enable spring add a context loader listener to your <b>/WEB-INF/web.xml</b>
file</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span>
encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
&lt;web-app xmlns=<span class="code-quote">"http://java.sun.com/xml/ns/javaee"</span>
         <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
         xsi:schemaLocation=<span class="code-quote">"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span>
         version=<span class="code-quote">"2.5"</span>&gt;

  <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>

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

<p>This will cause Spring to boot up and look for the <b>/WEB-INF/applicationContext.xml</b>
file.</p>

<h3><a name="TutorialonusingCamelinaWebApplication-Step2%3ACreatea%2FWEBINF%2FapplicationContext.xmlfile"></a>Step
2: Create a /WEB-INF/applicationContext.xml file</h3>

<p>Now you just need to create your Spring XML file and add your camel routes or configuration.</p>

<p>For example</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span>
encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
&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:context</span>=<span class="code-quote">"http://www.springframework.org/schema/context"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd"&gt;

  <span class="code-tag">&lt;camelContext 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">"seda:foo"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:results"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>

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

<p>Then boot up your web application and you're good to go!</p>


<h3><a name="TutorialonusingCamelinaWebApplication-HintsandTips"></a>Hints
and Tips</h3>

<p>If you use <a href="http://maven.apache.org/" class="external-link" rel="nofollow">Maven</a>
to build your application your directory tree will look like this...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
src/main/webapp/WEB-INF
  web.xml
  applicationContext.xml
</pre>
</div></div>

<p>To enable more rapid development we hightly recommend the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin"
class="external-link" rel="nofollow">jetty:run maven plugin</a>.</p>

<p>Please refer to the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin"
class="external-link" rel="nofollow">help for more information on using jetty:run</a>
- but briefly if you add the following to your pom.xml </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.mortbay.jetty&lt;/groupId&gt;
        &lt;artifactId&gt;maven-jetty-plugin&lt;/artifactId&gt;
        &lt;configuration&gt;
          &lt;webAppConfig&gt;
            &lt;contextPath&gt;/&lt;/contextPath&gt;
          &lt;/webAppConfig&gt;
          &lt;scanIntervalSeconds&gt;10&lt;/scanIntervalSeconds&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
</pre>
</div></div>

<p>Then you can run your web application as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn jetty:run
</pre>
</div></div>

<p>Then Jetty will also monitor your target/classes directory and your src/main/webapp
directory so that if you modify your spring XML, your web.xml or your java code the web application
will be restarted, re-creating your Camel routes.</p>

<p>If your unit tests take a while to run, you could miss them out when running your
web application via</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn -Dtest=<span class="code-keyword">false</span> jetty:run
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/Tutorial+on+using+Camel+in+a+Web+Application">View
Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97043&revisedVersion=11&originalVersion=10">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/Tutorial+on+using+Camel+in+a+Web+Application?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message