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

Serialize and Deserialize JSON data with C#

This post is about example for serialize and deserialize json data with C#. This example using Newtonsoft.Json to be a library to work with json data. You can add Newtonsoft.Json by download from  https://www.newtonsoft.com/json  or using Nuget to add it into your project. In this example is Bill object that hold billing data about Car object. Serialize Object to String Below is example code to serialize object to json string. At line 39 is code to convert object into json string with beautiful json format. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace JSON { class JSonExample { static void Main( string [] args) { List<Car> cars = new List<Car>(); float to...

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

Using composite key in Laravel

In this blog will be show you how to enable Laravel to support composite primary key (multiple column key). First of all you need to create class that override method save of class Model to support composite key. Below is the code. You can copy to your project. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use DB; class BaseModel extends Model { public function save ( array $options = []) { if ( ! is_array ( $this -> getKeyName ())) { return parent :: save ( $...