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 Tue, 28 Jun 2011 23:49: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+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="https://cwiki.apache.org/confluence/display/~boday">Ben
O&#39;Day</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        added hints for Maven war packaging setup...<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" >{code} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">To
enable more rapid development we hightly recommend the [jetty:run maven plugin|http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin].
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">You
should update your Maven pom.xml to enable WAR packaging/naming like this... <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code:xml}
<br>&lt;project&gt; <br>    ... <br>    &lt;packaging&gt;war&lt;/packaging&gt;
<br>    ... <br>    &lt;build&gt; <br>	&lt;finalName&gt;[desired
WAR file name]&lt;/finalName&gt; <br>        ... <br>    &lt;/build&gt;
<br>{code} <br> <br>To enable more rapid development we highly recommend
the [jetty:run maven plugin|http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin]. <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >Please refer to the [help for more
information on using jetty:run|http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin]
- but briefly if you add the following to your pom.xml  <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{code<span
class="diff-added-chars"style="background-color: #dfd;">:xml</span>}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >  &lt;build&gt; <br>
   &lt;plugins&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>You should update your Maven pom.xml to enable WAR packaging/naming like this...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;project&gt;</span>
    ...
    <span class="code-tag">&lt;packaging&gt;</span>war<span class="code-tag">&lt;/packaging&gt;</span>
    ...
    <span class="code-tag">&lt;build&gt;</span>
	<span class="code-tag">&lt;finalName&gt;</span>[desired WAR file name]<span
class="code-tag">&lt;/finalName&gt;</span>
        ...
    <span class="code-tag">&lt;/build&gt;</span>
</pre>
</div></div>

<p>To enable more rapid development we highly 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-xml">
  <span class="code-tag">&lt;build&gt;</span>
    <span class="code-tag">&lt;plugins&gt;</span>
      <span class="code-tag">&lt;plugin&gt;</span>
        <span class="code-tag">&lt;groupId&gt;</span>org.mortbay.jetty<span
class="code-tag">&lt;/groupId&gt;</span>
        <span class="code-tag">&lt;artifactId&gt;</span>maven-jetty-plugin<span
class="code-tag">&lt;/artifactId&gt;</span>
        <span class="code-tag">&lt;configuration&gt;</span>
          <span class="code-tag">&lt;webAppConfig&gt;</span>
            <span class="code-tag">&lt;contextPath&gt;</span>/<span
class="code-tag">&lt;/contextPath&gt;</span>
          <span class="code-tag">&lt;/webAppConfig&gt;</span>
          <span class="code-tag">&lt;scanIntervalSeconds&gt;</span>10<span
class="code-tag">&lt;/scanIntervalSeconds&gt;</span>
        <span class="code-tag">&lt;/configuration&gt;</span>
      <span class="code-tag">&lt;/plugin&gt;</span>
    <span class="code-tag">&lt;/plugins&gt;</span>
  <span class="code-tag">&lt;/build&gt;</span>
</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="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+on+using+Camel+in+a+Web+Application">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97043&revisedVersion=12&originalVersion=11">View
Changes</a>
                |
        <a href="https://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