通过ActiveMQ发送错误邮件的工具类

使用方法示例:

try {
    purchaseWalletPayMapper.update(upPay);
   }catch (Exception e){
   e.printStackTrace();
   //发送错误邮件                 
   activeMQDao.sendErrorMsg(e);
}

MQ发送错误邮件/短信工具类

package com.pospay.common.activemq;

import com.alibaba.fastjson.JSONObject;
import com.yxb.base.util.CommonUtil;
import com.yxb.base.util.DateUtil;
import com.yxb.base.util.IDGenerator;
import com.yxb.base.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static org.apache.commons.httpclient.util.DateUtil.formatDate;

/**
 * @Description
 * @Author xm
 * @Date 2020/6/7/007.
 */
@Repository
public class ActiveMQDao {

    /** 当前服务名 */
    public static final String Current_ServiceName = "pos";
    /** 系统异常 放入的队列 */
    private static final String Queue_Exception_SendMail = "send_error_mail";
    /** 非技术通知邮件 */
    private static final String Notify_EMail = "send_notify_email_xm";
    /** 统一的发送短信,里面需要有接收人的手机号和内容 */
    private static final String Unified_SMS = "send_operate_sms1";
    /** 统一发送短信 默认接收短信人手机号 **/
    private static final String SMS_accepter= "16666666666";
    /** 发送邮件 默认接收邮件人的邮箱 */
    private static final String Email_accepter = "xxxx@yxb.com";
    /** 统一的发送邮件,里面需要有接收人和标题 内容 **/
    private static final String Unified_Email = "send_operate_email";
    /** 海科交易流水统计 队列名字 */
    public static final String Hk_trade_Count = "hk_trade_count";
    /** 汇付交易流水统计 队列名字 */
    public static final String Hf_trade_Count = "hf_trade_count";
    /** 快钱MPOS交易流水统计 队列名字 */
    public static final String Kq_trade_Count = "kq_trade_count";
    /** 快钱电签交易流水统计 队列名字 */
    public static final String KqSign_trade_Count = "kqsign_trade_count";
    /** 快钱大POS交易流水统计 队列名字 */
    public static final String KqBig_trade_Count = "kqbig_trade_count";
    /** 国通交易流水统计 队列名字 */
    public static final String Gt_trade_Count = "gt_trade_count";
    /** 海科无押版交易流水统计 队列名字 */
    public static final String HkFree_trade_Count = "hkfree_trade_count";
    /** 海科青春无押版交易流水统计 队列名字 */
    public static final String HkFreePlus_trade_Count = "hkfreeplus_trade_count";
    /** 快钱无押版交易流水统计 队列名字 */
    public static final String KqFree_trade_Count = "kqfree_trade_count";
    /** 快钱电签无押交易流水统计 队列名字 */
    public static final String KqSignFree_trade_Count = "kqsignfree_trade_count";
    /** 富友交易流水统计 队列名字 */
    public static final String Fy_trade_Count = "fy_trade_Count";
    /** 富友刷脸支付交易流水统计 队列名字 */
    public static final String Face_trade_Count = "face_trade_count";
    /** 月结分润发放 队列名字 */
    public static final String income_grant = "income_grant";
    /**  商户预转移处理,每有一个新增商户时,触发该队列 */
    public static final String platform_activity = "platform_activity";
    /**  快钱交易二次通知 */
    public static final String Kq_Trade_Sec_Notice = "kq_trade_sec_notice";
    /**  快钱激活交易二次通知 */
    public static final String Kq_Active_Trade_Sec_Notice = "kq_active_trade_sec_notice";
    /**  商户发放处理费通知 */
    public static final String merchantBackAmount_notice = "merchantBackAmount_notice";
    /**  创建新的Execl下载任务  队列名字*/
    public static final String DownExcel_Task = "downExcel_task";

    @Value("${spring.application.name}")
    private String moduleName;
    @Value("${spring.application.environment}")
    private String applicationEnv;

    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * 发送非技术通知邮件
     * @param title 邮件标题
     * @param msg 邮件内容
     * @param accepter 指定收件人(未指定则为默认)
     * @return
     */
    public Boolean sendNotifyEmail(String title, String msg, String accepter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", "yxb非技术异常通知-" + title);
        jsonObject.put("accepter", accepter == null?Email_accepter:accepter);
        jsonObject.put("【ServiceName】", Current_ServiceName);
        jsonObject.put("【ModuleName】", moduleName);
        jsonObject.put("【Environment】", applicationEnv);
        jsonObject.put("【Time】",formatDate(new Date()));
        jsonObject.put("【Message】", msg);
        return this.sendJmsMsg(Notify_EMail, jsonObject.toString());
    }

    /**
     * 发送消息给指定队列
     * @param destinationName
     * @param message
     * @return
     */
    public Boolean sendJmsMsg(String destinationName,Object message){
        if(message == null){
            return false;
        }
        String msg = CommonUtil.beanToString(message);
        return this.sendJmsMsg(destinationName,msg);
    }
    /**
     * 发送指定目标mq
     * @param destinationName 目标名
     * @param message 消息内容
     * @return
     */
    public Boolean sendJmsMsg(String destinationName, String message) {
        try{
            jmsTemplate.convertAndSend(destinationName, message);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 发送错误邮件
     * @param e
     * @return
     */
    public Boolean sendErrorMsg(Exception e){
        return sendErrorMsg(e,null);
    }

    /**
     * 发送错误邮件
     * @param e
     * @param jsonObject
     * @return
     */
    public Boolean sendErrorMsg(Exception e, JSONObject jsonObject){
        if(jsonObject == null){
            jsonObject = new JSONObject();
        }
        String Cause = e.getClass().getName() + ":" + (StringUtils.isNotBlank(e.getMessage())?e.getMessage():"");
        if(Cause.indexOf("Broken pipe") > -1){
            return true;
        }
        jsonObject.put("Time", DateUtil.getDateTime());
        jsonObject.put("ServiceName", Current_ServiceName);
        jsonObject.put("ModuleName", moduleName);
        jsonObject.put("Environment", applicationEnv);
        jsonObject.put("Cause", Cause);
        StackTraceElement stackTraceRoot = e.getStackTrace()[0];
        jsonObject.put("MethodName", stackTraceRoot.getClassName()+ "." + stackTraceRoot.getMethodName()+"()");
        jsonObject.put("LineNumber", stackTraceRoot.getLineNumber());
        StackTraceElement[] stackTraces = e.getStackTrace();
        Boolean bPage = false;
        String pageId = null;
        String pageTwo = null;
        if (stackTraces != null && stackTraces.length > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (StackTraceElement stackTrace : stackTraces) {
                stringBuilder.append(stackTrace + "").append("\r\n");
            }
            if (stringBuilder.toString().length() > 6000) {
                bPage = true;
                pageId = IDGenerator.getStringId();
                String pageOne = stringBuilder.toString().substring(0, 6000);
                pageTwo = stringBuilder.toString().substring(6000, stringBuilder.toString().length());
                jsonObject.put("PageNum", 1 );
                jsonObject.put("PageInfo", "分页Id:"+ pageId+",第1页" );
                jsonObject.put("MoreInfo", pageOne);
            } else {
                jsonObject.put("PageNum", 1 );
                jsonObject.put("PageInfo", "" );
                jsonObject.put("MoreInfo", stringBuilder + "");
            }
        }
        this.sendJmsMsg(Queue_Exception_SendMail, jsonObject.toString());
        if (bPage) {
            jsonObject.put("PageNum", 2);
            jsonObject.put("PageInfo", "分页Id:"+ pageId+",第2页");
            jsonObject.put("MoreInfo", pageTwo);
            this.sendJmsMsg(Queue_Exception_SendMail, jsonObject.toString());
        }
        return true;
    }

    /**
     * 发送报警短信
     *  短信接收人和短信模板 接收人是固定的 通过code码来区分是哪个项目的
     * @param code
     */
    public void sendWarnSms(String code){
        if(StringUtil.isBlank(code)){
            return ;
        }
        JSONObject param = new JSONObject();
        param.put("code", code);
        sendUnifiedMessage(null, null, param);
    }

    /**
     * 统一的发送短信的
     * @param accepter 接收人短信人的手机号
     * @param smsTemplate 短信模板 可以为空,为空则代表给默认的短信模板
     * @param param 短信模板里面需要带的参数
     */
    public void sendUnifiedMessage(String accepter, String smsTemplate, JSONObject param){
        if(StringUtil.isBlank(accepter)){
            accepter = Constant.accepter;
        }
        if(smsTemplate == null){
            smsTemplate = Constant.Error_Default_SMSTemplate;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accepter", accepter);
        jsonObject.put("smsTemplate", smsTemplate);
        jsonObject.put("param", param.toString());
        jsonObject.put("serviceName", Current_ServiceName);
        this.sendJmsMsg(Unified_SMS,jsonObject);
    }

    /**
     * 发送统一标准的邮件
     * @param accepter 接收人的哟想 可以为空,如果为空,则发送给开发人员 platform-message项目已做处理
     * @param title 邮箱的标题 可以为空,如果为空,则title为:技术邮件,请相关人员尽快处理!
     * @param content  邮箱的内容,如果是html需要自己写html的内容
     */
    public void sendUnifiedEmail(String accepter,String title,String content){
        JSONObject mailJson = new JSONObject();
        if(StringUtil.isNotBlank(accepter)){
            mailJson.put("accepter", accepter);
        }
        if(StringUtil.isBlank(title)){
            title = "技术邮件,请相关人员尽快处理!";
        }else{
            title = "[yxb-" + applicationEnv + "]" + title;
        }
        if(StringUtil.isBlank(content)){
            return ;
        }
        mailJson.put("subject", title);
        mailJson.put("content", content);
        mailJson.put("ServiceName", Current_ServiceName);
        this.sendJmsMsg(Unified_Email,mailJson);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值