消息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) {
//具体业务,发送人,邮件标题等参数,邮件发送实现
}
}