使用命令行模式发送消息。
命令行模式网上介绍的有很多,就不再叙述 直接上实践
Invoker
钉钉消息命令执行者
/**
* @author haoran
*/
@Slf4j
@RequiredArgsConstructor
@Component
public class DingComponent {
@SneakyThrows
public void sendApiDingMsg(String title, String text, String uniqueId){
}
}
邮件消息执行者
/**
*/
@Slf4j
@RequiredArgsConstructor
@Component
public class EmailComponent {
/**
* @param email 郵箱
* @param userName 用戶名
* @param title 標題
* @param content 内容
*/
@SneakyThrows
public void sendDefaultPassword(String email, String userName, String title, String content) {
}
}
短信消息执行者
/**
* @author haoran
*/
@Data
@Component
@RequiredArgsConstructor
@Slf4j
public class SmsComponent {
public void send(String mobile, String msg) {
}
}
Command
public interface ISendMessageCmd {
/**
* 发送短消息
*/
void send(SendMessageDto sendMessageDto);
}
@Slf4j
@Component("ding")
@RequiredArgsConstructor
public class DingSendMessageCmd implements ISendMessageCmd {
private final DingComponent dingComponent;
@Override
public void send(SendMessageDto sendMessageDto) {
// TODO 实现具体业务
log.info("Send email msg: {} to receiveScope:{}, targetIds:{}", sendMessageDto.getMsg(), sendMessageDto.getReceiveScope(), sendMessageDto.getTargetIds());
for (String uniqueId : sendMessageDto.getTargetIds()) {
dingComponent.sendApiDingMsg(sendMessageDto.getTitle(),sendMessageDto.getMsg(),uniqueId);
}
}
}
@Slf4j
@Component("email")
@RequiredArgsConstructor
public class EmailSendMessageCmd implements ISendMessageCmd {
private final EmailComponent emailComponent;
@Override
public void send(SendMessageDto sendMessageDto) {
log.info("Send email msg: {} to receiveScope:{}, targetIds:{}", sendMessageDto.getMsg(), sendMessageDto.getReceiveScope(), sendMessageDto.getTargetIds());
for(String email:sendMessageDto.getTargetIds()){
emailComponent.sendDefaultPassword(email,sendMessageDto.getUserName(),sendMessageDto.getTitle(),sendMessageDto.getMsg());
}
}
}
@Slf4j
@Component("sms")
@RequiredArgsConstructor
public class SmsSendMessageCmd implements ISendMessageCmd {
private final SmsComponent smsComponent;
@Override
public void send(SendMessageDto sendMessageDto) {
// TODO 实现具体业务
log.info("Send sms msg: {} to receiveScope:{}, targetIds:{}", sendMessageDto.getMsg(), sendMessageDto.getReceiveScope(), sendMessageDto.getTargetIds());
for(String mobile:sendMessageDto.getTargetIds()){
smsComponent.send(mobile,sendMessageDto.getMsg());
}
}
}
插件扩展
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public static T getBean(Class clazz) {
return (T) applicationContext.getBean(clazz);
}
}
public enum MsgTypeEnum {
EMAIL("email", "邮件"),
DING("ding", "钉钉"),
SMS("sms", "短信");
@Getter
private String code;
@Getter
private String desc;
MsgTypeEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public ISendMessageCmd getInstance() {
return (ISendMessageCmd) SpringUtil.getBean(this.getCode());
}
}
开始调用
public static void main(String[] args) {
ISendMessageCmd instance = MsgTypeEnum.SMS.getInstance();
SendMessageDto sendMessageDto = new SendMessageDto();
sendMessageDto.setMsg("测试数据");
sendMessageDto.setTargetIds(Set.of(String.valueOf(loginId)));
instance.send(sendMessageDto);
}
public class SendMsgDTO implements Serializable {
/**
* 标题
*/
private String title;
/**
* 消息内容
*/
private String content;
/**
* 目标id,邮件方式为邮箱地址、钉钉方式为手机号、SMS方式为手机号
*/
private String targetId;
/**
* 消息发送渠道
*/
private MsgTypeEnum msgType;
}