-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathScheduledTaskConfig.java
88 lines (73 loc) · 3.65 KB
/
ScheduledTaskConfig.java
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
package org.mitre.springboot.config;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.mitre.oauth2.service.impl.DefaultOAuth2AuthorizationCodeService;
import org.mitre.oauth2.service.impl.DefaultOAuth2ProviderTokenService;
import org.mitre.openid.connect.service.ApprovedSiteService;
import org.mitre.springboot.config.ScheduledTaskConfig.SchedulingEnabledCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
@Conditional(SchedulingEnabledCondition.class)
@ConfigurationProperties(prefix="openid.connect.scheduling")
public class ScheduledTaskConfig implements SchedulingConfigurer {
@Autowired
private DefaultOAuth2ProviderTokenService defaultOAuth2ProviderTokenService;
@Autowired
private ApprovedSiteService approvedSiteService;
protected Integer corePoolSize = 5;
@Autowired
private DefaultOAuth2AuthorizationCodeService defaultOAuth2AuthorizationCodeService;
@Bean(destroyMethod="shutdown")
public Executor taskScheduler() {
return Executors.newScheduledThreadPool(corePoolSize);
}
@Scheduled(fixedDelayString = "${openid.connect.scheduling.tasks.clearExpiredTokens.fixedDelay:30000}",
initialDelayString = "${openid.connect.scheduling.tasks.clearExpiredTokens.initialDelay:60000}")
public void clearExpiredTokens() {
defaultOAuth2ProviderTokenService.clearExpiredTokens();
}
@Scheduled(fixedDelayString = "${openid.connect.scheduling.tasks.clearExpiredSites.fixedDelay:30000}",
initialDelayString = "${openid.connect.scheduling.tasks.clearExpiredSites.initialDelay:60000}")
public void clearExpiredSites() {
approvedSiteService.clearExpiredSites();
}
@Scheduled(fixedDelayString = "${openid.connect.scheduling.tasks.clearExpiredAuthorizationCodes.fixedDelay:30000}",
initialDelayString = "${openid.connect.scheduling.tasks.clearExpiredAuthorizationCodes.initialDelay:60000}")
public void clearExpiredAuthorizationCodes() {
defaultOAuth2AuthorizationCodeService.clearExpiredAuthorizationCodes();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
protected static class SchedulingEnabledCondition extends SpringBootCondition implements ConfigurationCondition {
@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.REGISTER_BEAN;
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
"openid.connect.scheduling.");
String enabled = resolver.getProperty("enabled");
if(enabled != null && Boolean.valueOf(enabled).equals(false)) {
return ConditionOutcome.noMatch("Task Scheduling is disabled");
}
return ConditionOutcome.match("Task Scheduling is enabled");
}
}
}