Skip to content
LK

Lejla Kasum

1 article

June 8, 2022

Schedulers in Java

Software Development

Schedulers in Java

Scheduler is a mechanism that supports executing of tasks periodically or at certain periods of time, without need to run tasks manually. For example, sometimes in an application we need to run some analysis every one hour, or we need to send an email notification every Monday at 08:00 AM, but we don’t want to be responsible for running these tasks each time. Using a scheduler would help implement these kinds of requirements. In this Tech Bite will be illustrated Java Spring Boot support for scheduling tasks that is simple to implement even in the smallest applications. 1. Enable support for scheduling Scheduling support will be simply enabled by adding annotation @EnableScheduling to the main class: @SpringBootApplication @EnableScheduling public class SchedulerApplication { public static void main(String[] args) { SpringApplication.run(SchedulerApplication.class, args); } } 2. Add scheduler service Scheduler service class will hold scheduled methods. These methods will perform tasks that need to be executed in a certain period of time or periodically and have to be annotated with @Scheduled In this case, method printCurrentTime() will print current time in the console each time that task is executed. @Component public class SchedulerService { @Scheduled public void printCurrentTime() { LocalTime currentTime = LocalDateTime.now().toLocalTime(); System.out.println("Current time is: " + currentTime); } } 3. Schedule task Final step is to properly set attributes of @Scheduledannotation, depending on when we want a task to run. If we want task to run every second we set: @Scheduled(fixedRate = 1000) This annotation also supports cron expressions, so if we want to schedule task every Monday at 08:00 AM we set: @Scheduled(cron = “0 0 8 ? * MON *”) More about each attribute can be found in official documentation. 4. Read attributes from configuration file (optional) When we want to change configuration for a scheduler, usually we don’t want to redeploy the entire application. So, good practice for values that are configuring schedulers would be to put them in the application properties file. In that case we can change configuration easily. @Scheduled(fixedRateString = "${project.scheduler.fixedRate}") or @Scheduled(cron = "${project.scheduler.cronExpression}") "Schedulers in Java" Tech Bite was brought to you by Lejla Kasum, Software Engineer at Atlantbh. (more…)

Ready to Achieve More?

We’ll help you reach your goals quickly with an easy and straightforward process to kick off our collaboration. Here’s what happens next.

STEP 1

Discovery Call

Let’s chat to understand your company, project needs, and answer any questions along the way.

STEP 2

Free Consultation

Work closely with our experts to explore the right solutions for your business.

STEP 3

Collaboration Proposal

We'll recommend the best strategy for your goals, ensuring you get the most from our expertise.

STEP 4

30-Day Cancellation
Policy Contract

Spoiler: It’s Never Been Used

Enjoy peace of mind while we deliver excellence from day one—our track record speaks for itself.

Services you're interested in (Optional)