0

I am trying out simple spring boot app with apache camel. I have two dummy camel processors, which have sysouts in their process() methods. I have one route builder which calls these processors one after other. The issue is that my springboot application shutdowns too quickly forcing camel context to shutdown without letting processors to execute and output anything on console.

The simple code looks like this

MyProcessor1.java

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

@Component
public class MyProcessor1 implements Processor
{
    public void process(Exchange exchange) throws Exception {
        System.out.println("Inside my processor 1");
    }
}

MyProcessor2.java

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

@Component
public class MyProcessor2 implements Processor
{
    public void process(Exchange exchange) throws Exception {
        System.out.println("Inside my processor 2");
    }
}

MyRouteBuilder.java

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.digitate.ignio.smarttriggers.processors.MyProcessor1;
import com.digitate.ignio.smarttriggers.processors.MyProcessor2;

@Component
public class MyRouteBuilder extends RouteBuilder
{
    @Autowired
    MyProcessor1 myProcessor1;

    @Autowired
    MyProcessor2 myProcessor2;

    public MyRouteBuilder(){}

    @Override
    public void configure() throws Exception {
        System.out.println("Inside route builder");
        from("file:C:\\Mahesh\\delete\\camelsource")
            .process(myProcessor1)
            .process(myProcessor2);
    }
}

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }
}

pom.xml (maven dependencies)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.5.2.RELEASE</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>2.17.0</version>
    </dependency>
</dependencies>

The console output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-04-05 17:17:14.587  INFO 5600 --- [           main] c.d.i.s.Application      : Starting Application on mymachine with PID 5600 (C:\Mahesh\workspaces\ws1\SpringbootCamelDemo\target\classes started by mahesh in C:\Mahesh\workspaces\ws1\SpringbootCamelDemo)
2017-04-05 17:17:14.592  INFO 5600 --- [           main] c.d.i.s.Application      : No active profile set, falling back to default profiles: default
2017-04-05 17:17:14.681  INFO 5600 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@276438c9: startup date [Wed Apr 05 17:17:14 IST 2017]; root of context hierarchy
2017-04-05 17:17:15.363  INFO 5600 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration$$EnhancerBySpringCGLIB$$3b1b7344] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-05 17:17:15.851  INFO 5600 --- [           main] o.a.c.i.converter.DefaultTypeConverter   : Loaded 185 type converters
2017-04-05 17:17:16.126  INFO 5600 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
Inside route builder
2017-04-05 17:17:16.166  INFO 5600 --- [           main] o.a.camel.spring.boot.RoutesCollector    : Loading additional Camel XML routes from: classpath:camel/*.xml
2017-04-05 17:17:16.166  INFO 5600 --- [           main] o.a.camel.spring.boot.RoutesCollector    : Loading additional Camel XML rests from: classpath:camel-rest/*.xml
2017-04-05 17:17:16.167  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.3 (CamelContext: camel-1) is starting
2017-04-05 17:17:16.168  INFO 5600 --- [           main] o.a.c.m.ManagedManagementStrategy        : JMX is enabled
2017-04-05 17:17:16.262  INFO 5600 --- [           main] o.a.c.i.DefaultRuntimeEndpointRegistry   : Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
2017-04-05 17:17:16.376  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2017-04-05 17:17:16.376  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : 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
2017-04-05 17:17:16.555  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: route1 started and consuming from: Endpoint[file://C:%5CMahesh%5Cdelete%5Ccamelsource]
2017-04-05 17:17:16.556  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 1 routes, of which 1 are started.
2017-04-05 17:17:16.559  INFO 5600 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.3 (CamelContext: camel-1) started in 0.390 seconds
2017-04-05 17:17:16.566  INFO 5600 --- [           main] c.d.i.s.Application      : Started Application in 2.364 seconds (JVM running for 2.951)
2017-04-05 17:17:16.567  INFO 5600 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@276438c9: startup date [Wed Apr 05 17:17:14 IST 2017]; root of context hierarchy
2017-04-05 17:17:16.568  INFO 5600 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-04-05 17:17:16.569  INFO 5600 --- [       Thread-2] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.3 (CamelContext: camel-1) is shutting down
2017-04-05 17:17:16.570  INFO 5600 --- [       Thread-2] o.a.camel.impl.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 300 seconds)
2017-04-05 17:17:16.575  INFO 5600 --- [ - ShutdownTask] o.a.camel.impl.DefaultShutdownStrategy   : Route: route1 shutdown complete, was consuming from: Endpoint[file://C:%5CMahesh%5Cdelete%5Ccamelsource]
2017-04-05 17:17:16.575  INFO 5600 --- [       Thread-2] o.a.camel.impl.DefaultShutdownStrategy   : Graceful shutdown of 1 routes completed in 0 seconds
2017-04-05 17:17:16.579  INFO 5600 --- [       Thread-2] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.3 (CamelContext: camel-1) uptime 0.413 seconds
2017-04-05 17:17:16.579  INFO 5600 --- [       Thread-2] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.3 (CamelContext: camel-1) is shutdown in 0.010 seconds

When I add Thread.sleep(10000); at the end of main() method in Application class, it correctly lets processors run and print corresponding line to the output.

I know this (springboot application exiting without letting processors run) is correct/expected behavior, but I want to know how if there is any way with which I can make spring boot application to wait till camel processors complete executing.

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

0 Answers0