竹简文档
通知服务

数据模型

NotifyMessage 与 NotifyResult 定义通知服务的请求与响应数据结构

数据模型

bamboo-notify 模块通过 NotifyMessageNotifyResult 两个数据模型,分别定义通知请求的输入参数与发送结果的输出结构。两者均采用 Lombok @Builder 模式,支持链式构建。

NotifyMessage

NotifyMessage 是通知消息的载体,封装了接收方、内容、类型等全部发送参数。

类定义

NotifyMessage.java
package com.xlf.utility.notify;

import lombok.Builder;

// 通知消息模型,使用 Builder 模式构建
@Builder
public class NotifyMessage {
    private String to;
    private String subject;
    private String content;
    private NotifyType type;
    private Map<String, Object> variables;
    private int priority;
}

字段说明

字段

类型

构建示例

NotifyMessageExample.java
// 完整参数构建
NotifyMessage message = NotifyMessage.builder()
        .to("admin@example.com")
        .subject("系统告警")
        .content("服务 ${service} 在 ${time} 出现异常:${error}")
        .type(NotifyType.EMAIL)
        .variables(Map.of(
                "service", "order-service",
                "time", "2026-03-02 14:30:00",
                "error", "数据库连接超时"
        ))
        .priority(1)
        .build();
NotifyMessageSimple.java
// 最简构建(仅必填字段)
NotifyMessage message = NotifyMessage.builder()
        .to("https://hooks.example.com/notify")
        .subject("部署完成")
        .content("{\"status\":\"success\"}")
        .type(NotifyType.WEBHOOK)
        .build();

NotifyResult

NotifyResult 是通知发送结果的封装,包含发送状态、结果消息与时间戳等信息。提供静态工厂方法简化实例创建。

类定义

NotifyResult.java
package com.xlf.utility.notify;

import lombok.Builder;
import java.time.LocalDateTime;

// 通知结果模型,提供静态工厂方法
@Builder
public class NotifyResult {
    private boolean success;
    private String message;
    private LocalDateTime timestamp;
    private String errorCode;

    public static NotifyResult success(String message) { ... }
    public static NotifyResult failure(String message) { ... }
    public static NotifyResult failure(String message, String errorCode) { ... }
}

字段说明

字段

类型

静态工厂方法

方法签名说明
success(String message)创建成功结果,successtrue
failure(String message)创建失败结果,successfalse,无错误码
failure(String message, String errorCode)创建失败结果,携带错误码

使用示例

NotifyResultExample.java
// 成功结果
NotifyResult success = NotifyResult.success("邮件发送成功");

// 失败结果(携带错误码)
NotifyResult failure = NotifyResult.failure(
        "SMTP 连接超时", "SMTP_TIMEOUT");

// 判断发送结果
if (result.isSuccess()) {
    log.info("通知发送成功: {}", result.getMessage());
} else {
    log.error("通知发送失败 [{}]: {}",
            result.getErrorCode(), result.getMessage());
}

下一步

On this page