使用 Camel 和 Quartz 2 安排具有特定顺序的路线

Schedule routes with a specific order with Camel and Quartz 2

我想使用 Apache Camel 和 Quartz 2 按特定顺序安排路线。 我定义了一个添加两条路由的上下文:

public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {

            from("quartz2://myGroup/myTimerName?trigger.repeatInterval=2000&trigger.repeatCount=5").startupOrder(1).setBody().simple("Current time is "+new SimpleDateFormat("HH:mm:ss").format(new Date())).to("stream:out");
            //from("quartz2://myGroup/myTimerName?trigger.repeatInterval=2000&trigger.repeatCount=5").setBody().simple("Current time is ${header.firedTime}").to("stream:out");

        }
    });

    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {

            from("quartz2://myGroup/myTimerName?trigger.repeatInterval=2000&trigger.repeatCount=5").startupOrder(2).setBody().simple("Current time is "+new SimpleDateFormat("HH:mm:ss").format(new Date())).to("file:C:\Users\milioli\Documents\NetBeansProjects\CamelWithQuartz\data\out.txt");
        }
    });

    context.startAllRoutes();
    Thread.sleep(10000);
    context.stop();

}

代码构建和 运行s 但我没有来自控制台的反馈,我期望顺序为 1 的路由结果。此外,顺序为 2 的路由不会写入作为参数传递的文件.每条路线,单独 运行 正确。 有人可以帮助我吗?

我建议您在开始编码之前创建单元测试,甚至将它们用于 Camel 路由的开发(只要您可以模拟端点)。

下面的测试表明,如果命名不同,您可以使用两个 Quartz 调度器:

package org.ruffp.camel.quartz;

import org.apache.camel.EndpointInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class QuartzEndpointTest extends CamelTestSupport {

    @EndpointInject(uri = "mock:DESTINATION")
    private MockEndpoint mockDestination;

    @Test
    public void testQuartz() throws Exception {
        resetMocks();
        mockDestination.expectedMessageCount(3);
        Thread.sleep(10000);
        assertMockEndpointsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                //@formatter:off
                from("quartz2://myGroup/myTimerNameOne?trigger.repeatInterval=100&trigger.repeatCount=1")
                    .setProperty("test").constant("This is the job 1")
                    .to(mockDestination);

                from("quartz2://myGroup/myTimerNameTwo?trigger.repeatInterval=100&trigger.repeatCount=0")
                .setProperty("test").constant("This is the job 2")
                .to(mockDestination);

                //@formatter:on
            }
        };
    }
}

如果您为两者输入相同的名称(例如 myTimerName),测试将不会通过,但路由定义不会抛出任何 error/exception。