Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-20115: Support for Start Date and End Date in camel-quartz #12089

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions components/camel-quartz/src/main/docs/quartz-component.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ quartz://groupName/timerName?cron=0+0/5+12-18+?+*+MON-FRI&trigger.timeZone=Europ

The timeZone value is the values accepted by `java.util.TimeZone`.

== Specifying start date

The Quartz Scheduler allows you to configure start date per trigger. You can provide the start date as TimeInMillis as follows:

----
quartz://groupName/timerName?cron=0+0/5+12-18+?+*+MON-FRI&trigger.startAt=1700452232554
----

== Specifying end date

The Quartz Scheduler allows you to configure end date per trigger. You can provide the end date as TimeInMillis as follows:

----
quartz://groupName/timerName?cron=0+0/5+12-18+?+*+MON-FRI&trigger.endAt=1700452232554
----

== Configuring misfire instructions

The quartz scheduler can be configured with a misfire instruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.component.quartz;

import java.util.Date;
import java.util.Collection;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -53,6 +54,12 @@ public class CamelJob implements Job, InterruptableJob {
public void execute(JobExecutionContext context) throws JobExecutionException {
Exchange exchange = null;
try {
if (hasTriggerExpired(context)) {
LOG.warn("Trigger exists outside StartTime={} and EndTime={}. Skipping CamelJob jobExecutionContext={}",
context.getTrigger().getStartTime(), context.getTrigger().getEndTime(), context);
return;
}

if (LOG.isDebugEnabled()) {
LOG.debug("Running CamelJob jobExecutionContext={}", context);
}
Expand Down Expand Up @@ -94,6 +101,25 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
}
}

/**
* Validates if the Fire Time lies within the Start Time and End Time
*
* @param context
*
* @return
*/
private boolean hasTriggerExpired(JobExecutionContext context) {
Date fireTime = context.getFireTime();

// Trigger valid if Start Time is null or before Fire Time
boolean validStartTime = context.getTrigger().getStartTime() == null || fireTime.after(context.getTrigger().getStartTime());

// Trigger valid if End Time is null or after Fire Time
boolean validEndTime = context.getTrigger().getEndTime() == null || fireTime.before(context.getTrigger().getEndTime());

return !(validStartTime && validEndTime);
}

protected CamelContext getCamelContext(JobExecutionContext context) throws JobExecutionException {
SchedulerContext schedulerContext = getSchedulerContext(context);
String camelContextName = context.getMergedJobDataMap().getString(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,7 @@ private void addJobInScheduler() throws Exception {
}
} else {
try {
// calculate whether the trigger can be triggered in the future
Calendar cal = null;
if (trigger.getCalendarName() != null) {
cal = scheduler.getCalendar(trigger.getCalendarName());
}
OperableTrigger ot = (OperableTrigger) trigger;
Date ft = ot.computeFirstFireTime(cal);
if (ft == null && ignoreExpiredNextFireTime) {
if (hasTriggerExpired(scheduler, trigger)) {
scheduled = false;
LOG.warn(
"Job {} (cron={}, triggerType={}, jobClass={}) not scheduled, because it will never fire in the future",
Expand Down Expand Up @@ -436,6 +429,22 @@ private void addJobInScheduler() throws Exception {
jobAdded.set(true);
}

private boolean hasTriggerExpired(Scheduler scheduler, Trigger trigger) throws SchedulerException {
Calendar cal = null;
if (trigger.getCalendarName() != null) {
cal = scheduler.getCalendar(trigger.getCalendarName());
}
OperableTrigger ot = (OperableTrigger) trigger;

// check if current time is past the Trigger EndDate
if (ot.getEndTime() != null && new Date().after(ot.getEndTime())) {
return true;
}
// calculate whether the trigger can be triggered in the future
Date ft = ot.computeFirstFireTime(cal);
return (ft == null && ignoreExpiredNextFireTime);
}

private boolean hasTriggerChanged(Trigger oldTrigger, Trigger newTrigger) {
if (newTrigger instanceof CronTrigger && oldTrigger instanceof CronTrigger) {
CronTrigger newCron = (CronTrigger) newTrigger;
Expand Down Expand Up @@ -474,6 +483,14 @@ private Trigger createTrigger(JobDetail jobDetail) throws Exception {
}
if (cron != null) {
LOG.debug("Creating CronTrigger: {}", cron);
final String startAt = (String) copy.get("startAt");
if (startAt != null) {
triggerBuilder.startAt(new Date(Long.parseLong(startAt)));
}
final String endAt = (String) copy.get("endAt");
if (endAt != null) {
triggerBuilder.endAt(new Date(Long.parseLong(endAt)));
}
final String timeZone = (String) copy.get("timeZone");
if (timeZone != null) {
if (ObjectHelper.isNotEmpty(customCalendar)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.quartz;

import java.util.concurrent.TimeUnit;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* This test the CronTrigger as a timer endpoint in a route.
*/
public class QuartzCronRouteWithStartDateEndDateTest extends BaseQuartzTest {

@Test
public void testQuartzCronRouteWithStartDateEndDateTest() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(2);
mock.await(5, TimeUnit.SECONDS);

MockEndpoint.assertIsSatisfied(context);
assertThat(mock.getReceivedExchanges().size() <= 3, CoreMatchers.is(true));
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
// triggers every 1th second at precise 00,01,02,03..59 with startAt and endAt exactly 2 second apart.
// configuration will create a maximum of three messages
from("quartz://myGroup/myTimerName?cron=0/1 * * * * ?&trigger.startAt=" + System.currentTimeMillis()
+ "&trigger.endAt=" + (System.currentTimeMillis() + 2000)).to("mock:result");
}
};
}
}