使springboot app等到我的Apache camel处理器在springboot-apache-camel-helloworld-app中完全执行

时间:2017-04-05 11:56:46

标签: spring spring-boot apache-camel

我正在尝试使用apache camel的简单弹簧启动应用程序。我有两个伪骆驼处理器,它们的sysout方法中有process()个。我有一个路由构建器,一个接一个地调用这些处理器。问题是我的springboot应用程序关闭太快,迫使camel上下文关闭,而不让处理器在控制台上执行和输出任何内容。

简单代码如下所示

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>
    <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>

控制台输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

当我在Thread.sleep(10000);类的main()方法末尾添加Application时,它会正确地让处理器运行并将相应的行打印到输出。

我知道这一点(springboot应用程序退出而不让处理器运行)是正确/预期的行为,但我想知道如果有任何方法可以让spring引导应用程序等到camel处理器完成执行。

0 个答案:

没有答案