TP14.4: Scheduled Tasks
Objectives of the Lab
- Configure scheduled tasks
- Use Cron expressions
- Implement automatic reports
- Manage asynchronous tasks
Estimated duration: 45 min
Architecture
Spring Boot Application
|
+-- Scheduler Thread Pool
|
+-- Daily Report (0 0 6 * * *)
+-- Hourly Cleanup (0 0 * * * *)
+-- Price Sync (fixedRate = 60000)
+-- Health Check (fixedDelay = 30000)
Part 1: Configuration (10 min)
1.1 Application
package com.example.scheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}
1.2 Configuration
package com.example.scheduling.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
public class SchedulingConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setTaskScheduler(taskScheduler());
}
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(5);
scheduler.setThreadNamePrefix("scheduler-");
scheduler.setErrorHandler(t -> {
System.err.println("Task error: " + t.getMessage());
});
return scheduler;
}
}
1.3 application.yml
app:
scheduling:
enabled: true
cleanup:
max-age-hours: 24
report:
email: admin@example.com
Part 2: Scheduled Tasks (20 min)
2.1 Task Service
package com.example.scheduling.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class ScheduledTaskService {
private static final Logger log = LoggerFactory.getLogger(ScheduledTaskService.class);
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final AtomicInteger taskCounter = new AtomicInteger(0);
// Every 10 seconds
@Scheduled(fixedRate = 10000)
public void heartbeat() {
int count = taskCounter.incrementAndGet();
log.info("[Heartbeat #{}] Application is running at {}",
count, LocalDateTime.now().format(formatter));
}
// Every 30 seconds after the previous one ends
@Scheduled(fixedDelay = 30000)
public void cleanupTask() {
log.info("[Cleanup] Starting cleanup task...");
// Simulate cleanup
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
log.info("[Cleanup] Cleanup completed - {} items removed",
(int)(Math.random() * 100));
}
// Every minute (at the start of the minute)
@Scheduled(cron = "0 * * * * *")
public void everyMinute() {
log.info("[Cron] Minute task executed at {}",
LocalDateTime.now().format(formatter));
}
// Every 5 minutes
@Scheduled(cron = "0 */5 * * * *")
public void everyFiveMinutes() {
log.info("[Cron] Five-minute task executed");
generateQuickReport();
}
// Every day at 6 AM
@Scheduled(cron = "0 0 6 * * *")
public void dailyReport() {
log.info("[Daily] Generating daily report...");
// Generate report
log.info("[Daily] Daily report generated and sent");
}
// Every Monday at 9 AM
@Scheduled(cron = "0 0 9 * * MON")
public void weeklyReport() {
log.info("[Weekly] Generating weekly report...");
}
private void generateQuickReport() {
log.info("Quick report: Active threads = {}, Free memory = {} MB",
Thread.activeCount(),
Runtime.getRuntime().freeMemory() / (1024 * 1024));
}
// Asynchronous method
@Async
public void asyncTask(String taskName) {
log.info("[Async] Starting async task: {} on thread {}",
taskName, Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
log.info("[Async] Completed async task: {}", taskName);
}
}
2.2 Statistics Service
package com.example.scheduling.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicLong;
@Service
public class StatisticsService {
private static final Logger log = LoggerFactory.getLogger(StatisticsService.class);
private final AtomicLong requestCount = new AtomicLong(0);
private final AtomicLong errorCount = new AtomicLong(0);
public void incrementRequests() {
requestCount.incrementAndGet();
}
public void incrementErrors() {
errorCount.incrementAndGet();
}
// Statistics report every minute
@Scheduled(cron = "0 * * * * *")
public void reportStatistics() {
long requests = requestCount.getAndSet(0);
long errors = errorCount.getAndSet(0);
log.info("[Stats] Last minute: {} requests, {} errors, Error rate: {}%",
requests,
errors,
requests > 0 ? (errors * 100 / requests) : 0);
}
// Health check every 30 seconds
@Scheduled(fixedRate = 30000)
public void healthCheck() {
Runtime runtime = Runtime.getRuntime();
long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024);
long maxMemory = runtime.maxMemory() / (1024 * 1024);
int usagePercent = (int) (usedMemory * 100 / maxMemory);
if (usagePercent > 80) {
log.warn("[Health] High memory usage: {}% ({} MB / {} MB)",
usagePercent, usedMemory, maxMemory);
} else {
log.debug("[Health] Memory usage: {}% ({} MB / {} MB)",
usagePercent, usedMemory, maxMemory);
}
}
}
Part 3: Controller for Tests (10 min)
package com.example.scheduling.controller;
import com.example.scheduling.service.ScheduledTaskService;
import com.example.scheduling.service.StatisticsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/tasks")
public class TaskController {
private final ScheduledTaskService taskService;
private final StatisticsService statisticsService;
public TaskController(ScheduledTaskService taskService,
StatisticsService statisticsService) {
this.taskService = taskService;
this.statisticsService = statisticsService;
}
@PostMapping("/async/{name}")
public ResponseEntity<Map<String, String>> triggerAsyncTask(@PathVariable String name) {
taskService.asyncTask(name);
return ResponseEntity.ok(Map.of(
"message", "Async task '" + name + "' started",
"status", "RUNNING"
));
}
@GetMapping("/simulate-traffic")
public ResponseEntity<Map<String, String>> simulateTraffic(
@RequestParam(defaultValue = "100") int requests) {
for (int i = 0; i < requests; i++) {
statisticsService.incrementRequests();
if (Math.random() < 0.05) { // 5% errors
statisticsService.incrementErrors();
}
}
return ResponseEntity.ok(Map.of(
"message", requests + " requests simulated"
));
}
}
Part 4: Tests (10 min)
4.1 Observe the Logs
Start the application and observe the logs:
[Heartbeat #1] Application is running at 2024-01-15 10:00:00
[Cron] Minute task executed at 2024-01-15 10:01:00
[Cleanup] Starting cleanup task...
[Cleanup] Cleanup completed - 45 items removed
[Stats] Last minute: 0 requests, 0 errors, Error rate: 0%
4.2 Test Async Tasks
# Trigger an async task
curl -X POST http://localhost:8080/api/tasks/async/test-task-1
# Simulate traffic
curl "http://localhost:8080/api/tasks/simulate-traffic?requests=500"
4.3 Verify Statistics
Wait 1 minute and observe the statistics report in the logs.
Cron Expressions - Reference
| Expression | Description |
|---|---|
0 * * * * * | Every minute |
0 */5 * * * * | Every 5 minutes |
0 0 * * * * | Every hour |
0 0 6 * * * | Every day at 6 AM |
0 0 9 * * MON | Every Monday at 9 AM |
0 0 0 1 * * | First day of the month |
Validation Checklist
- Scheduled tasks execute
- Logs show executions
- Async tasks work
- Statistics are calculated