-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTM-1229: introduce a dummy job that is scheduled and add it to the t…
…estdata
- Loading branch information
Showing
5 changed files
with
236 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.scheduling; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import org.quartz.DisallowConcurrentExecution; | ||
import org.quartz.JobExecutionContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.scheduling.quartz.QuartzJobBean; | ||
|
||
/** Dummy job for testing purposes. This will only log messages. */ | ||
@DisallowConcurrentExecution | ||
public class DummyJob extends QuartzJobBean { | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
@Override | ||
protected void executeInternal(@NonNull JobExecutionContext context) { | ||
logger.info("Dummy job executing, details follow:"); | ||
context | ||
.getJobDetail() | ||
.getJobDataMap() | ||
.forEach((key, value) -> logger.info(" Key: {}, Value: {}", key, value)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/org/tailormap/api/scheduling/JobCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.scheduling; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.quartz.CronScheduleBuilder; | ||
import org.quartz.DateBuilder; | ||
import org.quartz.Job; | ||
import org.quartz.JobBuilder; | ||
import org.quartz.JobDataMap; | ||
import org.quartz.JobDetail; | ||
import org.quartz.JobKey; | ||
import org.quartz.ObjectAlreadyExistsException; | ||
import org.quartz.Scheduler; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.Trigger; | ||
import org.quartz.TriggerBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class JobCreator { | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
private final Scheduler scheduler; | ||
|
||
public JobCreator(Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
} | ||
|
||
/** | ||
* Create a job and schedule it with a cron expression. | ||
* | ||
* @param job the job to create | ||
* @param jobData a map with job data, the "type" key is mandatory | ||
* @param cronExpression the cron expression | ||
* @return the job name | ||
* @throws SchedulerException if the job could not be scheduled | ||
*/ | ||
public String createJob(Class<? extends Job> job, Map<?, ?> jobData, String cronExpression) | ||
throws SchedulerException { | ||
|
||
// Create a job | ||
JobDetail jobDetail = | ||
JobBuilder.newJob() | ||
.withIdentity(new JobKey(UUID.randomUUID().toString(), jobData.get("type").toString())) | ||
.withDescription(/* TODO add parameter of get from jobData */ job.getSimpleName()) | ||
.usingJobData(new JobDataMap(jobData)) | ||
.ofType(job) | ||
.build(); | ||
|
||
// Create a trigger | ||
Trigger trigger = | ||
TriggerBuilder.newTrigger() | ||
.withIdentity(jobDetail.getKey().getName(), jobDetail.getKey().getGroup()) | ||
.startAt(DateBuilder.futureDate(30, DateBuilder.IntervalUnit.SECOND)) | ||
.withSchedule( | ||
CronScheduleBuilder.cronSchedule(cronExpression) | ||
.withMisfireHandlingInstructionFireAndProceed()) | ||
.build(); | ||
|
||
try { | ||
scheduler.scheduleJob(jobDetail, trigger); | ||
} catch (ObjectAlreadyExistsException ex) { | ||
logger.warn( | ||
"Job {} with trigger {} has not bean added to scheduler as it already exists.", | ||
jobDetail.getKey(), | ||
trigger.getKey()); | ||
return null; | ||
} | ||
|
||
return jobDetail.getKey().getName(); | ||
} | ||
} |
Oops, something went wrong.