-
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.
#10 extract Quartz Scheduler from git module to global config
performance improvent by replacing file.getConanicalPath
- Loading branch information
Thorsten Marx
committed
Aug 20, 2024
1 parent
d71b21b
commit f7b8a86
Showing
32 changed files
with
598 additions
and
134 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
33 changes: 33 additions & 0 deletions
33
cms-api/src/main/java/com/github/thmarx/cms/api/scheduler/CronJob.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,33 @@ | ||
package com.github.thmarx.cms.api.scheduler; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public interface CronJob extends Consumer<CronJobContext> { | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
cms-api/src/main/java/com/github/thmarx/cms/api/scheduler/CronJobContext.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,33 @@ | ||
package com.github.thmarx.cms.api.scheduler; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import com.github.thmarx.cms.api.feature.FeatureContainer; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class CronJobContext extends FeatureContainer { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
cms-api/src/main/java/com/github/thmarx/cms/api/scheduler/CronJobScheduler.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,32 @@ | ||
package com.github.thmarx.cms.api.scheduler; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public interface CronJobScheduler { | ||
|
||
void schedule (String cronExpression, CronJob job); | ||
} |
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,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.github.thmarx.cms</groupId> | ||
<artifactId>cms-parent</artifactId> | ||
<version>5.2.0</version> | ||
</parent> | ||
<artifactId>cms-core</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.thmarx.cms</groupId> | ||
<artifactId>cms-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.quartz-scheduler</groupId> | ||
<artifactId>quartz</artifactId> | ||
<version>2.3.2</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP-java7</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>com.mchange</groupId> | ||
<artifactId>c3p0</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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
89 changes: 89 additions & 0 deletions
89
cms-core/src/main/java/com/github/thmarx/cms/core/scheduler/CMSCronJobScheduler.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,89 @@ | ||
package com.github.thmarx.cms.core.scheduler; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import com.github.thmarx.cms.api.scheduler.CronJob; | ||
import com.github.thmarx.cms.api.scheduler.CronJobScheduler; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.quartz.CronScheduleBuilder; | ||
import org.quartz.CronTrigger; | ||
import org.quartz.JobBuilder; | ||
import org.quartz.JobDataMap; | ||
import org.quartz.JobDetail; | ||
import org.quartz.Scheduler; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.SchedulerFactory; | ||
import org.quartz.TriggerBuilder; | ||
import org.quartz.impl.StdSchedulerFactory; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
@Slf4j | ||
public class CMSCronJobScheduler implements CronJobScheduler, AutoCloseable { | ||
|
||
Scheduler scheduler; | ||
|
||
@Override | ||
public void schedule(String cronExpression, CronJob job) { | ||
JobDataMap data = new JobDataMap(); | ||
data.put("cronJob", job); | ||
JobDetail jobDetail = JobBuilder | ||
.newJob(CronJobRunner.class) | ||
.usingJobData(data) | ||
.build(); | ||
|
||
CronTrigger trigger = TriggerBuilder.newTrigger() | ||
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) | ||
.startNow() | ||
.forJob(jobDetail) | ||
.build(); | ||
|
||
try { | ||
scheduler.scheduleJob(jobDetail, trigger); | ||
} catch (SchedulerException ex) { | ||
log.error(null, ex); | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
public void open () { | ||
try { | ||
SchedulerFactory schedulerFactory = new StdSchedulerFactory(); | ||
scheduler = schedulerFactory.getScheduler(); | ||
scheduler.start(); | ||
} catch (SchedulerException ex) { | ||
log.error(null, ex); | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
if (scheduler != null) { | ||
scheduler.shutdown(); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
cms-core/src/main/java/com/github/thmarx/cms/core/scheduler/CronJobRunner.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,49 @@ | ||
package com.github.thmarx.cms.core.scheduler; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
|
||
import com.github.thmarx.cms.api.scheduler.CronJob; | ||
import com.github.thmarx.cms.api.scheduler.CronJobContext; | ||
import org.quartz.Job; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class CronJobRunner implements Job { | ||
|
||
public static final String DATA_CRONJOB = "cronjob"; | ||
public static final String DATA_CONTEXT = "context"; | ||
|
||
@Override | ||
public void execute(JobExecutionContext context) throws JobExecutionException { | ||
if (context.get(DATA_CRONJOB) != null) { | ||
CronJobContext jobContext = (CronJobContext) context.get(DATA_CONTEXT); | ||
((CronJob)context.get(DATA_CRONJOB)).accept(null); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.