or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

aop.mdconfiguration.mddependency-injection.mdfunctions.mdhttp-client.mdhttp-server.mdindex.mdmanagement.mdmessaging.mdreactive.mdretry.mdscheduling.mdwebsocket.md

scheduling.mddocs/

0

# Scheduling and Async

1

2

Micronaut provides comprehensive scheduling and asynchronous execution capabilities with configurable thread pools, scheduled tasks, and async method execution.

3

4

## Capabilities

5

6

### Scheduled Tasks

7

8

Execute methods on a schedule with flexible timing configurations.

9

10

```java { .api }

11

/**

12

* Scheduled task execution

13

*/

14

@Singleton

15

public class ScheduledService {

16

17

@Scheduled(fixedDelay = "5s")

18

void periodicTask() {

19

// Executes every 5 seconds after previous completion

20

}

21

22

@Scheduled(fixedRate = "10s")

23

void fixedRateTask() {

24

// Executes every 10 seconds regardless of previous completion

25

}

26

27

@Scheduled(cron = "0 0 9 * * MON-FRI")

28

void cronTask() {

29

// Executes at 9 AM Monday through Friday

30

}

31

32

@Scheduled(initialDelay = "1m", fixedDelay = "30s")

33

void delayedTask() {

34

// First execution after 1 minute, then every 30 seconds

35

}

36

}

37

```

38

39

### Asynchronous Execution

40

41

Execute methods asynchronously with various return types.

42

43

```java { .api }

44

/**

45

* Asynchronous method execution

46

*/

47

@Singleton

48

public class AsyncService {

49

50

@Async

51

CompletableFuture<String> asyncOperation() {

52

return CompletableFuture.completedFuture("result");

53

}

54

55

@Async

56

Single<String> reactiveAsync() {

57

return Single.fromCallable(() -> "reactive result");

58

}

59

60

@Async

61

void fireAndForget() {

62

// Asynchronous execution without return value

63

}

64

65

@Async("custom-pool")

66

CompletableFuture<String> customPoolOperation() {

67

// Execute on custom thread pool

68

}

69

}

70

```

71

72

### Thread Pool Configuration

73

74

Configure custom thread pools for different execution contexts.

75

76

```java { .api }

77

/**

78

* Custom executor configuration

79

*/

80

@Singleton

81

@Named("custom-pool")

82

public class CustomExecutorConfiguration {

83

84

@Bean

85

@Named("custom-pool")

86

ExecutorService customExecutor() {

87

return Executors.newFixedThreadPool(10);

88

}

89

}

90

91

/**

92

* Executor service interface

93

*/

94

public interface ExecutorService extends Executor {

95

<T> Future<T> submit(Callable<T> task);

96

<T> Future<T> submit(Runnable task, T result);

97

Future<?> submit(Runnable task);

98

}

99

```

100

101

### Task Scheduling Service

102

103

Programmatic task scheduling with the TaskScheduler.

104

105

```java { .api }

106

/**

107

* Programmatic task scheduling

108

*/

109

@Singleton

110

public class TaskService {

111

112

private final TaskScheduler taskScheduler;

113

114

public TaskService(TaskScheduler taskScheduler) {

115

this.taskScheduler = taskScheduler;

116

}

117

118

public ScheduledFuture<?> scheduleTask(Runnable task, Duration delay) {

119

return taskScheduler.schedule(task, delay);

120

}

121

122

public ScheduledFuture<?> scheduleAtFixedRate(Runnable task,

123

Duration initialDelay,

124

Duration period) {

125

return taskScheduler.scheduleAtFixedRate(task, initialDelay, period);

126

}

127

128

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task,

129

Duration initialDelay,

130

Duration delay) {

131

return taskScheduler.scheduleWithFixedDelay(task, initialDelay, delay);

132

}

133

}

134

```

135

136

### Conditional Scheduling

137

138

Control task execution with conditions and configuration.

139

140

```java { .api }

141

/**

142

* Conditional scheduled tasks

143

*/

144

@Singleton

145

@Requires(property = "myapp.tasks.enabled", value = "true")

146

public class ConditionalTasks {

147

148

@Scheduled(fixedDelay = "1m")

149

@Requires(property = "myapp.cleanup.enabled", value = "true", defaultValue = "false")

150

void cleanupTask() {

151

// Only runs if both conditions are met

152

}

153

}

154

```

155

156

## Types

157

158

```java { .api }

159

// Scheduling annotations

160

@Target({ElementType.METHOD})

161

@Retention(RetentionPolicy.RUNTIME)

162

public @interface Scheduled {

163

String cron() default "";

164

String fixedDelay() default "";

165

String fixedRate() default "";

166

String initialDelay() default "";

167

String zone() default "";

168

}

169

170

@Target({ElementType.METHOD})

171

@Retention(RetentionPolicy.RUNTIME)

172

public @interface Async {

173

String value() default "";

174

}

175

176

// Core scheduling interfaces

177

public interface TaskScheduler {

178

ScheduledFuture<?> schedule(Runnable task, Instant startTime);

179

ScheduledFuture<?> schedule(Runnable task, Duration delay);

180

ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration initialDelay, Duration period);

181

ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration initialDelay, Duration delay);

182

}

183

184

public interface ScheduledFuture<V> extends Future<V> {

185

long getDelay(TimeUnit unit);

186

boolean cancel(boolean mayInterruptIfRunning);

187

boolean isCancelled();

188

boolean isDone();

189

}

190

```