0
0
Spring Bootframework~10 mins

Scheduled tasks with @Scheduled in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to schedule a method to run every 5 seconds.

Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskScheduler {

    @Scheduled(fixedRate = [1])
    public void runTask() {
        System.out.println("Task executed");
    }
}
Drag options to blanks, or click blank then click option'
A"5s"
B5
C5000
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Using seconds instead of milliseconds
Using string values instead of numbers
2fill in blank
medium

Complete the code to schedule a method to run every day at 2:30 AM.

Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class DailyTask {

    @Scheduled(cron = [1])
    public void runDaily() {
        System.out.println("Daily task executed");
    }
}
Drag options to blanks, or click blank then click option'
A"0 30 2 * * ?"
B"0 2 30 * * ?"
C"30 2 * * *"
D"0 0 2 * * ?"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up minute and hour positions
Omitting seconds field
3fill in blank
hard

Fix the error in the code to enable scheduling in the Spring Boot application.

Spring Boot
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
[1]
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
Drag options to blanks, or click blank then click option'
A@EnableScheduling
B@Scheduled
C@Component
D@Configuration
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Scheduled on the main class
Forgetting to enable scheduling
4fill in blank
hard

Fill both blanks to create a scheduled method that runs every minute and logs the current time.

Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;

@Component
public class ClockTask {

    @Scheduled(cron = [1])
    public void logTime() {
        System.out.println("Current time: " + LocalTime.[2]());
    }
}
Drag options to blanks, or click blank then click option'
A"0 * * * * ?"
B"fixedRate = 60000"
Cnow
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixedRate with wrong value
Using incorrect LocalTime method
5fill in blank
hard

Fill all three blanks to create a scheduled task that runs every 10 seconds, prints a message, and uses a component annotation.

Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.[1].Component;

@[2]
public class MessageTask {

    @Scheduled(fixedRate = [3])
    public void printMessage() {
        System.out.println("Hello every 10 seconds");
    }
}
Drag options to blanks, or click blank then click option'
Astereotype
BComponent
C10000
DConfiguration
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong import package
Using @Configuration instead of @Component
Wrong fixedRate value