使用Camel和Quartz 2

时间:2015-04-22 13:40:15

标签: apache-camel quartz-scheduler

我想使用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();

}

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

1 个答案:

答案 0 :(得分:0)

我建议你在开始编码之前创建单元测试,甚至用它们来开发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),则测试将不会通过,但路由定义不会引发任何错误/异常。