PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   25-09-2021

SCHEDULING

! Initialize a spring new project. Add awaitility library to Maven pom.xml.
 
spring init scheduledtask
 
<dependency>
  <groupId>org.awaitility</groupId>
  <artifactId>awaitility</artifactId>
  <version>3.1.2</version>
  <scope>test</scope>
</dependency>
Enable scheduling in you App with @EnableScheduling annotation. Create you component class to manage scheduled tasks.
 
package com.example.scheduletask;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling // Look Here
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}
... 5 lines
 
 
package com.example.scheduletask;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component // Look Here
public class ScheduledTasks {
    
    private static final SimpleDateFormat dateFormat = 
        new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 1000) // 1 second
    public void runningTime() {
        System.out.println(
            "Time is: " + dateFormat.format(new Date())
        );
    }
}
... 10 lines
 
Compile and run you application.
 
./mvnw spring-boot:run

    # Time is: 17:36:24
    # Time is: 17:36:25
    # Time is: 17:36:26

Questions    
Maven

        A B C D E F
🔔
1/1