apache camel 不会停止并多次查询数据库行

apache camel doesn't stop and query database rows multiple times

我正在使用 apache camel 设置从 oracle 数据库(它有一个 table 包含 xml 类型列)到 mongoDB 的管道。我的问题是,如果我将值 2000ms 添加到 thread.sleep(2000) 路由停止 运行 并在轮询行中间关闭,我将该值更改为 Integer.MAX_VALUE,我发现每行都被多次添加到 mongoDB 中。我对这个框架完全陌生,所以我想你可能会帮助它!

这里是骆驼-context.xml:

<camelContext id="camelContext-f0b5f69c-a9b5-434a-974b-6fe815aa3d06" 
 xmlns="http://camel.apache.org/schema/spring">
    <!-- here is a sample which processes the input files
     (leaving them in place - see the 'noop' flag)
     then performs content based routing on the message using XPath -->
    <threadPool id="myPool" maxPoolSize="25" maxQueueSize="200"
        poolSize="20" threadName="Cool"/>
    <route id="jdbc_connect"  streamCache="true">
        <from uri="timer://foo"/>
        <to id="_to3" uri="sql:SELECT * FROM Schema.Park ? 
dataSource=#dataSource&amp;outputType=StreamList"/>
        <split executorServiceRef="myPool" id="_split1"
            parallelProcessing="true" stopOnException="true" 
            streaming="true">
            <simple>${in.body}</simple>
            <process id="_process1" ref="rowProcessor"/>
            <unmarshal id="_unmarshal1">
                <jaxb contextPath="OracleMongo.Orcl.JAXB"/>
            </unmarshal>
            <marshal id="_marshal1">
                <json library="Gson"/>
            </marshal>
            <convertBodyTo id="_convertBodyTo1" type="String"/>
            <to id="_to2" uri="mongodb3:mongoBean? 
        database=test&amp;collection=Park&amp;operation=insert"/>
        </split>
    </route>
</camelContext>

这是主要内容 class:

 public static final void main(String[] args) throws Exception {
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
            "META-INF/spring/camel-context.xml");
    CamelContext camelContext = SpringCamelContext.springCamelContext(
            appContext, false);
    try {
        camelContext.start();
        Thread.sleep(Integer.MAX_VALUE);
    } finally {
        camelContext.stop();
    }
}

这是我将值 2000 毫秒添加到 Thread.sleep 时得到的结果:

 thread #2 - ShutdownTask] DefaultShutdownStrategy        INFO  Route: 
 jdbc_connect shutdown complete, was consuming from: timer://foo
 [                          main] DefaultShutdownStrategy        INFO  
 Graceful shutdown of 1 routes completed in 300 seconds
 [                          main] DefaultInflightRepository      WARN  
 Shutting down while there are still 11 inflight exchanges.

为了让您的主线程 运行 成为守护进程,您可以使用 Main class 实用程序来提供上下文:

public static void main(String[] args) throws Exception {
    org.apache.camel.spring.Main main = new org.apache.camel.spring.Main();
    main.setApplicationContextUri("META-INF/spring/camel-context.xml");
    main.run();
}

这将执行您的路由,而不必担心在您的主线程上添加任何 sleep(...)。当然,由于您不知道路由何时结束,因此您必须手动停止它(CTRL+C 向 JVM 发出信号),不确定这是否足以满足您的用例。

关于计时器,正如克劳斯在问题评论中所建议的那样,只需使用重复选项即可执行一次(默认情况下,计时器将无限期地每秒启动一次):

<from uri="timer://foo?repeatCount=1"/>

希望对您有所帮助。