Skip to content

Commit 1e26f74

Browse files
authored
Merge pull request #43 from junpengzhou/master
fix(1.9.1): 动态设置系统默认时区,避免写死的+8时区导致触发时间部署国外期望按系统默认时间进行触发时偏移触发导致的问题
2 parents 307cf7d + 179318d commit 1e26f74

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

snail-job-server-starter/src/main/java/com/aizuda/snailjob/server/SnailJobServerApplication.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package com.aizuda.snailjob.server;
1616

17+
import cn.hutool.core.util.StrUtil;
1718
import com.aizuda.snailjob.server.common.rpc.server.grpc.GrpcServer;
1819
import lombok.extern.slf4j.Slf4j;
1920
import org.springframework.boot.ApplicationRunner;
@@ -33,7 +34,9 @@
3334
public class SnailJobServerApplication {
3435

3536
public static void main(String[] args) {
36-
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
37+
// 初始化默认时区
38+
initDefaultTimeZone();
39+
// 启动SnailJob服务
3740
SpringApplication.run(SnailJobServerApplication.class, args);
3841
}
3942

@@ -47,7 +50,7 @@ public ApplicationRunner nettyStartupChecker(GrpcServer grpcServer,
4750
ServletWebServerFactory serverFactory) {
4851
return args -> {
4952
// 判定Grpc
50-
boolean started = grpcServer.isStarted();
53+
boolean started = grpcServer.isStarted();
5154
// 最长自旋10秒,保证 grpcHttpServer启动完成
5255
int waitCount = 0;
5356
while (!started && waitCount < 100) {
@@ -65,4 +68,27 @@ public ApplicationRunner nettyStartupChecker(GrpcServer grpcServer,
6568
}
6669
};
6770
}
71+
72+
/**
73+
* 从环境变量读取时区配置,如果没有则使用系统默认时区
74+
* fixed 避免固定上海时区导致部署国际化服务的机房时,时区如何设置调整都为上海时区时间进行触发的问题
75+
*/
76+
private static void initDefaultTimeZone() {
77+
String timezone = System.getenv("TZ");
78+
79+
if (StrUtil.isNotBlank(timezone)) {
80+
try {
81+
TimeZone.setDefault(TimeZone.getTimeZone(timezone.trim()));
82+
log.info("--------> System timezone set to: {} from environment variable TZ", timezone);
83+
} catch (Exception e) {
84+
log.warn("--------> Invalid timezone '{}' from environment variable TZ, " +
85+
"using system default timezone. Error: {}",
86+
timezone, e.getMessage());
87+
}
88+
return;
89+
}
90+
91+
log.info("--------> Environment variable TZ not set, using system default timezone: {}",
92+
TimeZone.getDefault().getID());
93+
}
6894
}

0 commit comments

Comments
 (0)