以下单元测试尝试触发每秒的quartz2路径:
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 CamelQuartzTest extends CamelTestSupport {
static private String routeId = "test-route";
@Test
public void testSuspendRoute() throws Exception {
// arrange
MockEndpoint mock = getMockEndpoint("mock:result");
// act
System.out.println("context.start()");
context.start();
Thread.sleep(2000);
System.out.println(String.format("receivedCounter = %d", mock.getReceivedCounter()));
System.out.println("context.startRoute()");
context.startRoute(routeId);
Thread.sleep(2000);
System.out.println(String.format("receivedCounter = %d", mock.getReceivedCounter()));
System.out.println("context.suspendRoute()");
context.suspendRoute(routeId);
Thread.sleep(2000);
System.out.println(String.format("receivedCounter = %d", mock.getReceivedCounter()));
System.out.println("context.resumeRoute()");
context.resumeRoute(routeId);
Thread.sleep(2000);
System.out.println(String.format("receivedCounter = %d", mock.getReceivedCounter()));
System.out.println("context.stop()");
context.stop();
System.out.println(String.format("receivedCounter = %d", mock.getReceivedCounter()));
// assert
assertEquals(4, mock.getReceivedCounter());
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("quartz2://testtimer?cron=0/1+*+*+?+*+*")
.autoStartup(false)
.routeId(routeId)
.setBody()
.simple("${header.triggerName}: ${header.fireTime}")
.to("mock:result", "stream:out");
}
};
}
}
结果输出:
context.start()
receivedCounter = 0
context.startRoute()
testtimer: Tue Oct 21 10:06:38 CEST 2014
testtimer: Tue Oct 21 10:06:39 CEST 2014
receivedCounter = 2
context.suspendRoute()
receivedCounter = 2
context.resumeRoute()
testtimer: Tue Oct 21 10:06:41 CEST 2014
testtimer: Tue Oct 21 10:06:41 CEST 2014
testtimer: Tue Oct 21 10:06:42 CEST 2014
testtimer: Tue Oct 21 10:06:43 CEST 2014
receivedCounter = 6
context.stop()
receivedCounter = 6
恢复路线后,结果显示4个传入触发器,而预期2个。显然,石英2计时器在路线暂停时继续射击。如何在路线暂停时让quartz2暂停?
答案 0 :(得分:0)
找到根本原因:如果石英作业暂停一段时间并再次恢复,石英的默认行为是赶上暂停期间错过的触发器,即“失火”。我找不到关闭这种失火行为的方法。但是,在我的情况下,将失火阈值从60秒降低到500毫秒。这可以通过在默认类路径中将默认quartz.properties
从quartz-<version>.jar
复制到org/quartz/quartz.properties
来完成,并取消失火阈值:
# Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance.
# This file overrules the default quartz.properties file in the
# quartz-<version>.jar
#
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
# default threshold: 60 seconds
#org.quartz.jobStore.misfireThreshold: 60000
# overruled threshold: 500 ms, to prevent superfluous triggers after resuming
# a quartz job
org.quartz.jobStore.misfireThreshold: 500
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore