Package org.nanonative.nano.core
Class NanoThreads<T extends NanoThreads<T>>
java.lang.Object
org.nanonative.nano.core.NanoBase<T>
org.nanonative.nano.core.NanoThreads<T>
- Type Parameters:
T- The payload of theNanoThreadsimplementation, used for method chaining.
- Direct Known Subclasses:
NanoServices
The abstract base class for
Nano framework providing thread handling functionalities.-
Field Summary
FieldsFields inherited from class org.nanonative.nano.core.NanoBase
CONFIG_KEYS, context, createdAtNs, EVENT_CHANNELS, EVENT_ID_COUNTER, eventCount, isReady, listeners, logService -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNanoThreads(Map<Object, Object> config, String... args) InitializesNanoThreadswith configurations and command-line arguments. -
Method Summary
Modifier and TypeMethodDescriptionprotected SchedulerasyncFromPool(Supplier<Context> context) Creates aSchedulerfrom the thread pool.protected voidexecuteScheduler(Supplier<Context> context, ExRunnable task, Scheduler scheduler, boolean periodically) Executes the task.protected static ZonedDateTimeinitialPlanned(LocalTime atTime, DayOfWeek dow, ZoneId zone) Calculates the first execution time after now for the given wall-clock time and day of week.protected voidkill(ExecutorService executorService, long timeoutMs) Forces shutdown of anExecutorServiceif it doesn't terminate within the specified timeout.protected static ZonedDateTimenextPlanned(ZonedDateTime prevPlanned, LocalTime atTime, DayOfWeek dow, ZoneId zone) Calculates the next execution time after a previous planned run.protected voidremoveScheduler(ExecutorService executorService) Removes a scheduler from the set of managed schedulers.protected static ZonedDateTimeresolveWallTime(LocalDate date, LocalTime time, ZoneId zone) DST-safe wall time: pushes through gaps, picks the earlier offset in overlaps.run(Supplier<Context> context, ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until) Executes a task periodically, starting after an initial delay.run(Supplier<Context> context, ExRunnable task, long delay, TimeUnit timeUnit) Executes a task asynchronously after a specified delay.run(Supplier<Context> context, ExRunnable task, LocalTime atTime, DayOfWeek dow, ZoneId zone, BooleanSupplier until) Core scheduling method for daily or weekly execution at a fixed wall-clock time.run(Supplier<Context> context, ExRunnable task, LocalTime atTime, DayOfWeek dow, BooleanSupplier until) Executes a task periodically, starting after an initial delay.runDaily(Supplier<Context> context, ExRunnable task, LocalTime atTime, BooleanSupplier until) Executes a task daily at the specified wall-clock time in the server's default time zone until the stop condition returnstrue.runWeekly(Supplier<Context> context, ExRunnable task, DayOfWeek dow, LocalTime atTime, BooleanSupplier until) Executes a task weekly at the given day of week and wall-clock time in the server's default time zone until the stop condition returnstrue.protected voidscheduleOnce(Supplier<Context> context, ExRunnable task, Scheduler scheduler, BooleanSupplier until, LocalTime atTime, DayOfWeek dow, ZoneId zone, ZonedDateTime planned) Internal recursive scheduler: schedules the given task once, then reschedules itself for the next occurrence.Provides an unmodifiable set ofScheduledExecutorService.protected voidshutdownExecutors(long timeoutMs, ExecutorService... executorServices) Shuts down executors and handles timeout for forced termination.protected voidShuts down all threads and scheduled executors gracefully.Methods inherited from class org.nanonative.nano.core.NanoBase
createdAtMs, displayHelpMenu, eventCount, heapMemoryUsage, isReady, listeners, pid, readConfigs, standardiseKey, stop, stop, subscribeError, subscribeError, subscribeEvent, subscribeEvent, unsubscribeEvent, usedMemoryMB
-
Field Details
-
schedulers
-
-
Constructor Details
-
NanoThreads
InitializesNanoThreadswith configurations and command-line arguments.- Parameters:
config- Configuration parameters for theNanoThreadsinstance.args- Command-line arguments passed during the application start.
-
-
Method Details
-
run
public T run(Supplier<Context> context, ExRunnable task, LocalTime atTime, DayOfWeek dow, BooleanSupplier until) Executes a task periodically, starting after an initial delay.nano.run(() -> myMethod(), LocalTime.of(7, 0, 0))- Parameters:
task- The task to execute.atTime- The time of hour/minute/second to start the task.dow- The day of the week to start the task.until- A BooleanSupplier indicating the termination condition.truestops the next execution.- Returns:
- Self for chaining
-
runDaily
public T runDaily(Supplier<Context> context, ExRunnable task, LocalTime atTime, BooleanSupplier until) Executes a task daily at the specified wall-clock time in the server's default time zone until the stop condition returnstrue.- Parameters:
context- the execution context suppliertask- the task to executeatTime- the daily wall-clock time (hour, minute, second)until- stop condition; whentrue, cancels further runs- Returns:
- self for chaining
-
runWeekly
public T runWeekly(Supplier<Context> context, ExRunnable task, DayOfWeek dow, LocalTime atTime, BooleanSupplier until) Executes a task weekly at the given day of week and wall-clock time in the server's default time zone until the stop condition returnstrue.- Parameters:
context- the execution context suppliertask- the task to executedow- the day of the weekatTime- the weekly wall-clock time (hour, minute, second)until- stop condition; whentrue, cancels further runs- Returns:
- self for chaining
-
run
public T run(Supplier<Context> context, ExRunnable task, LocalTime atTime, DayOfWeek dow, ZoneId zone, BooleanSupplier until) Core scheduling method for daily or weekly execution at a fixed wall-clock time. UsesZonedDateTimein the given zone to account for daylight saving changes.- Parameters:
context- the execution context suppliertask- the task to executeatTime- the wall-clock time (hour, minute, second)dow- optional day of week; ifnull, runs every dayzone- the time zone (usuallyZoneId.systemDefault())until- stop condition; whentrue, cancels further runs- Returns:
- self for chaining
-
scheduleOnce
protected void scheduleOnce(Supplier<Context> context, ExRunnable task, Scheduler scheduler, BooleanSupplier until, LocalTime atTime, DayOfWeek dow, ZoneId zone, ZonedDateTime planned) Internal recursive scheduler: schedules the given task once, then reschedules itself for the next occurrence.- Parameters:
context- the execution context suppliertask- the task to executescheduler- the scheduler executoruntil- stop condition; whentrue, cancels further runsatTime- the wall-clock time (hour, minute, second)dow- optional day of week; ifnull, runs every dayzone- the time zoneplanned- the planned execution time
-
initialPlanned
Calculates the first execution time after now for the given wall-clock time and day of week.- Parameters:
atTime- the wall-clock timedow- optional day of week; ifnull, runs dailyzone- the time zone- Returns:
- the first valid planned execution time
-
nextPlanned
protected static ZonedDateTime nextPlanned(ZonedDateTime prevPlanned, LocalTime atTime, DayOfWeek dow, ZoneId zone) Calculates the next execution time after a previous planned run.- Parameters:
prevPlanned- the previous planned run timeatTime- the wall-clock timedow- optional day of week; ifnull, runs dailyzone- the time zone- Returns:
- the next planned execution time
-
resolveWallTime
DST-safe wall time: pushes through gaps, picks the earlier offset in overlaps. -
schedulers
Provides an unmodifiable set ofScheduledExecutorService.- Returns:
- An unmodifiable set of
ScheduledExecutorServiceinstances.
-
run
Executes a task asynchronously after a specified delay.- Parameters:
task- The task to execute.delay- The delay before executing the task.timeUnit- The time unit of the delay parameter.- Returns:
- Self for chaining
-
run
public T run(Supplier<Context> context, ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until) Executes a task periodically, starting after an initial delay.- Parameters:
task- The task to execute.delay- The initial delay before executing the task.period- The period between successive task executions.unit- The time unit of the initialDelay and period parameters.until- A BooleanSupplier indicating the termination condition.truestops the next execution.- Returns:
- Self for chaining
-
asyncFromPool
Creates aSchedulerfrom the thread pool.- Returns:
- The newly created
Scheduler.
-
shutdownThreads
protected void shutdownThreads()Shuts down all threads and scheduled executors gracefully. -
shutdownExecutors
Shuts down executors and handles timeout for forced termination.- Parameters:
timeoutMs- The maximum time to wait for executor termination.executorServices- An array of ExecutorService instances to shut down.
-
removeScheduler
Removes a scheduler from the set of managed schedulers.- Parameters:
executorService- TheExecutorServiceto remove from the scheduler set.
-
kill
Forces shutdown of anExecutorServiceif it doesn't terminate within the specified timeout.- Parameters:
executorService- The executor service to shut down.timeoutMs- The maximum time to wait for termination.- Throws:
InterruptedException- if interrupted while waiting.
-
executeScheduler
protected void executeScheduler(Supplier<Context> context, ExRunnable task, Scheduler scheduler, boolean periodically) Executes the task.- Parameters:
context- the contexttask- the taskscheduler- the schedulerperiodically- the periodically
-