Skip to content

Commit

Permalink
#10 extract Quartz Scheduler from git module to global config
Browse files Browse the repository at this point in the history
performance improvent by replacing file.getConanicalPath
  • Loading branch information
Thorsten Marx committed Aug 20, 2024
1 parent d71b21b commit f7b8a86
Show file tree
Hide file tree
Showing 32 changed files with 598 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public ReadOnlyFile toAbsolutePath() {

@Override
public String getCanonicalPath() throws IOException {
return file.toFile().getCanonicalPath();
return file.toAbsolutePath().normalize().toString();
}

@Override
Expand Down
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> {

}
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 {

}
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);
}
21 changes: 10 additions & 11 deletions cms-api/src/main/java/com/github/thmarx/cms/api/utils/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@
@Slf4j
public class PathUtil {

public static String canonicalPath(final Path file) {
return file.toAbsolutePath().normalize().toString();
}

public static boolean isContentFile(final Path file) {
return file.toString().endsWith(".md");
}

public static boolean isChild(Path possibleParent, Path maybeChild) {
try {
if (maybeChild == null || possibleParent == null) {
return false;
}
return maybeChild.toFile().getCanonicalPath().startsWith(possibleParent.toFile().getCanonicalPath());
} catch (IOException ex) {
log.error("", ex);
if (maybeChild == null || possibleParent == null) {
return false;
}
return false;
return canonicalPath(maybeChild).startsWith(canonicalPath(possibleParent));
}

public static String toRelativePath(final Path contentPath, final Path contentBase) {
Expand All @@ -60,7 +59,7 @@ public static String toRelativePath(final Path contentPath, final Path contentBa
uri = uri.replaceAll("\\\\", "/");
return uri;
}

public static String toRelativePath(final ReadOnlyFile contentPath, final ReadOnlyFile contentBase) {
ReadOnlyFile tempPath = contentPath;
if (!contentPath.isDirectory()) {
Expand Down Expand Up @@ -91,7 +90,7 @@ public static String toRelativeFile(ReadOnlyFile contentFile, final ReadOnlyFile
uri = uri.replaceAll("\\\\", "/");
return uri;
}

public static String toURI(final Path contentFile, final Path contentBase) {
var relFile = toRelativeFile(contentFile, contentBase);
if (relFile.endsWith("index.md")) {
Expand All @@ -113,7 +112,7 @@ public static String toURI(final Path contentFile, final Path contentBase) {

return relFile;
}

public static String toURI(final ReadOnlyFile contentFile, final ReadOnlyFile contentBase) {
var relFile = toRelativeFile(contentFile, contentBase);
if (relFile.endsWith("index.md")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import java.io.IOException;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
Expand All @@ -32,7 +33,13 @@
*/
public class PathUtilTest {


@Test
public void test_canonical () throws IOException {
String file = "pom.xml";

System.out.println("canonical: " + Path.of(file).toFile().getCanonicalPath());
System.out.println("alternative: " + Path.of(file).toAbsolutePath().normalize().toString());
}

@Test
public void test_to_uri() {
Expand Down
38 changes: 38 additions & 0 deletions cms-core/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.thmarx.cms.eventbus;
package com.github.thmarx.cms.core.eventbus;

/*-
* #%L
Expand Down Expand Up @@ -26,7 +26,6 @@
import com.github.thmarx.cms.api.eventbus.Event;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down
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();
}
}

}
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);
}
}

}
Loading

0 comments on commit f7b8a86

Please sign in to comment.