Skip to content

Commit 9adb57b

Browse files
committed
add jobs tool for run scheduler jobs in app
1 parent b39257d commit 9adb57b

File tree

9 files changed

+128
-7
lines changed

9 files changed

+128
-7
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
This project, implement instance of basic feature, <br> you maybe want use for App base on Spring-boot <br>
44

5+
### Note for upgrade to 2.4.X
6+
1) for active profile config: <br>
7+
for before 2.4.x you can activate profile in pom file and add <b>@spring.profiles.active@</b> in application.properties <br>
8+
and then properties apply in project base on profile that active. <br>
9+
for 2.4.x or above
10+
- if you want to use same as old solution you must add ```spring.config.use-legacy-processing=true``` in application.properties
11+
- if you want to migrate in new approach please check https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide
12+
13+
514
## Features:
615
- spring-web
716
- spring-jpa
@@ -30,6 +39,8 @@ This project, implement instance of basic feature, <br> you maybe want use for A
3039
- use CriteriaQuery and CriteriaBuilder for search base on Entity
3140
- jasypt
3241
- use for encrypt password and added to config file, then decrypted pass in runtime
42+
- Jobs
43+
- use Spring TaskScheduler for run jobs
3344

3445
## Run guide
3546
### Run for develop and debug: <br>
@@ -109,3 +120,11 @@ hikari.dataSource.password="ENC({output})"
109120
~~~
110121
--jasypt.encryptor.password={secret-key}
111122
~~~
123+
124+
### Jobs Run Tool
125+
1 - define class implement Runnable in order to execute job base on schedule
126+
2 - define bean (AlertServiceJob) in BeanConfig class for your jobs
127+
3 - define trigger time in properties ```(ex: app.jobs.alertServiceJob=0 */1 * ? * *)``` file
128+
4 - @EnableScheduling on SpringApp
129+
130+
notice: that name of bean in BeanConfig must be equal with name of key in properties file.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ir.bigz.springbootreal.configuration;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
@Component
11+
@ConfigurationProperties("app")
12+
@Data
13+
public class AppProperties {
14+
15+
private Map<String, String> jobs = new HashMap<>();
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ir.bigz.springbootreal.configuration;
2+
3+
import ir.bigz.springbootreal.service.AlertServiceJob;
4+
import ir.bigz.springbootreal.service.UserService;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.scheduling.TaskScheduler;
8+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
9+
10+
@Configuration
11+
public class BeanConfig {
12+
13+
@Bean("taskScheduler")
14+
public TaskScheduler taskScheduler(){
15+
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
16+
scheduler.setPoolSize(10);
17+
scheduler.setThreadNamePrefix("scheduled-task-");
18+
scheduler.setDaemon(true);
19+
return scheduler;
20+
}
21+
22+
@Bean("alertServiceJob")
23+
public AlertServiceJob alertServiceJob(UserService userService){
24+
return new AlertServiceJob(userService);
25+
}
26+
}

src/main/java/ir/bigz/springbootreal/configuration/HikariDataSourceInit.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
/**
1717
* {@link HikariDataSourceInit} use for build connection-pool base on HikariCp with customize properties,
1818
* and then added to {@link DataSourceConfiguration} class as datasource for use in app.
19-
* also if you want use for production uncomment line 32 and comment line 33 and 34
20-
* and if you want test with testContainer uncomment line 33 and 34 and comment line 32
19+
* also if you want use for production uncomment line 41 and comment line 42 and 43
20+
* and if you want test with testContainer uncomment line 42 and 43 and comment line 41
2121
*
2222
* DataBase password encryption with JASYPT:
2323
* first of all use jasypt to encrypted db-password and added into application.properties file,
24-
* then uncomment line 34 (encryptorPassword)
24+
* then uncomment line 37 (encryptorPassword)
2525
* at the end add --jasypt.encryptor.password={secret-key} in program environment
2626
*/
2727

@@ -38,9 +38,9 @@ public class HikariDataSourceInit{
3838

3939
@Bean(name = "HikariDataSourceInit")
4040
public DataSource dataSource(){
41-
HikariConfig hikariConfig = new HikariConfig(hikariProperties()); // use for production
42-
// HikariConfig hikariConfig = new HikariConfig();
43-
// hikariConfig.setDataSource(InitDataSource());
41+
// HikariConfig hikariConfig = new HikariConfig(hikariProperties()); // use for production
42+
HikariConfig hikariConfig = new HikariConfig();
43+
hikariConfig.setDataSource(InitDataSource());
4444
int cpuCores = Runtime.getRuntime().availableProcessors();
4545
hikariConfig.setMaximumPoolSize(cpuCores * 4);
4646
hikariConfig.setConnectionTimeout(Long.parseLong(env.getProperty("hikari.connectionTimeout")));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ir.bigz.springbootreal.configuration;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.scheduling.TaskScheduler;
8+
import org.springframework.scheduling.annotation.EnableScheduling;
9+
import org.springframework.scheduling.support.CronTrigger;
10+
11+
@Slf4j
12+
@Configuration
13+
@EnableScheduling
14+
public class JobsConfig {
15+
16+
public JobsConfig(ApplicationContext applicationContext,
17+
@Qualifier("taskScheduler") TaskScheduler taskScheduler,
18+
AppProperties appProperties) {
19+
for (var e: appProperties.getJobs().entrySet()){
20+
var bean = (Runnable) applicationContext.getBean(e.getKey());
21+
if(bean == null){
22+
log.error("Job {} is not registered", e.getKey());
23+
}
24+
else {
25+
log.info("Job {} is registered as cron {}", e.getKey(), e.getValue());
26+
taskScheduler.schedule(bean::run, new CronTrigger(e.getValue()));
27+
}
28+
}
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ir.bigz.springbootreal.service;
2+
3+
import ir.bigz.springbootreal.viewmodel.UserModel;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
6+
import java.util.List;
7+
8+
public class AlertServiceJob implements Runnable {
9+
10+
private final UserService userService;
11+
12+
@Autowired
13+
public AlertServiceJob(UserService userService) {
14+
this.userService = userService;
15+
}
16+
17+
@Override
18+
public void run() {
19+
List<UserModel> all = userService.getAll();
20+
System.out.println("count of data is " + all.size());
21+
}
22+
}

src/main/resources/application-dev.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ [email protected]@
3535
springdoc.swagger-ui.path=/swagger-ui.html
3636
springdoc.api-docs.path=/api-docs
3737
springdoc.use-fqn=true
38+
39+
40+
################### Application Jobs Schedule ##########################
41+
app.jobs.alertServiceJob=0 */1 * ? * *

src/main/resources/application-docker.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ [email protected]@
3939
application-version=@project.version@
4040
springdoc.swagger-ui.path=/swagger-ui.html
4141
springdoc.api-docs.path=/api-docs
42-
springdoc.use-fqn=true
42+
springdoc.use-fqn=true
43+
44+
################### Application Jobs Schedule ##########################
45+
app.jobs.alertServiceJob=0 */1 * ? * *
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
spring.config.use-legacy-processing=true
12
spring.profiles.active[email protected]@

0 commit comments

Comments
 (0)