訊息sms 郵箱/手機號/push傳送的方案 & 定時任務xxlJob靈活度 & 泛型和傳送的模板類設計

oktokeep發表於2024-03-27

訊息sms 郵箱/手機號/push傳送的方案 & 定時任務xxlJob靈活度 & 泛型和傳送的模板類設計

1.訊息sms 郵箱/手機號/push傳送的方案
1.判斷收件人地址是否為空,不為空則傳送郵件。為空則不傳送。可以透過該方法終止一些訊息的傳送。
2.收件人的地址可以配置在Apollo中,直接刪除該key測試沒有生效。需要將該key配置成空字串。

2.定時任務xxlJob靈活度
xxlJob需要接收外部的引數,來靈活處理排程任務。比如資料表格的郵件傳送
需求背景:拉取前一天的資料,生成excel表格,然後透過email傳送出來。

@Slf4j
@Component
@JobHandler(value = "sendEmailTask")
public class SendEmailTask extends IJobHandler {

    @Autowired
    @Qualifier("mySendEmailImpl")
    private SendBiDataService<MyBiDataEntity> sendBiDataService;

    //根據實體類類注入也可以。
//    @Autowired
//    MySendEmailImpl mySendEmailImpl;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        Transaction t = Cat.newTransaction("JOB", "sendEmailTask");
        try {
            log.info("report start......");
            String startTime = "";
            String endTime = "";
            //獲取外部引數,開始時間和結束時間
            if(StringUtils.isNotBlank(s)){
                startTime = s.split(",")[0];
                endTime = s.split(",")[1];
            }else{
                //獲取系統設定時間,前天的開始時間和結束時間
                LocalDate today = LocalDate.now();
                LocalDate yesterday = today.plusDays(-1);
                LocalTime lt1 = LocalTime.of(0,0,1);
                LocalTime lt2 = LocalTime.of(23,59,59);
                LocalDateTime startLdt = yesterday.atTime(lt1);
                LocalDateTime endLdt = yesterday.atTime(lt2);
                startTime = LocalDateTimeUtils.formatDateTime(startLdt);
                endTime = LocalDateTimeUtils.formatDateTime(endLdt);
            }
            //業務邏輯 查詢資料
            List<MyBiDataEntity> list = sendBiDataService.queryBiData(startTime,endTime);
            if(CollectionUtils.isEmpty(list)){
                log.info("report 資料為空......");
               return ReturnT.SUCCESS;
            }
            //業務邏輯 建立excel
            String fileName = sendBiDataService.createBiExcel(list);
            log.info("report fileName......" + fileName);
            //業務邏輯 傳送郵件
            boolean flag = sendBiDataService.sendBiExcel(fileName);
            log.info("report flag......" + flag);
            log.info("report end......");
            t.setStatus(Transaction.SUCCESS);
            return SUCCESS;
        } catch (Exception e) {
            log.error("report郵件傳送異常:", e);
            Cat.logError("report郵件傳送異常:", e);
            t.setStatus(e);
            return FAIL;
        } finally {
            t.complete();
        }
    }
}

public class LocalDateTimeUtils {
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String formatDateTime(LocalDateTime dateTime) {
        if(dateTime == null) {
            return "";
        }
        return formatDateTime(dateTime, DEFAULT_PATTERN);
    }
}

3.泛型和傳送的模板類設計

@Service
public interface SendBiDataService<T extends Object> {
    List<T> queryBiData(String startTime,String endTime);
    String createBiExcel(List<T> list);
    boolean sendBiExcel(String fileName);
}



@Service
public abstract class SendBiDataCommonService<T> implements  SendBiDataService<T> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public String createBiExcel(List<T> list) {
      //通用公共邏輯實現
    }
}
    

@Slf4j
@Service("mySendEmailImpl")
public class MySendEmailImpl extends SendBiDataCommonService<MyBiDataEntity> implements SendBiDataService<MyBiDataEntity> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public List<MyBiDataEntity> queryBiData(String startTime, String endTime) {
        logger.info("queryBiData startTime=[{}],endTime=[{}]",startTime,endTime);
        //具體業務,查詢資料
        
    }

    @Override
    public String createBiExcel(List<MyBiDataEntity> list) {
        //呼叫公用的抽象類方法
        return super.createBiExcel(list);
    }

    @Override
    public boolean sendBiExcel(String fileName) {
        //具體業務,傳送人,郵件標題等引數,郵件傳送實現
    }
}

相關文章