Spring 允许骆驼上下文永远运行

Spring 允许骆驼上下文永远运行,spring,apache-camel,Spring,Apache Camel,我正在为springCamelContext使用camel-spring罐。当我启动camel上下文时,它会运行5分钟(默认时间)。我可以让我的线程在某个特定的时间睡眠,例如 try { camelContext.start(); Thread.sleep(50 * 60 * 1000); camelContext.stop(); } catch (Exception e) { e.p

我正在为springCamelContext使用camel-spring罐。当我启动camel上下文时,它会运行5分钟(默认时间)。我可以让我的线程在某个特定的时间睡眠,例如

try {
            camelContext.start();
            Thread.sleep(50 * 60 * 1000);
            camelContext.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
但我希望我的camelContext永远运行,因为这个应用程序将被部署,它将监听来自KAFKA服务器的消息。我知道有一门课

org.apache.camel.spring.Main
但我不知道如何用springCamelContext配置它,也不知道是否还有其他方法。谢谢

更新:即使我删除了camelContext.stop(),上下文也会在一段时间后停止,我会得到以下日志:

[Thread-1] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) is shutting down
    [Thread-1] INFO org.apache.camel.impl.DefaultShutdownStrategy - Starting to graceful shutdown 1 routes (timeout 300 seconds)
    [Camel (camel-1) thread #1 - ShutdownTask] INFO org.apache.camel.component.kafka.KafkaConsumer - Stopping Kafka consumer
    [Camel (camel-1) thread #1 - ShutdownTask] INFO org.apache.camel.impl.DefaultShutdownStrategy - Route: route1 shutdown complete, was consuming from: Endpoint[kafka://localhost:9092?groupId=group0&serializerClass=org.springframework.integration.kafka.serializer.avro.AvroSerializer&topic=my-topic]
    [Thread-1] INFO org.apache.camel.impl.DefaultShutdownStrategy - Graceful shutdown of 1 routes completed in 0 seconds
    [Thread-1] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) uptime 4 minutes
    [Thread-1] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.2 (CamelContext: camel-1) is shutdown in 0.022 seconds 

下面是一个简单的示例,它永远运行,只将文件从一个文件夹复制到另一个文件夹:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class FileWriteRoute {

    public static void main(String[] args) throws Exception {

        Main main = new Main();

        main.addRouteBuilder(new RouteBuilder() {

            public void configure() {
                from("file://D:/dev/playground/camel-activemq/src/data")
                        .to("file://D:/dev/playground/camel-activemq/src/data_out");
            }
        });

        main.run();
    }
}

如果在类中定义了路线,请尝试:

public static void main(String[] args) throws Exception {

  Main main = new Main();
  CamelContext context = main.getOrCreateCamelContext();
  try {
    context.addRoutes(new YOURROUTECLASS());
    context.start();
    main.run();
  }
  catch (Exception e){
    enter code here
  }
 }

你为什么调用context.stop()?@MartinFrey我已经更新了我的问题。请看一看,你是怎么部署骆驼的?据我所知,Camel上下文是一个长期运行的环境,它的使用寿命与应用程序的使用寿命一样长。你看了吗:@RamachandranGA我已经看过了,正如我在问题中提到的。骆驼上下文的默认正常运行时间为5分钟。我想知道springCamelContext中是否有任何属性/方法可以让我的上下文永远运行,直到JVM终止。