Java – Spring Boot applications with Apache Camel shut down immediately after startup

Spring Boot applications with Apache Camel shut down immediately after startup… here is a solution to the problem.

Spring Boot applications with Apache Camel shut down immediately after startup

My application is written in Java using Spring Boot and Apache Camel. When running in my Windows development environment, it starts and runs fine. But when running in my Open Shift test environment, the application starts normally and then closes like I pressed CTRL+C.

Some suggestions on the web are to add or remove certain dependencies. This has been done in my Maven pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Here is the most relevant part of my log:

2019-04-23T12:34:37.009+0000 INFO  [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: myservice-localhost started and consuming from:  direct://myservice-localhost
2019-04-23T12:34:37.011+0000 INFO  [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: route1 started and consuming from: direct://myservice-myroute
2019-04-23T12:34:37.016+0000 INFO  [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: myservice started and consuming from: servlet: /myservice/%7BcompanyID%7D
2019-04-23T12:34:37.021+0000 INFO  [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Total 9 routes, of which 9 are started
2019-04-23T12:34:37.041+0000 INFO  [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Apache Camel 2.21.0 (CamelContext: OBGW-Camel) started in 4.165 seconds
2019-04-23T12:34:37.289+0000 INFO  [main] [org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer] [] [] [] [] [] [] [] [] [] [] Undertow started on port(s ) 8090 (http)
2019-04-23T12:34:37.294+0000 INFO  [main] [org.springframework.context.support.DefaultLifecycleProcessor] [] [] [] [] [] [] [] [] [] [] Starting beans in phase 0
2019-04-23T12:34:37.415+0000 INFO  [main] [org.apache.camel.component.servlet.CamelHttpTransportServlet] [] [] [] [] [] [] [] [] [] [] Initialized CamelHttpTransportServlet[name= CamelServlet, contextPath=]
2019-04-23T12:34:37.416+0000 INFO  [main] [org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer] [] [] [] [] [] [] [] [] [] [] Undertow started on port(s ) 8080 (http)
2019-04-23T12:34:37.422+0000 INFO  [main] [com.mycompany.Application] [] [] [] [] [] [] [] [] [] [] Started Application in 21.385 seconds (JVM running for 22.759)
2019-04-23T12:34:49.811+0000 INFO  [Thread-2] [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] [] [] [] [] [] [] [] [] [] [] Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5387f9e0: startup date [Tue Apr 23 12:34:17 UTC 2019]; root of context hierarchy
2019-04-23T12:34:49.812+0000 INFO  [Thread-2] [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] [] [] [] [] [] [] [] [] [] [] Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@630390b9: startup date [Tue Apr 23 12:34:31 UTC 2019]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5387f9e0
2019-04-23T12:34:49.846+0000 INFO  [Thread-2] [org.springframework.context.support.DefaultLifecycleProcessor] [] [] [] [] [] [] [] [] [] [] Stopping beans in phase 2147483647
2019-04-23T12:34:49.846+0000 INFO  [Thread-2] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Apache Camel 2.21.0 (CamelContext: OBGW-Camel) is shutting down
2019-04-23T12:34:49.852+0000 INFO  [Thread-2] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Starting to graceful shutdown 9 routes (timeout 300 seconds)
2019-04-23T12:34:49.872+0000 INFO  [Camel (OBGW-Camel) thread #1 - ShutdownTask] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Route: myservice shutdown complete, was consuming from: servlet:/myservice/%7BcompanyID%7D
2019-04-23T12:34:49.872+0000 INFO  [Camel (OBGW-Camel) thread #1 - ShutdownTask] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Route: route1 shutdown complete, was consuming from: direct://myservice-myroute

Everything is exactly the same in an Open Shift environment until the statement Started Application starts closing.

Solution

Usually, the dependency on spring-boot-starter-web is sufficient to keep the process up and running.

However, you are using Undertow as Camel’s HTTP server instead of Spring Boot’s standard Tomcat. Therefore, you may need to place

camel.springboot.main-run-controller=true

Go to your app properties (as suggested in the review). documentation for this is also mentioned on the Camel SpringBoot page.

Related Problems and Solutions