How to deploy a camel java DSL servlet on Tomcat server?

I have found several examples of camel servlet for tomcat server using spring DSL, I want to use Java DSL and create a RouteBuilder as shown below. This servlet works as expected on Jetty when I run using mvn jetty: run command, but doesn’t work with tomcat server.


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class DefaultRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        
        System.out.println("DefaultRouter");
        from("servlet:hello?matchOnUriPrefix=true").process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
                String path = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
                path = path.substring(path.lastIndexOf("/"));
                exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");
                exchange.getOut().setHeader("PATH", path);
                exchange.getOut().setBody("<b>Hello World</b>");
            }
        });
    }
}
    

As per my understanding my web.xml looks like this

<?xml version="1.0" encoding="ISO-8859-1"?>

<webapp>
  <display-name>camel-servlet</display-name>

    <context-param>
        <param-name>name</param-name>
        <param-value>MyCamel</param-value>
    </context-param>
    <context-param>
        <param-name>routeBuilder-MyRoute</param-name>
        <param-value>org.apache.camel.community.so.servletlistener.DefaultRouteBuilder</param-value>
    </context-param>
  <listener>
    <listener-class>org.apache.camel.component.servletlistener.JndiCamelServletContextListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>CamelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CamelServlet</servlet-name>
    <url-pattern>/camel/*</url-pattern>
  </servlet-mapping>
</webapp>

Edit: Logs from mvn jetty: run command –

[INFO] Scanning elapsed time=1409ms
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] node0 Scavenging every 660000ms
2022-07-19 11:15:39,454 [main           ] INFO  CamelServletContextListener    - CamelContextServletListener initializing ...
DefaultRouter
2022-07-19 11:15:39,797 [main           ] INFO  ServletCamelContext            - Apache Camel 2.25.0 (CamelContext: MyCamel) is starting
2022-07-19 11:15:39,804 [main           ] INFO  ManagedManagementStrategy      - JMX is enabled
2022-07-19 11:15:40,094 [main           ] INFO  DefaultTypeConverter           - Type converters loaded (core: 195, classpath: 9)
2022-07-19 11:15:40,233 [main           ] INFO  ServletCamelContext            - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2022-07-19 11:15:40,272 [main           ] INFO  ServletCamelContext            - Route: route1 started and consuming from: servlet:/hello?matchOnUriPrefix=true
2022-07-19 11:15:40,274 [main           ] INFO  ServletCamelContext            - Total 1 routes, of which 1 are started
2022-07-19 11:15:40,276 [main           ] INFO  ServletCamelContext            - Apache Camel 2.25.0 (CamelContext: MyCamel) started in 0.483 seconds
2022-07-19 11:15:40,277 [main           ] INFO  CamelServletContextListener    - CamelContextServletListener initialized
2022-07-19 11:15:40,316 [main           ] INFO  CamelHttpTransportServlet      - Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=/camel-example-servlet]
[INFO] Started o.e.j.m.p.JettyWebAppContext@2764c546{Camel Servlet Example,/camel-example-servlet,file:///C:/Users/rrdoshi/Downloads/camel-example-servlet/src/main/webapp/,AVAILABLE}{file:///C:/Users/rrdoshi/Downloads/camel-example-servlet/src/main/webapp/}
[INFO] Started ServerConnector@2b73bd6b{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[INFO] Started @8097ms
[INFO] Started Jetty Server

For tomcat deployment, I am placing the war file in tomcat webapps directory and executing startup.bat

19-Jul-2022 11:21:21.403 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
19-Jul-2022 11:21:21.576 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [146] milliseconds.
19-Jul-2022 11:21:21.596 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsApacheCamelDemoServlet]
19-Jul-2022 11:21:21.645 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsApacheCamelDemoServlet] has finished in [49] ms
19-Jul-2022 11:21:21.646 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsdocs]
19-Jul-2022 11:21:21.671 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsdocs] has finished in [25] ms
19-Jul-2022 11:21:21.672 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsexamples]
19-Jul-2022 11:21:22.097 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsexamples] has finished in [425] ms
19-Jul-2022 11:21:22.099 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappshost-manager]
19-Jul-2022 11:21:22.135 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappshost-manager] has finished in [36] ms
19-Jul-2022 11:21:22.136 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsmanager]
19-Jul-2022 11:21:22.175 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsmanager] has finished in [38] ms
19-Jul-2022 11:21:22.175 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsROOT]
19-Jul-2022 11:21:22.202 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:UsersrrdoshiDownloadsapache-tomcat-9.0.64-windows-x64apache-tomcat-9.0.64webappsROOT] has finished in [26] ms
19-Jul-2022 11:21:22.205 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8088"]
19-Jul-2022 11:21:22.224 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [2721] milliseconds

access logs

127.0.0.1 - - [19/Jul/2022:11:23:51 +0530] "GET /camel-servlet/ HTTP/1.1" 200 1675
127.0.0.1 - - [19/Jul/2022:11:24:01 +0530] "GET /camel-servlet/camel/hello HTTP/1.1" 404 784

localhost logs

19-Jul-2022 11:21:22.085 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
19-Jul-2022 11:21:22.085 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
19-Jul-2022 11:21:22.087 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@68ab0936')

The WAR file is getting logs but there are no error. There are no logs regarding camelContext starting up.

Leave a Comment