前言
系統有一個需求就是采購員審批注冊供應商的信息時,會生成一個供應商的賬號,此時需要發送供應商的賬號信息(賬號、密碼)到注冊填寫的郵箱中,通知供應商賬號信息,當時很快就寫好了一個工具類,用來發送普通的文本郵件信息。但是隨著系統的迭代,后面又新增了一些需求,比如一些單據需要在供應商確認時,發送一條站內信到首頁,這樣采購員登錄時就可以看到最新的單據信息,進行相應的處理;或者采購員創建一些單據時,需要發送站內信到首頁,然后供應商登錄系統時,可以看到最新單據信息并進行處理,因此,我在原有的工具類基礎上,修改發送郵件信息的方法,加入了消息類型參數,并根據消息類型,調用相應的方法處理;過了一段時間,業務又找了過來,說當用戶修改密碼時,需要發送一個短信驗證碼,驗證碼輸對了才給他修改,接著我又在工具類里面,加入了處理短信的發送邏輯。
偽代碼如下:
@Component
public class NoticeSendUtils {// 省略其他配置/*** 發送消息** @param params 參數* @param type 消息類型(0-郵件消息,1-站內信消息,2-短信消息)* @param content 消息內容*/public void sendMessage(Object params, Integer type, String content) {if (type.equals(0)) {this.sendMailMessage(params, content);} else if (type.equals(1)) {this.sendStationMessage(params, content);} else {this.sendPhoneMessage(params, content);}}/*** 發送郵件消息* * @param params* @param content*/private void sendMailMessage(Object params, String content) {// 處理郵件消息}/*** 發送站內信消息* * @param params* @param content*/private void sendStationMessage(Object params, String content) {// 處理站內信消息}/*** 發送短信消息* * @param params* @param content*/private void sendPhoneMessage(Object params, String content) {// 處理短信消息}
存在問題
- 當需要新增一種消息發送類型時,需要修改該工具類加上if-else邏輯,處理新的消息類型發送,這違背了開放封閉原則(軟件實體應該對擴展開放,對修改封閉。這意味著當軟件需要適應新的需求時,應該通過添加新的代碼來擴展系統的行為,而不是修改已有的代碼),新增一種消息類型,就要修改該類原有的方法
- 調用者調用時,需要指定消息類型和內容,系統就會存在大量這樣的調用代碼,如果需要在發送消息的方法新增參數,那么所有調用者都需要改變新增參數,系統后期就會非常難維護
- 沒有對消息發送過程產生的異常進行處理,無法知曉消息有沒有發送成功
因此,趁著最近沒有什么需求,對消息發送功能采用策略模式進行了重構,由消息模板的類型決定調用相應的消息類型處理類處理消息發送,獨立維護了一個消息中心模塊,也提供頁面管理功能,可對消息發送模板進行配置,并且存儲了消息發送記錄,這樣可以知曉消息有沒有發送成功,對原有的消息發送功能進行了解耦。
以下僅提供部分核心代碼和相關表設計,關鍵的是其中的設計思想
使用
消息規則配置
主要配置發送方的郵箱配置和短信功能賬號配置,系統采用了阿里云的短信服務,所以配置了阿里云的短信服務的賬號和密碼;在發送消息時,先查一下這里面的配置,比如發送郵箱消息,則查詢規則類型為郵箱的信息,查詢到了就調用相應的方法發送消息
如圖所示
消息模板配置
主要配置消息模板,每個消息模板都有唯一的模板編碼,一個消息模板可以有多個適用規則,比如一個模板有短信和站內信的適用規則,那么當調用者使用這個模板時,會同時發送一個站內信(首頁待辦消息展示)和一封郵件信息
列表頁面如圖所示
修改頁面如圖所示
- 短信相關配置只有適用規則為短信才必填
- PC-地址主要是為了站內信實現點擊消息時,跳轉到對應頁面
- 短信模板編碼由阿里云短信服務提供
- 調用者的參數字段名稱需要和模板內容的${}表達式中的名稱一致(使用了freemarker進行模板渲染)
消息發送記錄
主要查看消息有沒有發送成功
消息接收中心
主要顯示站內信發送情況
設計
消息規則配置表
CREATE TABLE `msg_configuration` (`id` varchar(36) NOT NULL COMMENT '主鍵',`code` varchar(255) DEFAULT NULL COMMENT '編碼',`ip` varchar(255) DEFAULT NULL COMMENT 'ip',`password` varchar(255) DEFAULT NULL COMMENT '密碼',`port` varchar(50) DEFAULT NULL COMMENT '端口',`protocol` varchar(100) DEFAULT NULL COMMENT '協議名稱',`type` int(11) DEFAULT NULL COMMENT '0郵箱 1短信',`username` varchar(255) DEFAULT NULL COMMENT '用戶名',`enable` int(11) DEFAULT NULL COMMENT '0未啟用 1啟用',`create_date` datetime DEFAULT NULL COMMENT '創建時間',`creator` varchar(36) DEFAULT NULL COMMENT '創建人',`update_date` datetime DEFAULT NULL COMMENT '修改時間',`modifier` varchar(36) DEFAULT NULL COMMENT '最后修改人',PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='消息規則配置表';
消息模板配置表
CREATE TABLE `msg_public_template` (`id` varchar(36) NOT NULL COMMENT '主鍵',`code` varchar(200) DEFAULT NULL COMMENT '模板編號',`sys_notice_content` mediumtext COMMENT '模板內容',`message_code` varchar(100) DEFAULT NULL COMMENT '短信編碼',`message_type_code` varchar(200) DEFAULT NULL COMMENT '消息類型 1站內信 2郵件 3短信',`name` varchar(200) DEFAULT NULL COMMENT '模板名稱',`notice_type_code` tinyint(2) DEFAULT NULL COMMENT '通知類型快碼',`service_module_code` varchar(100) DEFAULT NULL COMMENT '業務模塊快照編碼',`template_type_code` tinyint(7) DEFAULT NULL COMMENT '模板類型快照編碼',`title` varchar(200) DEFAULT NULL COMMENT '消息模板標題',`pc_url` varchar(255) DEFAULT NULL COMMENT 'PC-跳轉地址',`business_obj_id` varchar(36) DEFAULT NULL COMMENT '業務對象',`notice_enabled_flag` tinyint(2) DEFAULT NULL COMMENT '通知是否啟用(1.啟用/0.不啟用)',`create_date` datetime DEFAULT NULL COMMENT '創建時間',`creator` varchar(36) DEFAULT NULL COMMENT '創建人',`update_date` datetime DEFAULT NULL COMMENT '修改時間',`modifier` varchar(36) DEFAULT NULL COMMENT '最后修改人',PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='消息模板配置表';
消息發送記錄表
CREATE TABLE `msg_send_record` (`id` varchar(36) NOT NULL,`content` mediumtext COMMENT '發送內容',`msg_public_template_id` varchar(200) DEFAULT NULL COMMENT '消息模板Id',`read_flag` tinyint(2) DEFAULT NULL COMMENT '已讀狀態(1.已讀/0.未讀)',`receiver_name` varchar(100) DEFAULT NULL COMMENT '接收人姓名',`receiver_uid` varchar(36) DEFAULT NULL COMMENT '接收人主鍵',`send_time` datetime DEFAULT NULL COMMENT '發送時間',`send_type` tinyint(2) DEFAULT NULL COMMENT '通知渠道 1站內信 2郵件 3短信',`status` tinyint(2) DEFAULT NULL COMMENT '發送狀態(1.發送中/2.發送成功/3.發送失敗)',`title` varchar(200) DEFAULT NULL COMMENT '發送主題',`business_id` varchar(36) DEFAULT NULL COMMENT '業務id(跳轉頁面鏈接可以拼接相關id跳轉)',`create_date` datetime DEFAULT NULL COMMENT '創建時間',`creator` varchar(36) DEFAULT NULL COMMENT '創建人',`update_date` datetime DEFAULT NULL COMMENT '修改時間',`modifier` varchar(36) DEFAULT NULL COMMENT '最后修改人',`error_msg` varchar(1000) DEFAULT NULL COMMENT '錯誤信息',PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='消息發送記錄表';
實現
如圖所示,經過策略模式設計如下,后續有新的消息類型增加,只需要新增一個具體策略類實現相關發送邏輯即可,無需修改原有的代碼,沒有違背開放封閉原則
- 消息發送類型的抽象策略類NoticeExchanger,規定了具體策略類必須重寫的抽象方法match(是否支持當前消息類型發送)、exchanger(處理消息發送),以及自己實現的saveMessageRecord方法(保存消息發送記錄)、parseMessage方法(解析模板內容和標題)
- 具體策略類EmailNoticeExchanger,負責郵件消息的發送
- 具體策略類StationNoticeExchanger,負責站內信的發送
- 具體策略類SmsNoticeExchanger,負責短信消息的發送
- 環境類NoticeServiceImpl,維護一個策略對象的引用集合,負責將消息發送請求委派給具體的策略對象執行
抽象策略類NoticeExchanger
public abstract class NoticeExchanger {@Resourceprivate MsgSendRecordService msgSendRecordService;/*** 是否支持當前消息類型發送(true-支持,false-不支持)* @param type 消息類型* @return*/public abstract boolean match(String type);/*** 處理消息發送** @param map 相關參數* @return*/public abstract boolean exchanger(Map<String, Object> map) throws Exception;/*** 使用Freemarker解析模板內容和標題** @param notice 相關參數* @return*/public Map<String, Object> parseMessage(Map<String, Object> notice){MsgPublicTemplate msgPublicTemplate = notice.get("msgPublicTemplate");if(msgPublicTemplate==null){throw new CommonException(ExceptionDefinition.TEMPLATE_NOT_FOUND);}if(msgPublicTemplate.getNoticeEnabledFlag().intValue()==0){throw new CommonException(ExceptionDefinition.TEMPLATE_NOT_ENABLED);}//freemarker解析模板,填充模板內容//標題String title=msgPublicTemplate.getTitle();//內容String sysNoticeContent = msgPublicTemplate.getContent();Map<String, Object> params = notice.get("params");try {title= FreemarkerUtils.generateContent(params,title);sysNoticeContent=FreemarkerUtils.generateContent(params,sysNoticeContent);} catch (Exception e) {throw new CommonException(ExceptionDefinition.TRANSFORMATION_OF_THE_TEMPLATE);}Map<String, Object> result = new HashMap<>();result.put("title", title);result.put("sysNoticeContent", sysNoticeContent);return result;}/*** 保存消息發送記錄** @param msgSendRecordDto 相關參數* @return*/public void saveSendMessage(MsgSendRecordDto msgSendRecordDto){// ...參數校驗String [] ids = msgSendRecordDto.getUserId().split(",");String [] names = msgSendRecordDto.getUserName().split(",");// ...參數填充//是否多個用戶if (ids.length == 0) {msgSendRecord.setReceiverName(msgSendRecordDto.getUserName()).setReceiverUid(msgSendRecordDto.getUserId());msgSendRecordService.save(msgSendRecord);}if (ids.length > 0) {List<MsgSendRecord> msgSendRecordList = Lists.newArrayList();for (int i = 0; i < ids.length; i++) {MsgSendRecord data = BeanUtils.copyProperties(msgSendRecordDto, MsgSendRecord.class);data.setReceiverUid(ids[i]);data.setReceiverName(names[i]);msgSendRecordList.add(data);}msgSendRecordService.saveBatch(msgSendRecordList);} }
}
具體策略類EmailNoticeExchanger
發送郵件
@Component
public class EmailNoticeExchanger extends NoticeExchanger {private Logger logger = LoggerFactory.getLogger(EmailNoticeExchanger.class);@Autowiredprivate ISendEmailService sendEmailService;@Autowiredprivate MsgConfigurationMapper msgConfigurationMapper;/*** 是否支持郵件發送** @param type 消息類型* @return*/@Overridepublic boolean match(String type) {if (!String.valueOf(SendTypeEnum.EMAIL.getItem()).equals(type)) {return false;}return true;}/*** 處理消息發送** @param map 相關參數* @return*/@Overridepublic boolean exchanger(Map<String, Object> map) throws Exception {EmailNotice notice = new EmailNotice();BeanUtils.populate(notice, map);String code = notice.getCode();Map<String, Object> params = notice.getParams();// 解析模板內容和標題Map<String, Object> objectMap = parseMessage(map);String title = objectMap.get("title") == null ? "" : objectMap.get("title").toString();String sysNoticeContent = objectMap.get("sysNoticeContent") == null ? "" : objectMap.get("sysNoticeContent").toString();try {// 查詢郵箱配置MsgConfiguration msgConfiguration = 省略...if (msgConfiguration == null) {throw new CommonException(ExceptionDefinition.NO_LAUNCH_CONFIGURATION);}// 組裝參數發送郵件EmailConfig emailConfig = new EmailConfig();emailConfig.setUsername(msgConfiguration.getUsername());emailConfig.setPassword(msgConfiguration.getPassword());emailConfig.setMailServerHost(msgConfiguration.getIp());emailConfig.setMailServerPort(msgConfiguration.getPort());emailConfig.setProtocol(msgConfiguration.getProtocol());emailConfig.setFromAddress(msgConfiguration.getUsername());MailData mailData = new MailData();mailData.setSubject(title);mailData.setContent(sysNoticeContent);mailData.setToAddresss(notice.getToAddress());mailData.setCcAddresss(notice.getCcAddress());//發送郵件sendEmailService.sendMail(mailData, emailConfig);// 省略組裝參數...// 保存發送記錄saveSendMessage(msgSendRecordDto);logger.info("send email success!");return true;} catch (Exception e) {logger.error(e.getMessage());// 省略組裝參數...// 保存發送記錄saveSendMessage(msgSendRecordDto);return false;}return false;}
}
具體策略類StationNoticeExchanger
發送站內信
@Component
public class StationNoticeExchanger extends NoticeExchanger {private Logger logger = LoggerFactory.getLogger(StationNoticeExchanger.class); /*** 是否支持站內信發送** @param type 消息類型* @return*/@Overridepublic boolean match(String type) {if (!String.valueOf(SendTypeEnum.STATION.getItem()).equals(type)) {return false;}return true;}/*** 處理消息發送** @param map 相關參數* @return*/@Overridepublic boolean exchanger(Map<String, Object> map) throws Exception {logger.info("=========== send station begin !========================");StationNotice notice = new StationNotice();BeanUtils.populate(notice, map);// 解析模板內容和標題Map<String, Object> objectMap = parseMessage(map);String title = objectMap.get("title") == null ? "" : objectMap.get("title").toString();String sysNoticeContent = objectMap.get("sysNoticeContent") == null ? "" : objectMap.get("sysNoticeContent").toString();// 發送站內信即保存發送記錄即可,記錄類型為站內信MsgSendRecordDto msgSendRecordDto = new MsgSendRecordDto();// 省略組裝參數...// 保存發送記錄saveSendMessage(msgSendRecordDto);logger.info("=================send station success!==========================");return true;}
}
具體策略類SmsNoticeExchanger
發送短信
@Component
public class SmsNoticeExchanger extends NoticeExchanger{private Logger logger = LoggerFactory.getLogger(SmsNoticeExchanger.class); @Autowiredprivate ISendSmsService sendSmsService;@Autowiredprivate MsgConfigurationMapper msgConfigurationMapper;@Overridepublic boolean match(String type) {if(!String.valueOf(SendTypeEnum.SMS.getItem()).equals(type)){return false;}return true;}@Overridepublic boolean exchanger(Map<String, Object> map) {SmsNotice notice = new SmsNotice();BeanUtils.populate(notice, map);// 解析模板內容和標題,這里的模板內容和標題只在發送記錄使用,短信的模板內容配置在了阿里云短信服務Map<String, Object> objectMap = parseMessage(map);String title = objectMap.get("title") == null ? "" : objectMap.get("title").toString();String sysNoticeContent = objectMap.get("sysNoticeContent") == null ? "" : objectMap.get("sysNoticeContent").toString();try {// 查詢短信配置MsgConfiguration msgConfiguration = 省略...if(msgMailConfiguration == null){throw new CommonException(ExceptionDefinition.SEND_CHANNELS);}logger.info("send sms success begin !");//發送短信,填充阿里云用戶名、密碼、短信模板編碼、參數等等,調用阿里云api發送短信sendSmsService.sendSms(notice, msgConfiguration);// 省略組裝參數...// 保存發送記錄saveSendMessage(msgSendRecordDto);logger.info("send sms success!");} catch (Exception e) {// 省略組裝參數...// 保存發送記錄saveSendMessage(msgSendRecordDto);logger.error(e.getMessage());throw new CommonException(ExceptionDefinition.SEND_SMS_EXCEPTIONS);}return true;}
}
消息類型枚舉類
public enum SendTypeEnum {/*** 通知渠道類型*/STATION(1,"站內信"),EMAIL(2,"郵件"),SMS(3,"短信");private int item;private String itemName;SendTypeEnum(int item, String itemName) {this.item = item;this.itemName = itemName;}public int getItem() {return item;}public void setItem(int item) {this.item = item;}public String getItemName() {return itemName;}public void setItemName(String itemName) {this.itemName = itemName;}public static String getItemName(int item){for (SendTypeEnum es : SendTypeEnum.values()){if(item == es.getItem()){return es.getItemName();}}return "";}
}
環境類NoticeServiceImpl
負責將消息發送請求委派給具體的策略對象執行
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService,ApplicationContextAware {// 保存所有的消息策略類private Collection<NoticeExchanger> exchangers;// 線程池,異步發送消息private ExecutorService executorService;@Resourceprivate NoticeConvertUtils noticeConvertUtils;@Resourceprivate MsgPublicTemplateMapper msgPublicTemplateMapper;public NoticeServiceImpl(){// 創建線程池Integer availableProcessors = Runtime.getRuntime().availableProcessors();Integer numOfThreads = availableProcessors * 2;executorService = new ThreadPoolExecutor(availableProcessors,numOfThreads,100, TimeUnit.SECONDS,new LinkedBlockingDeque<>());}/*** 當前Bean初始化之前會執行當前方法,獲取所有的消息策略類*/@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// 獲取實現了NoticeExchanger接口的所有beanMap<String, NoticeExchanger> beansOfType = applicationContext.getBeansOfType(NoticeExchanger.class);this.exchangers=beansOfType.values();}@Override@Transactional(rollbackFor = Exception.class)public void sendMessage(NoticeParamDto noticeParamDto) {Map<String, Object> notice = null;try {// 將參數轉換成mapnotice = noticeConvertUtils.sendMessageIsParam(notice);} catch (Exception e) {log.error("消息發送失敗,消息模板內容轉換失敗", e);throw new CommonException("消息發送失敗,消息模板內容轉換失敗", 999);}if(notice.get("code") == null){throw new CustomException(CommonCode.NO_TEMPLATE);}QueryWrapper<MsgPublicTemplate> queryWrapper = new QueryWrapper<>();queryWrapper.eq("code", notice.get("code").toString());MsgPublicTemplate msgPublicTemplate = msgPublicTemplateMapper.selectOne(queryWrapper);notice.put("msgPublicTemplate",msgPublicTemplate);if(msgPublicTemplate.getMessageTypeCode() == null){throw new CustomException(CommonCode.NO_TEMPLATE);}// 獲取當前消息模板的類型,以逗號隔開,由所有的消息策略類去匹配類型,匹配成功則提交任務給線程池異步執行String[] array = msgPublicTemplate.getMessageTypeCode().split(",");for(int i = 0; i < array.length; i++){if(StringUtils.isNotBlank(array[i])){exchangers.forEach(item->{if(item.match(array[i)){//開啟線程池處理log.info("發送站內信任務提交");executorService.submit(new NoticeTask(item,notice));log.info("發送站內信任務提交完成");}});}}}
}
noticeConvertUtils的sendMessageIsParam(notice)邏輯,主要將傳遞過來的參數轉成Map對象
public Map<String, Object> sendMessageIsParam(NoticeParamDto notice) throws Exception {Map<String, Object> map = new HashMap<>();map = this.convertToMap(notice.getParams(), map);Map<String, Object> returnMap = new HashMap<>();returnMap.put("businessId", notice.getBusinessId());returnMap.put("code", notice.getSendMessageCode());returnMap.put("phones", notice.getPhones());returnMap.put("toAddress", notice.getToAddress());if (StringUtils.isEmpty(notice.getCcAddress())) {returnMap.put("ccAddress", notice.getCcAddress());}returnMap.put("userId", notice.getUserId());returnMap.put("userName", notice.getUserName());returnMap.put("params", map);return returnMap;}
參數類NoticeParamDto
@Data
@Accessors(chain = true)
public class NoticeParamDto {/*** 消息id*/private String id;/*** 業務單據id*/private String businessId;/*** 消息模板編碼*/private String sendMessageCode;/*** 接收人手機號*/private String phones;/*** 接收人郵箱,多個以英文逗號分割*/private String toAddress;/*** 抄送人郵箱*/private String ccAddress;/*** 接收人賬號*/private String userId;/*** 接收人姓名*/private String userName;/*** 傳遞參數,字段名稱需和模板內容、標題一樣,否則解析模板內容、標題失敗*/private Object params;
}
任務類NoticeTask
@Slf4j
public class NoticeTask implements Callable<Boolean>{private NoticeExchanger noticeExchanger;private Map<String, Object> notice;public NoticeTask(NoticeExchanger noticeExchanger, Map<String, Object> notice){this.noticeExchanger=noticeExchanger;this.notice=notice;}@Overridepublic Boolean call() throws Exception {log.info("============發送消息任務開始=============");return noticeExchanger.exchanger(notice);}
}
至此,核心代碼已經介紹完成。由于這是一個獨立的服務,所以我寫了一個接口來調用NoticeServiceImpl的發送消息方法,然后再寫一個Feign接口提供給其他服務使用,調用者調用時只需要傳遞消息模板編碼、接收人消息等參數即可,無需在原來的代碼上寫上大量的內容拼接參數處理,實現了解耦,后續也更好維護。
當后續需要新增消息發送類型,比如要發送微信公眾號消息,接著擴展即可,新增一個微信公眾號消息發送的策略類和枚舉類型,寫對應邏輯即可,其他不需要變化,這樣就非常靈活,也變得易擴展、易維護了。
當然這里還可以優化,比如我上面代碼用了大量的map操作,有時候這些參數看著一頭霧水,應該封裝成實體類;再比如,我這里采用的是feign遠程調用,對于調用者來說還是同步調用,需要等待其發送消息完成,有一定的性能消耗,后續可以采用消息隊列進行優化,調用者將參數發送到消息隊列就返回客戶端提升用戶體驗,然后環境類監聽主題消費即可。當然引入消息隊列,還得考慮其中的常見問題(消息丟失、消息重復消費等等)。
好了,今天就講這么多了!