Modern, JVM-based framework for building modular, easily testable microservice and serverless applications with compile-time DI and fast startup.
—
Micronaut provides comprehensive scheduling and asynchronous execution capabilities with configurable thread pools, scheduled tasks, and async method execution.
Execute methods on a schedule with flexible timing configurations.
/**
* Scheduled task execution
*/
@Singleton
public class ScheduledService {
@Scheduled(fixedDelay = "5s")
void periodicTask() {
// Executes every 5 seconds after previous completion
}
@Scheduled(fixedRate = "10s")
void fixedRateTask() {
// Executes every 10 seconds regardless of previous completion
}
@Scheduled(cron = "0 0 9 * * MON-FRI")
void cronTask() {
// Executes at 9 AM Monday through Friday
}
@Scheduled(initialDelay = "1m", fixedDelay = "30s")
void delayedTask() {
// First execution after 1 minute, then every 30 seconds
}
}Execute methods asynchronously with various return types.
/**
* Asynchronous method execution
*/
@Singleton
public class AsyncService {
@Async
CompletableFuture<String> asyncOperation() {
return CompletableFuture.completedFuture("result");
}
@Async
Single<String> reactiveAsync() {
return Single.fromCallable(() -> "reactive result");
}
@Async
void fireAndForget() {
// Asynchronous execution without return value
}
@Async("custom-pool")
CompletableFuture<String> customPoolOperation() {
// Execute on custom thread pool
}
}Configure custom thread pools for different execution contexts.
/**
* Custom executor configuration
*/
@Singleton
@Named("custom-pool")
public class CustomExecutorConfiguration {
@Bean
@Named("custom-pool")
ExecutorService customExecutor() {
return Executors.newFixedThreadPool(10);
}
}
/**
* Executor service interface
*/
public interface ExecutorService extends Executor {
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
}Programmatic task scheduling with the TaskScheduler.
/**
* Programmatic task scheduling
*/
@Singleton
public class TaskService {
private final TaskScheduler taskScheduler;
public TaskService(TaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
}
public ScheduledFuture<?> scheduleTask(Runnable task, Duration delay) {
return taskScheduler.schedule(task, delay);
}
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task,
Duration initialDelay,
Duration period) {
return taskScheduler.scheduleAtFixedRate(task, initialDelay, period);
}
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task,
Duration initialDelay,
Duration delay) {
return taskScheduler.scheduleWithFixedDelay(task, initialDelay, delay);
}
}Control task execution with conditions and configuration.
/**
* Conditional scheduled tasks
*/
@Singleton
@Requires(property = "myapp.tasks.enabled", value = "true")
public class ConditionalTasks {
@Scheduled(fixedDelay = "1m")
@Requires(property = "myapp.cleanup.enabled", value = "true", defaultValue = "false")
void cleanupTask() {
// Only runs if both conditions are met
}
}// Scheduling annotations
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Scheduled {
String cron() default "";
String fixedDelay() default "";
String fixedRate() default "";
String initialDelay() default "";
String zone() default "";
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Async {
String value() default "";
}
// Core scheduling interfaces
public interface TaskScheduler {
ScheduledFuture<?> schedule(Runnable task, Instant startTime);
ScheduledFuture<?> schedule(Runnable task, Duration delay);
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration initialDelay, Duration period);
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration initialDelay, Duration delay);
}
public interface ScheduledFuture<V> extends Future<V> {
long getDelay(TimeUnit unit);
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
}Install with Tessl CLI
npx tessl i tessl/maven-micronaut