From 0782f40d6dd64d05cc8d87da71aa2ca2cc411df7 Mon Sep 17 00:00:00 2001 From: cseewald Date: Wed, 13 Sep 2023 16:29:21 +0200 Subject: [PATCH] Improve Examples * Remove ExampleHelpers.java: method sleep was unused, method registerShutdownHook is replaced by registerShutdownHook in SchedulerBuilder * Add missing main method in RecurringTaskMain.java --- .../CheckForNewBatchDirectlyMain.java | 6 +-- .../github/kagkarlsson/examples/CronMain.java | 4 +- .../examples/DeletingUnresolvedTasksMain.java | 4 +- .../EnableImmediateExecutionMain.java | 6 +-- .../examples/ExponentialBackoffMain.java | 8 ++-- .../ExponentialBackoffWithMaxRetriesMain.java | 8 ++-- .../kagkarlsson/examples/MaxRetriesMain.java | 8 ++-- .../examples/RecurringTaskMain.java | 4 ++ .../kagkarlsson/examples/SchedulerMain.java | 6 +-- .../examples/helpers/ExampleHelpers.java | 39 ------------------- 10 files changed, 21 insertions(+), 72 deletions(-) delete mode 100644 examples/features/src/main/java/com/github/kagkarlsson/examples/helpers/ExampleHelpers.java diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/CheckForNewBatchDirectlyMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/CheckForNewBatchDirectlyMain.java index ea942826..8ec3f0ae 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/CheckForNewBatchDirectlyMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/CheckForNewBatchDirectlyMain.java @@ -13,10 +13,7 @@ */ package com.github.kagkarlsson.examples; -import static com.github.kagkarlsson.examples.helpers.ExampleHelpers.sleep; - import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask; import com.github.kagkarlsson.scheduler.task.helper.Tasks; @@ -43,10 +40,9 @@ public void run(DataSource dataSource) { Scheduler.create(dataSource, onetimeTask) .pollingInterval(Duration.ofSeconds(10)) .threads(2) + .registerShutdownHook() .build(); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); sleep(2); diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/CronMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/CronMain.java index 710ae732..5cc140f4 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/CronMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/CronMain.java @@ -14,7 +14,6 @@ package com.github.kagkarlsson.examples; import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.helper.RecurringTask; import com.github.kagkarlsson.scheduler.task.helper.Tasks; @@ -45,10 +44,9 @@ public void run(DataSource dataSource) { Scheduler.create(dataSource) .startTasks(cronTask) .pollingInterval(Duration.ofSeconds(1)) + .registerShutdownHook() .build(); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); } } diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/DeletingUnresolvedTasksMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/DeletingUnresolvedTasksMain.java index 68617f17..cfd68f20 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/DeletingUnresolvedTasksMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/DeletingUnresolvedTasksMain.java @@ -14,7 +14,6 @@ package com.github.kagkarlsson.examples; import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.SchedulerClient; import com.github.kagkarlsson.scheduler.task.helper.RecurringTask; @@ -56,10 +55,9 @@ public void run(DataSource dataSource) { .pollingInterval(Duration.ofSeconds(1)) .heartbeatInterval(Duration.ofSeconds(5)) .deleteUnresolvedAfter(Duration.ofSeconds(20)) + .registerShutdownHook() .build(); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); IntStream.range(0, 5) diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/EnableImmediateExecutionMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/EnableImmediateExecutionMain.java index 76d96cd6..dd61b5bd 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/EnableImmediateExecutionMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/EnableImmediateExecutionMain.java @@ -13,10 +13,7 @@ */ package com.github.kagkarlsson.examples; -import static com.github.kagkarlsson.examples.helpers.ExampleHelpers.sleep; - import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask; import com.github.kagkarlsson.scheduler.task.helper.Tasks; @@ -44,10 +41,9 @@ public void run(DataSource dataSource) { Scheduler.create(dataSource, onetimeTask) .pollingInterval(Duration.ofSeconds(20)) .enableImmediateExecution() + .registerShutdownHook() .build(); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); sleep(2000); diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffMain.java index 067bae74..2127a0ae 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffMain.java @@ -16,7 +16,6 @@ import static java.time.Duration.*; import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.FailureHandler; import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask; @@ -41,12 +40,13 @@ public void run(DataSource dataSource) { }); final Scheduler scheduler = - Scheduler.create(dataSource, failingTask).pollingInterval(ofSeconds(2)).build(); + Scheduler.create(dataSource, failingTask) + .pollingInterval(ofSeconds(2)) + .registerShutdownHook() + .build(); scheduler.schedule(failingTask.instance("1"), Instant.now()); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); } } diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffWithMaxRetriesMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffWithMaxRetriesMain.java index 518ca7e8..5d31b3a7 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffWithMaxRetriesMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/ExponentialBackoffWithMaxRetriesMain.java @@ -16,7 +16,6 @@ import static java.time.Duration.ofSeconds; import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.FailureHandler; import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask; @@ -43,12 +42,13 @@ public void run(DataSource dataSource) { }); final Scheduler scheduler = - Scheduler.create(dataSource, failingTask).pollingInterval(ofSeconds(2)).build(); + Scheduler.create(dataSource, failingTask) + .pollingInterval(ofSeconds(2)) + .registerShutdownHook() + .build(); scheduler.schedule(failingTask.instance("1"), Instant.now()); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); } } diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/MaxRetriesMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/MaxRetriesMain.java index 332ba345..549d68f6 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/MaxRetriesMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/MaxRetriesMain.java @@ -14,7 +14,6 @@ package com.github.kagkarlsson.examples; import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.FailureHandler; import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask; @@ -52,12 +51,13 @@ public void run(DataSource dataSource) { }); final Scheduler scheduler = - Scheduler.create(dataSource, failingTask).pollingInterval(Duration.ofSeconds(2)).build(); + Scheduler.create(dataSource, failingTask) + .pollingInterval(Duration.ofSeconds(2)) + .registerShutdownHook() + .build(); scheduler.schedule(failingTask.instance("1"), Instant.now()); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); } } diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/RecurringTaskMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/RecurringTaskMain.java index 66f5d972..6b2dce78 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/RecurringTaskMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/RecurringTaskMain.java @@ -26,6 +26,10 @@ public class RecurringTaskMain extends Example { private static final Logger LOG = LoggerFactory.getLogger(RecurringTaskMain.class); + public static void main(String[] args) { + new RecurringTaskMain().runWithDatasource(); + } + @Override public void run(DataSource dataSource) { diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/SchedulerMain.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/SchedulerMain.java index 219ee993..ca1cde17 100644 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/SchedulerMain.java +++ b/examples/features/src/main/java/com/github/kagkarlsson/examples/SchedulerMain.java @@ -13,10 +13,7 @@ */ package com.github.kagkarlsson.examples; -import static com.github.kagkarlsson.examples.helpers.ExampleHelpers.sleep; - import com.github.kagkarlsson.examples.helpers.Example; -import com.github.kagkarlsson.examples.helpers.ExampleHelpers; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.ExecutionContext; import com.github.kagkarlsson.scheduler.task.TaskInstance; @@ -118,10 +115,9 @@ public void run(DataSource dataSource) { final Scheduler scheduler = Scheduler.create(dataSource, onetime1, onetime2) .startTasks(recurring1, recurring2, custom1) + .registerShutdownHook() .build(); - ExampleHelpers.registerShutdownHook(scheduler); - scheduler.start(); sleep(3000); diff --git a/examples/features/src/main/java/com/github/kagkarlsson/examples/helpers/ExampleHelpers.java b/examples/features/src/main/java/com/github/kagkarlsson/examples/helpers/ExampleHelpers.java deleted file mode 100644 index c1c7515a..00000000 --- a/examples/features/src/main/java/com/github/kagkarlsson/examples/helpers/ExampleHelpers.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) Gustav Karlsson - * - *

Licensed 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 com.github.kagkarlsson.examples.helpers; - -import static org.slf4j.LoggerFactory.getLogger; - -import com.github.kagkarlsson.scheduler.Scheduler; - -public class ExampleHelpers { - - public static void sleep(int seconds) { - try { - Thread.sleep(seconds * 1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - public static void registerShutdownHook(Scheduler scheduler) { - Runtime.getRuntime() - .addShutdownHook( - new Thread( - () -> { - getLogger(ExampleHelpers.class).info("Received shutdown signal."); - scheduler.stop(); - })); - } -}