2026-06-30 24681c81c09022f584a57006f2534b5f74723414
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
package cn.iocoder.yudao.module.iot.service.rule.data.action;
 
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkDatabaseConfig;
import cn.iocoder.yudao.module.iot.enums.rule.IotDataSinkTypeEnum;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
 
/**
 * Database 的 {@link IotDataRuleAction} 实现类
 *
 * 通过 JDBC 连接数据库,将设备消息写入指定表。
 * 支持 MySQL、PostgreSQL、Oracle、SQL Server、DM 达梦等数据库,
 * HikariCP 会根据 JDBC URL 自动加载对应的驱动。
 *
 * @author HUIHUI
 */
@Component
@Slf4j
public class IotDatabaseDataRuleAction extends
        IotDataRuleCacheableAction<IotDataSinkDatabaseConfig, JdbcTemplate> {
 
    @Override
    public Integer getType() {
        return IotDataSinkTypeEnum.DATABASE.getType();
    }
 
    @Override
    public void execute(IotDeviceMessage message, IotDataSinkDatabaseConfig config) throws Exception {
        try {
            // 1. 获取或创建 JdbcTemplate
            JdbcTemplate jdbcTemplate = getProducer(config);
 
            // 2. 构建并执行 INSERT SQL
            String sql = StrUtil.format(
                    "INSERT INTO {} (id, device_id, tenant_id, method, report_time, data, create_time) VALUES (?, ?, ?, ?, ?, ?, NOW())",
                    config.getTableName());
            String messageJson = JsonUtils.toJsonString(message);
            jdbcTemplate.update(sql,
                    message.getId(),
                    message.getDeviceId(),
                    message.getTenantId(),
                    message.getMethod(),
                    message.getReportTime(),
                    messageJson);
            log.info("[execute][message({}) config({}) 写入数据库成功,table: {}]",
                    message.getId(), config, config.getTableName());
        } catch (Exception e) {
            log.error("[execute][message({}) config({}) 写入数据库失败]", message, config, e);
            throw e;
        }
    }
 
    @Override
    protected JdbcTemplate initProducer(IotDataSinkDatabaseConfig config) throws Exception {
        // 使用 HikariCP 连接池,HikariCP 会根据 JDBC URL 自动检测并加载对应的数据库驱动
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(config.getJdbcUrl());
        hikariConfig.setUsername(config.getUsername());
        hikariConfig.setPassword(config.getPassword());
        // 连接池配置
        hikariConfig.setMaximumPoolSize(5); // 数据流转场景,不需要太多连接
        hikariConfig.setMinimumIdle(1);
        hikariConfig.setConnectionTimeout(10000); // 连接超时 10 秒
        hikariConfig.setIdleTimeout(300000); // 空闲超时 5 分钟
        hikariConfig.setMaxLifetime(600000); // 最大生命周期 10 分钟
        HikariDataSource dataSource = new HikariDataSource(hikariConfig);
        log.info("[initProducer][数据库连接池创建成功,jdbcUrl: {}]", config.getJdbcUrl());
        return new JdbcTemplate(dataSource);
    }
 
    @Override
    protected void closeProducer(JdbcTemplate producer) throws Exception {
        if (producer.getDataSource() instanceof HikariDataSource) {
            ((HikariDataSource) producer.getDataSource()).close();
        }
    }
 
}