广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 974c7aa4010d77bb410b99931b4435d5442deb4b
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
package com.shlb.timescaledbutils.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Component;
 
import javax.sql.DataSource;
import java.sql.Connection;
 
@Component
@Slf4j
public class StartupStatusPrinter implements ApplicationRunner {
 
    @Autowired
    private Environment environment;
 
    @Autowired
    private DataSource dataSource;
 
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String port = environment.getProperty("server.port");
        
        boolean postgresConnected = false;
        try (Connection connection = dataSource.getConnection()) {
            postgresConnected = connection.isValid(1000);
        } catch (Exception e) {
            log.error("PostgreSQL 连接失败: {}", e.getMessage());
        }
 
        boolean redisConnected = false;
        try {
            String ping = redisConnectionFactory.getConnection().ping();
            redisConnected = "PONG".equalsIgnoreCase(ping);
        } catch (Exception e) {
            log.error("Redis 连接失败: {}", e.getMessage());
        }
 
        System.out.println("\n----------------------------------------------------------");
        System.out.println("\tApplication is running! Access URLs:");
        System.out.println("\tLocal: \t\thttp://localhost:" + port);
        System.out.println("\tPostgreSQL:\t" + (postgresConnected ? "Connected ✅" : "Failed ❌"));
        System.out.println("\tRedis:\t\t" + (redisConnected ? "Connected ✅" : "Failed ❌"));
        System.out.println("----------------------------------------------------------\n");
    }
}