Skip to content

Commit c30295c

Browse files
authored
[improve] Increase the priority of configuration initialization (apache#2709)
1 parent bb0e2ea commit c30295c

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java renamed to manager/src/main/java/org/apache/hertzbeat/manager/config/ConfigInitializer.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Locale;
2525
import java.util.Random;
2626
import java.util.TimeZone;
27+
import lombok.SneakyThrows;
2728
import org.apache.commons.lang3.StringUtils;
2829
import org.apache.hertzbeat.common.constants.CommonConstants;
2930
import org.apache.hertzbeat.common.entity.manager.GeneralConfig;
@@ -37,46 +38,48 @@
3738
import org.apache.hertzbeat.manager.service.impl.SystemSecretServiceImpl;
3839
import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl;
3940
import org.springframework.beans.factory.annotation.Value;
40-
import org.springframework.boot.CommandLineRunner;
41+
import org.springframework.context.SmartLifecycle;
4142
import org.springframework.core.Ordered;
4243
import org.springframework.core.annotation.Order;
4344
import org.springframework.stereotype.Component;
4445

4546
/**
46-
* Common CommandLineRunner class
47+
* Config Initializer
4748
*/
4849
@Component
4950
@Order(value = Ordered.HIGHEST_PRECEDENCE + 2)
50-
public class CommonCommandLineRunner implements CommandLineRunner {
51+
public class ConfigInitializer implements SmartLifecycle {
5152

52-
private static final String DEFAULT_JWT_SECRET = "CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R "
53+
private boolean running = false;
54+
55+
private static final String DEFAULT_JWT_SECRET = "CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R "
5356
+ "LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9 "
5457
+ "8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5 "
5558
+ "dKhaX0csgOCTlCxq20yhmUea6H6JIpSE2Rwp";
56-
59+
5760
@Value("${sureness.jwt.secret:" + DEFAULT_JWT_SECRET + "}")
5861
private String currentJwtSecret;
59-
62+
6063
@Resource
6164
private SystemGeneralConfigServiceImpl systemGeneralConfigService;
62-
65+
6366
@Resource
6467
private SystemSecretServiceImpl systemSecretService;
65-
68+
6669
@Resource
6770
private TemplateConfigServiceImpl templateConfigService;
68-
71+
6972
@Resource
7073
private AppService appService;
71-
74+
7275
@Resource
7376
protected GeneralConfigDao generalConfigDao;
74-
77+
7578
@Resource
7679
protected ObjectMapper objectMapper;
7780

78-
@Override
79-
public void run(String... args) throws Exception {
81+
@SneakyThrows
82+
public void initConfig() {
8083
// for system config
8184
SystemConfig systemConfig = systemGeneralConfigService.getConfig();
8285
if (systemConfig != null) {
@@ -89,7 +92,7 @@ public void run(String... args) throws Exception {
8992
} else {
9093
// init system config data
9194
systemConfig = SystemConfig.builder().timeZoneId(TimeZone.getDefault().getID())
92-
.locale(Locale.getDefault().getLanguage() + CommonConstants.LOCALE_SEPARATOR
95+
.locale(Locale.getDefault().getLanguage() + CommonConstants.LOCALE_SEPARATOR
9396
+ Locale.getDefault().getCountry())
9497
.build();
9598
String contentJson = objectMapper.writeValueAsString(systemConfig);
@@ -126,4 +129,25 @@ public void run(String... args) throws Exception {
126129
// set the jwt secret token in util
127130
JsonWebTokenUtil.setDefaultSecretKey(currentJwtSecret);
128131
}
132+
133+
@Override
134+
public void start() {
135+
initConfig();
136+
running = true;
137+
}
138+
139+
@Override
140+
public void stop() {
141+
running = false;
142+
}
143+
144+
@Override
145+
public boolean isRunning() {
146+
return running;
147+
}
148+
149+
@Override
150+
public int getPhase() {
151+
return Ordered.HIGHEST_PRECEDENCE;
152+
}
129153
}

0 commit comments

Comments
 (0)