All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.monitor.warn.MailWarn Maven / Gradle / Ivy

There is a newer version: 2025.01
Show newest version
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.monitor.warn;

import com.taotao.boot.common.utils.context.ContextUtils;
import com.taotao.boot.common.utils.lang.StringUtils;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.common.utils.reflect.ReflectionUtils;
import com.taotao.boot.common.utils.servlet.RequestUtils;
import com.taotao.boot.core.utils.RequestWebUtils;
import com.taotao.boot.monitor.model.Message;
import com.taotao.boot.monitor.properties.WarnProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * MailWarn
 *
 * @author shuigedeng
 * @version 2021.9
 * @since 2021-09-10 16:47:26
 */
public class MailWarn extends AbstractWarn {

    private static final String CLASS = "com.taotao.boot.mail.template.MailTemplate";

    private final boolean driverExist;

    public MailWarn() {
        this.driverExist = ReflectionUtils.tryClassForName(CLASS) != null;
    }

    @Override
    public void notify(Message message) {
        if (!driverExist) {
            LogUtils.error("未找到MailTemplate, 不支持邮件预警");
            return;
        }

        WarnProperties warnProperties = ContextUtils.getBean(WarnProperties.class, true);
        Object mailTemplate = ContextUtils.getBean(ReflectionUtils.tryClassForName(CLASS), true);

        if (Objects.nonNull(mailTemplate) && Objects.nonNull(warnProperties)) {
            String ip = RequestUtils.getIpAddress();

            String dingDingFilterIP = warnProperties.getDingdingFilterIP();
            if (!StringUtils.isEmpty(ip) && !dingDingFilterIP.contains(ip)) {
                String context = StringUtils.subString3(message.getTitle(), 100)
                        + "\n"
                        + "详情: "
                        + RequestWebUtils.getBaseUrl()
                        + "/health/\n"
                        + StringUtils.subString3(message.getContent(), 500);

                try {
                    List param = new ArrayList<>();
                    param.add("[email protected]");
                    param.add("服务状态预警");
                    param.add(context);
                    ReflectionUtils.callMethod(mailTemplate, "sendSimpleMail", param.toArray());
                } catch (Exception e) {
                    LogUtils.error(e);
                }
            }
        }
    }
}