Skip to main content

Make a scheduling job in Spring Boot

You can make a class to run as a schedule job in Spring Boot. By using @EnableSchedule in your project. Below is example code to enable schedule in Spring Boot.

Declare Spring Boot application to support schedule

By declare @EnableSchedule at the main class (see at line 10). This enables detection of @Scheduled annotations on any Spring-managed bean in the container.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package com.myexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication
@EnableScheduling
public class MyExampleApplication {

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

Declare schedule configuration at the method you need

Use @Scheduled to configure a schedule method in many way.
  1. Scheduling by static interval. You must use attribute fixedRate as this example. This value is milliseconds.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    package com.myco.tasks;
    
    import org.springframework.scheduling.annotation.Scheduled;
    
    public class MyTask {
    
         @Scheduled(fixedRate=1000)
         public void work() {
             // task execution logic
         }
     }
    

  2. Scheduling by fixed delay after last successful execution. This value is milliseconds. In this example work() will be delay 1 second after last successful execution.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    package com.myco.tasks;
    
    import org.springframework.scheduling.annotation.Scheduled;
    
    public class MyTask {
    
         @Scheduled(fixedDelay=1000)
         public void work() {
             // task execution logic
         }
     }
    

  3. Scheduling as a UNIX cron job. By using cron attribute.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    package com.myco.tasks;
    
    import org.springframework.scheduling.annotation.Scheduled;
    
    public class MyTask {
    
         @Scheduled(cron="0 * * * * MON-FRI")
         public void work() {
             // task execution logic
         }
     }
    

  4. Scheduling by number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    package com.myco.tasks;
    
    import org.springframework.scheduling.annotation.Scheduled;
    
    public class MyTask {
    
         @Scheduled(fixedRate=1000, initialDelay=5000)
         public void work() {
             // task execution logic
         }
     }
    

  5. Dynamic setup fixedRate or fixedDelay by using environment variables from application.properties. Spring Boot have fixedRateString, fixedDelayString and initialDelayString for you to set value of @Scheduled from environment variables. Below is example (see at line 7).

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    package com.myco.tasks;
    
    import org.springframework.scheduling.annotation.Scheduled;
    
    public class MyTask {
    
         @Scheduled(@Scheduled(fixedDelayString="${my.environment.variable}"))
         public void work() {
             // task execution logic
         }
     }
    

More Information

for more information please see Sprint Boot document here.




Comments

Popular posts from this blog

Install Spring Boot application as a Windows services.

I using  WinSW  to be wrapper for Spring Boot application to run as a Windows service (following section 61.3 of Spring Boot document). There few easy step to setup. Download WinSW binary distribution from website  https://github.com/kohsuke/winsw/releases Copy WinSW.exe into Spring Boot application folder (ex: my file is WinSW.Net4.exe) Rename your WinSW.exe to same as your jar file (for easy to remember). Create XML file name same as jar file. This file is using for configuration of Windows services. Put configuration for services wrapper in your xml file. 1 2 3 4 5 6 7 8 9 <?xml version="1.0" encoding="UTF-8"?> <service> <id>my-application-0.0.1</id> <name>my-application-0.0.1</name> <description>My Exaple Spring Boot Services</description> <executable>java</executable> <arguments>-jar -Xmx1024M -Xms128M "my-application-0.0.1.jar"</arguments> <logmode>rotate...

CURL SSL error in WAMP

I facing problem about certificate error when I using curl to request HTTPS domain. I find cause of problem is there is no certificate configuration in PHP. Below is how to solve my problem. Here is sample of error: * About to connect() to notify-api.line.me port 443 (#0) * Trying {IP Address}... * connected * Connected to notify-api.line.me ({IP Address}) port 443 (#0) * SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed * Closing connection #0 Step to solve this problem Download  Certificate Bundle Extract and put PEM file to your web server folder or other folder. Enable mod_ssl in Apache and php_openssl.dll in php.ini Add configuration into php.ini curl.cainfo="C:/wamp/cacert.pem" openssl.cafile="C:/wamp/cacert.pem" Restart Apache Service

Bootstrap 4.1.3

Hot on the heels of v4.1.2, we’re shipping another patch release to address an issue with our browserslist config, fix some CSS bugs, make JavaScript plugins UMD ready, and improve form control rendering. Up next will be v4.2, our second minor release where we add some new features. But first, here are the highlights for v4.1.3. Pay attention to the change to  .form-control s which adds a new fixed  height . Fixed:  Moved the browserslist config from our  package.json  to a separate file to avoid unintended inherited browser settings across npm projects. Fixed:  Removed the  :not(:root)  selector from our  svg  Reboot styles, resolving an issue that caused all inline SVGs ignore  vertical-align  styles via single class due to higher specificity. Fixed:  Buttons in custom file inputs are once again clickable when focused. Improved:  Bootstrap’s plugins can now be imported separately in any contexts because they...