parent
d752de723e
commit
51c7fe7358
|
|
@ -2,6 +2,7 @@ package com.xjs.business.api;
|
|||
|
||||
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.api.domain.NowWeather;
|
||||
import com.xjs.business.api.factory.RemoteWeatherFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
|
@ -21,7 +22,7 @@ import java.util.Map;
|
|||
public interface RemoteWeatherFeign {
|
||||
|
||||
@GetMapping("/weather/getWeatherForRPC")
|
||||
R getWeatherForRPC() ;
|
||||
R<NowWeather> getWeatherForRPC() ;
|
||||
|
||||
@GetMapping("/weather/getHistoryWeatherForRPC")
|
||||
R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
package com.xjs.business.api.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@Data
|
||||
public class NowWeather implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 省份名
|
||||
*/
|
||||
private String province;
|
||||
|
||||
/**
|
||||
* 城市名
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 城市编码
|
||||
*/
|
||||
private String adcode;
|
||||
|
||||
/**
|
||||
* 天气现象(汉字描述)
|
||||
*/
|
||||
private String weather;
|
||||
|
||||
/**
|
||||
* 实时气温,单位:摄氏度
|
||||
*/
|
||||
private String temperature;
|
||||
|
||||
/**
|
||||
* 风向描述
|
||||
*/
|
||||
private String winddirection;
|
||||
|
||||
/**
|
||||
* 风力级别,单位:级
|
||||
*/
|
||||
private String windpower;
|
||||
|
||||
/**
|
||||
* 空气湿度
|
||||
*/
|
||||
private String humidity;
|
||||
|
||||
/**
|
||||
* 数据发布的时间
|
||||
*/
|
||||
private Date reporttime;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.xjs.business.warning;
|
||||
|
||||
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
||||
import com.xjs.business.warning.factory.RemoteMailFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 远程调用预警服务邮件feign
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@FeignClient(contextId = "remoteMailFeign",
|
||||
value = ServiceNameConstants.BUSINESS_WARNING_SERVICE,
|
||||
fallbackFactory = RemoteMailFactory.class)
|
||||
public interface RemoteMailFeign {
|
||||
|
||||
@GetMapping("mail/send-weather-mail")
|
||||
void sendWeatherMailForRPC();
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.xjs.business.warning.factory;
|
||||
|
||||
import com.xjs.business.warning.RemoteMailFeign;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 远程调用预警服务邮件feign降级
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RemoteMailFactory implements FallbackFactory<RemoteMailFeign> {
|
||||
@Override
|
||||
public RemoteMailFeign create(Throwable cause) {
|
||||
return new RemoteMailFeign() {
|
||||
@Override
|
||||
public void sendWeatherMailForRPC() {
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -13,9 +13,9 @@ import org.springframework.stereotype.Component;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 调用预警服务降级处理
|
||||
* @author xiejs
|
||||
* @desc 调用预警服务降级处理
|
||||
* @create 2021-12-31
|
||||
* @since 2021-12-31
|
||||
*/
|
||||
@Component
|
||||
public class RemoteWarningCRUDFactory implements FallbackFactory<RemoteWarningCRUDFeign> {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.xjs.job.task.warn;
|
||||
|
||||
import com.xjs.business.warning.RemoteMailFeign;
|
||||
import com.xjs.job.aop.TaskLog;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 定时发送邮件任务
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@Component("MailTask")
|
||||
@Log4j2
|
||||
public class MailTask {
|
||||
|
||||
@Resource
|
||||
private RemoteMailFeign remoteMailFeign;
|
||||
|
||||
@TaskLog(name = "邮件天气播报任务")
|
||||
public void execution() {
|
||||
log.info("---------------邮件天气任务定时任务Start-------------------");
|
||||
remoteMailFeign.sendWeatherMailForRPC();
|
||||
log.info("---------------邮件天气任务定时任务End-------------------");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.xjs.job.task;
|
||||
package com.xjs.job.task.warn;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
|
|
@ -60,6 +60,11 @@ public class RedisConst {
|
|||
*/
|
||||
public static final String REPTILE_WEIXIN_LINK_COUNT = "bussiness:reptile:weixin.link.count";
|
||||
|
||||
/**
|
||||
* 邮件记录状态常量信息key
|
||||
*/
|
||||
public static final String MAIL_STATUS = "bussiness:mail:status";
|
||||
|
||||
|
||||
//--------------------------mall-key-----------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -60,9 +60,9 @@ public class WeatherController {
|
|||
|
||||
@GetMapping("getWeatherForRPC")
|
||||
@ApiOperation("远程调用获取天气信息")
|
||||
public R getWeatherForRPC() {
|
||||
public R<NowWeather> getWeatherForRPC() {
|
||||
NowWeather nowWeather = weatherService.save();
|
||||
return Objects.nonNull(nowWeather.getCity()) ? R.ok() : R.fail();
|
||||
return Objects.nonNull(nowWeather.getCity()) ? R.ok(nowWeather) : R.fail();
|
||||
}
|
||||
|
||||
@GetMapping("getHistoryWeatherForRPC")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package com.xjs.controller;
|
||||
|
||||
import com.xjs.service.MailService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 邮件服务控制器
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("mail")
|
||||
@Api(tags = "业务模块-邮件管理")
|
||||
public class MailController {
|
||||
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
|
||||
|
||||
//-----------------------------------远程调用------------------------------------
|
||||
@GetMapping("/send-weather-mail")
|
||||
@ApiOperation("发送天气邮件ForRPC")
|
||||
public void sendWeatherMailForRPC() {
|
||||
mailService.sendWeatherMail();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -28,9 +28,50 @@ public class MailBean implements Serializable {
|
|||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 附件地址
|
||||
*/
|
||||
private String absolutePath;
|
||||
|
||||
/**
|
||||
* 邮件发送类型
|
||||
*/
|
||||
private MailType mailType;
|
||||
|
||||
/**
|
||||
* 内部类-邮件发送类型
|
||||
*/
|
||||
public enum MailType {
|
||||
SIMPLE(1, "文本邮件"),
|
||||
HTML(2, "HTML邮件"),
|
||||
ATTACHMENT(3, "附件邮件"),
|
||||
INLINE(4, "静态资源邮件"),
|
||||
TEMPLATE(5, "模板邮件");
|
||||
|
||||
|
||||
private final int code;
|
||||
private final String msg;
|
||||
|
||||
MailType(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.xjs.server;
|
||||
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.xjs.domain.mall.MailBean;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -13,8 +14,12 @@ import org.thymeleaf.TemplateEngine;
|
|||
import org.thymeleaf.context.Context;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xjs.consts.RedisConst.MAIL_STATUS;
|
||||
|
||||
/**
|
||||
* 邮箱发送工具
|
||||
|
|
@ -36,8 +41,42 @@ public class MailServer {
|
|||
@Autowired
|
||||
private TemplateEngine templateEngine;
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
// todo 优化 邮箱发送失败重试机制、防止邮件被识别为垃圾邮件,固定时间内发送邮件的限制等。
|
||||
|
||||
/**
|
||||
* 发送邮件统一出口
|
||||
*
|
||||
* @param mailBean 邮箱实体
|
||||
*/
|
||||
public Boolean sendMail(MailBean mailBean) {
|
||||
|
||||
if (redisService.hasKey(MAIL_SENDER)) {
|
||||
throw new RuntimeException("邮件发送频繁!请稍后重试!");
|
||||
}
|
||||
|
||||
int code = mailBean.getMailType().getCode();
|
||||
try {
|
||||
if (code == MailBean.MailType.SIMPLE.getCode()) {
|
||||
this.sendSimpleMail(mailBean);
|
||||
} else if (code == MailBean.MailType.HTML.getCode()) {
|
||||
this.sendHTMLMail(mailBean);
|
||||
} else if (code == MailBean.MailType.ATTACHMENT.getCode()) {
|
||||
this.sendAttachmentMail(mailBean);
|
||||
} else if (code == MailBean.MailType.INLINE.getCode()) {
|
||||
this.sendInlineMail(mailBean);
|
||||
} else if (code == MailBean.MailType.TEMPLATE.getCode()) {
|
||||
this.sendTempLateMail(mailBean);
|
||||
}
|
||||
|
||||
redisService.setCacheObject(MAIL_STATUS, true, 3L, TimeUnit.SECONDS);
|
||||
|
||||
return Boolean.TRUE;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("邮件发送失败");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +84,7 @@ public class MailServer {
|
|||
*
|
||||
* @param mailBean 邮箱实体
|
||||
*/
|
||||
public void sendSimpleMail(MailBean mailBean) {
|
||||
private void sendSimpleMail(MailBean mailBean) throws Exception {
|
||||
try {
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setFrom(MAIL_SENDER);
|
||||
|
|
@ -56,6 +95,7 @@ public class MailServer {
|
|||
javaMailSender.send(mailMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("文本邮件发送失败:{}", e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +104,7 @@ public class MailServer {
|
|||
*
|
||||
* @param mailBean 邮箱实体
|
||||
*/
|
||||
public void sendHTMLMail(MailBean mailBean) {
|
||||
private void sendHTMLMail(MailBean mailBean) throws MessagingException {
|
||||
MimeMessage mimeMailMessage = null;
|
||||
try {
|
||||
mimeMailMessage = javaMailSender.createMimeMessage();
|
||||
|
|
@ -79,6 +119,7 @@ public class MailServer {
|
|||
javaMailSender.send(mimeMailMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("HTML格式邮件发送失败:{}", e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +129,7 @@ public class MailServer {
|
|||
*
|
||||
* @param mailBean 邮箱实体
|
||||
*/
|
||||
public void sendAttachmentMail(MailBean mailBean) {
|
||||
private void sendAttachmentMail(MailBean mailBean) throws MessagingException {
|
||||
MimeMessage mimeMailMessage = null;
|
||||
try {
|
||||
mimeMailMessage = javaMailSender.createMimeMessage();
|
||||
|
|
@ -110,6 +151,7 @@ public class MailServer {
|
|||
javaMailSender.send(mimeMailMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("附件格式邮件发送失败:{}", e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +161,7 @@ public class MailServer {
|
|||
*
|
||||
* @param mailBean 邮箱实体
|
||||
*/
|
||||
public void sendInlineMail(MailBean mailBean) {
|
||||
private void sendInlineMail(MailBean mailBean) throws MessagingException {
|
||||
MimeMessage mimeMailMessage = null;
|
||||
try {
|
||||
mimeMailMessage = javaMailSender.createMimeMessage();
|
||||
|
|
@ -137,28 +179,23 @@ public class MailServer {
|
|||
javaMailSender.send(mimeMailMessage);
|
||||
} catch (Exception e) {
|
||||
log.error("静态资源格式邮件发送失败:{}", e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送Thymeleaf模版邮件
|
||||
*
|
||||
* @param recipient 接受者邮箱
|
||||
* @param name 用户名称
|
||||
* @param title 主体
|
||||
*/
|
||||
public void sendTempLateMail(String recipient, String name, String title) {
|
||||
private void sendTempLateMail(MailBean mailBean) throws MessagingException {
|
||||
//注意:Context 类是在org.thymeleaf.context.Context包下的。
|
||||
Context context = new Context();
|
||||
//html中填充动态属性值
|
||||
context.setVariable("username", name);
|
||||
context.setVariable("username", mailBean.getUserName());
|
||||
context.setVariable("url", "#");
|
||||
//注意:process第一个参数名称要和templates下的模板名称一致。要不然会报错
|
||||
//org.thymeleaf.exceptions.TemplateInputException: Error resolving template [email]
|
||||
String emailContent = templateEngine.process("email", context);
|
||||
MailBean mailBean = new MailBean();
|
||||
mailBean.setRecipient(recipient);
|
||||
mailBean.setSubject(title);
|
||||
mailBean.setContent(null);
|
||||
mailBean.setContent(emailContent);
|
||||
this.sendHTMLMail(mailBean);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package com.xjs.service;
|
||||
|
||||
/**
|
||||
* 邮件发送service接口
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
public interface MailService {
|
||||
|
||||
/**
|
||||
* 定时发送天气预报邮件
|
||||
* @return true/false
|
||||
*/
|
||||
Boolean sendWeatherMail();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package com.xjs.service.impl;
|
||||
|
||||
import com.ruoyi.common.core.constant.HttpStatus;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.api.RemoteWeatherFeign;
|
||||
import com.xjs.business.api.domain.NowWeather;
|
||||
import com.xjs.domain.mall.MailBean;
|
||||
import com.xjs.server.MailServer;
|
||||
import com.xjs.service.MailService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 邮件发送service接口实现
|
||||
* @author xiejs
|
||||
* @since 2022-04-13
|
||||
*/
|
||||
@Service
|
||||
public class MailServiceImpl implements MailService {
|
||||
|
||||
@Resource
|
||||
private RemoteWeatherFeign remoteWeatherFeign;
|
||||
@Autowired
|
||||
private MailServer mailServer;
|
||||
|
||||
@Override
|
||||
public Boolean sendWeatherMail() {
|
||||
|
||||
R<NowWeather> r = remoteWeatherFeign.getWeatherForRPC();
|
||||
if (r.getCode() == HttpStatus.SUCCESS) {
|
||||
NowWeather nowWeather = r.getData();
|
||||
|
||||
MailBean mailBean = new MailBean();
|
||||
mailBean.setUserName("用户");
|
||||
mailBean.setSubject("天气播报");
|
||||
mailBean.setRecipient("1294405880@qq.com");
|
||||
mailBean.setMailType(MailBean.MailType.HTML);
|
||||
|
||||
mailBean.setContent("<h3>当前"+nowWeather.getCity()+"的天气:"+nowWeather.getTemperature()+"℃</h3>");
|
||||
return mailServer.sendMail(mailBean);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -36,6 +36,5 @@ class MailServerTest {
|
|||
|
||||
@Test
|
||||
void sendTempLateMail() {
|
||||
mailServer.sendTempLateMail("1294405880@qq.com","生哥","这是标题");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue