camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Servlet Tomcat No Spring Example
Date Fri, 04 Jan 2013 12:31: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/Servlet+Tomcat+No+Spring+Example">Servlet
Tomcat No Spring 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="ServletTomcatNoSpringExample-ServletTomcatNoSpringExample"></a>Servlet
Tomcat No Spring Example</h2>
<p><b>Available as of Camel 2.11</b></p>

<p>This example is located in the <tt>examples/camel-example-servlet-tomcat-no-spring</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="ServletTomcatNoSpringExample-About"></a>About</h3>

<p>This example demonstrates how you can create light-weight web applications without
the need for Spring Framework.<br/>
The example contains a Camel routes that used the <a href="/confluence/display/CAMEL/SERVLET"
title="SERVLET">Servlet</a> component to expose a http service.</p>

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

<p>In the <tt>web.xml</tt> file in the <tt>src/main/webapp/WEB-INF</tt>
folder we have both a CamelServlet and CamelContextListener defined.<br/>
The CamelServlet is mandatory to do when using the <a href="/confluence/display/CAMEL/SERVLET"
title="SERVLET">Servlet</a> component. And the <tt>CamelServletContextListener</tt>
is used to bootstrap the Camel application. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>web.xml</b></div><div class="codeContent
panelContent">
<pre class="code-xml">&lt;web-app version=<span class="code-quote">"2.4"</span>
xmlns=<span class="code-quote">"http://java.sun.com/xml/ns/j2ee"</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/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span>&gt;

  <span class="code-tag">&lt;display-name&gt;</span>My Web Application<span
class="code-tag">&lt;/display-name&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- you can configure
any of the properties on CamelContext, eg setName will be configured as below --&gt;</span></span>
  <span class="code-tag">&lt;context-param&gt;</span>
    <span class="code-tag">&lt;param-name&gt;</span>name<span class="code-tag">&lt;/param-name&gt;</span>
    <span class="code-tag">&lt;param-value&gt;</span>MyCamel<span class="code-tag">&lt;/param-value&gt;</span>
  <span class="code-tag">&lt;/context-param&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- location of Camel
route xml files --&gt;</span></span>
  <span class="code-tag">&lt;context-param&gt;</span>
    <span class="code-tag">&lt;param-name&gt;</span>routeBuilder-MyRoute<span
class="code-tag">&lt;/param-name&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- define the routes
as a resource from the classpath by prefixing the value with classpath: --&gt;</span></span>
    <span class="code-tag"><span class="code-comment">&lt;!-- note: instead
of using a XML file we can also define the routes in Java code in a RouteBuilder class --&gt;</span></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>

  <span class="code-tag"><span class="code-comment">&lt;!-- the listener that
kick-starts Camel --&gt;</span></span>
  <span class="code-tag">&lt;listener&gt;</span>
    <span class="code-tag">&lt;listener-class&gt;</span>org.apache.camel.component.servletlistener.CamelServletContextListener<span
class="code-tag">&lt;/listener-class&gt;</span>
  <span class="code-tag">&lt;/listener&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- Camel servlet
used in the Camel application --&gt;</span></span>
  <span class="code-tag">&lt;servlet&gt;</span>
    <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
    <span class="code-tag">&lt;servlet-class&gt;</span>org.apache.camel.component.servlet.CamelHttpTransportServlet<span
class="code-tag">&lt;/servlet-class&gt;</span>
    <span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
  <span class="code-tag">&lt;/servlet&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- Camel servlet
mapping --&gt;</span></span>
  <span class="code-tag">&lt;servlet-mapping&gt;</span>
    <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
    <span class="code-tag">&lt;url-pattern&gt;</span>/camel/*<span
class="code-tag">&lt;/url-pattern&gt;</span>
  <span class="code-tag">&lt;/servlet-mapping&gt;</span>

<span class="code-tag">&lt;/web-app&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>Its the <a href="/confluence/display/CAMEL/ServletListener+Component"
title="ServletListener Component">ServletListener Component</a> that is used to bootstrap
Camel in the web application. See more details at <a href="/confluence/display/CAMEL/ServletListener+Component"
title="ServletListener Component">ServletListener Component</a>.</td></tr></table></div>

<h4><a name="ServletTomcatNoSpringExample-TheCamelroute"></a>The Camel route</h4>
<p>The route is a simple <a href="/confluence/display/CAMEL/Content+Based+Router"
title="Content Based Router">Content Based Router</a> defined in the DSL XML as shown:</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"><span class="code-tag"><span class="code-comment">&lt;!--
here we have the Camel route(s). --&gt;</span></span>
&lt;!-- we must still use the http://camel.apache.org/schema/spring namespace so Camel
can load the routes
     though Spring JARs is not required --&gt;
<span class="code-tag">&lt;routes 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"><span class="code-comment">&lt;!-- incoming requests
from the servlet is routed --&gt;</span></span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"servlet:///hello"</span>/&gt;</span>
    <span class="code-tag">&lt;choice&gt;</span>
      <span class="code-tag">&lt;when&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- is there
a header with the key name? --&gt;</span></span>
        <span class="code-tag">&lt;header&gt;</span>name<span class="code-tag">&lt;/header&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- yes so return
back a message to the user --&gt;</span></span>
        <span class="code-tag">&lt;transform&gt;</span>
          <span class="code-tag">&lt;simple&gt;</span>Hello ${header.name}
how are you?<span class="code-tag">&lt;/simple&gt;</span>
        <span class="code-tag">&lt;/transform&gt;</span>
      <span class="code-tag">&lt;/when&gt;</span>
      <span class="code-tag">&lt;otherwise&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- if no name
parameter then output a syntax to the user --&gt;</span></span>
        <span class="code-tag">&lt;transform&gt;</span>
          <span class="code-tag">&lt;constant&gt;</span>Add a name parameter
to uri, eg ?name=foo<span class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;/transform&gt;</span>
      <span class="code-tag">&lt;/otherwise&gt;</span>
    <span class="code-tag">&lt;/choice&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>

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

<h3><a name="ServletTomcatNoSpringExample-Runningtheexample"></a>Running
the example</h3>

<p>This example runs in any web container such as Apache Tomcat. For example to deploy
in Apache Tomcat you will have to package the .war file and copy it to the <tt>webapp</tt>
folder of Tomcat, which is the hot deploy folder.</p>

<p><b>Note:</b> You have to use the version number of Camel you use. In
this documentation we are using 2.11.0.</p>

<p>There is a main page at: <tt><a href="http://localhost:8080/camel-example-servlet-tomcat-no-spring-2.11.0"
class="external-link" rel="nofollow">http://localhost:8080/camel-example-servlet-tomcat-no-spring-2.11.0</a></tt>
which has more instructions.<br/>
You can then use a web browser and send a request to the <tt><a href="http://localhost:8080/camel-example-servlet-tomcat-no-spring-2.11.0/camel/hello"
class="external-link" rel="nofollow">http://localhost:8080/camel-example-servlet-tomcat-no-spring-2.11.0/camel/hello</a></tt>
url. </p>

<h3><a name="ServletTomcatNoSpringExample-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/Servlet+Tomcat+Example" title="Servlet Tomcat
Example">Servlet Tomcat Example</a></li>
	<li><a href="/confluence/display/CAMEL/ServletListener+Component" title="ServletListener
Component">ServletListener Component</a></li>
	<li><a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">Servlet</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</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/Servlet+Tomcat+No+Spring+Example">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/Servlet+Tomcat+No+Spring+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message