下载或导入数据失败时,向指定服务器报告错误,同时用邮件通知维护人员
parent
c160cca531
commit
4864bd2fbd
@ -0,0 +1,14 @@
|
||||
package com.htfp.weather.schedule;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author : shiyi
|
||||
* @Date : 2024/6/13 15:32
|
||||
* @Description : 异常报告内容
|
||||
*/
|
||||
@Data
|
||||
public class ErrorReport {
|
||||
String time;
|
||||
String message;
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.htfp.weather.schedule;
|
||||
|
||||
import com.htfp.weather.utils.HttpClientUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.retry.annotation.Backoff;
|
||||
import org.springframework.retry.annotation.Recover;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @Author : shiyi
|
||||
* @Date : 2024/6/13 10:57
|
||||
* @Description : 邮件发送服务
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ErrorReportService {
|
||||
@Autowired
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
@Value("${mail.send.from}")
|
||||
public String sender;// 发送者
|
||||
// 发送简单文本文件
|
||||
|
||||
@Retryable(value = {ReportException.class}, maxAttempts = 4, backoff = @Backoff(delay = 2000, multiplier = 2))
|
||||
public void sendSimpleEmail(final Mail mail) {
|
||||
try {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(sender);
|
||||
message.setTo(mail.getTos());
|
||||
message.setSubject(mail.getSubject());
|
||||
message.setText(mail.getContent());
|
||||
mailSender.send(message);
|
||||
log.info("[error report] 异常邮件发送成功,{} --> {}, 主题:{}", sender, mail.getTos(), mail.getSubject());
|
||||
} catch (Exception e) {
|
||||
log.error("[error report] 异常邮件发送失败, 重试中: {}", e.getMessage());
|
||||
throw new ReportException();
|
||||
}
|
||||
}
|
||||
|
||||
@Retryable(value = {ReportException.class}, maxAttempts = 4, backoff = @Backoff(delay = 2000, multiplier = 2))
|
||||
public void reportToRemote(final ErrorReport report) {
|
||||
try {
|
||||
// TODO 2024/6/13: 发送到指定服务器上
|
||||
// HttpClientUtils.sendPost();
|
||||
log.info("[error report] 异常报告发送成功:{}", report.message);
|
||||
} catch (Exception e) {
|
||||
log.error("[error report] 邮件发送失败, 重试中: {}", e.getMessage());
|
||||
throw new ReportException();
|
||||
}
|
||||
}
|
||||
|
||||
@Recover
|
||||
public void recover(ReportException e) {
|
||||
// recover 方法的返回值要和重试方法的返回值一致
|
||||
log.error("[error report]:异常通知发送重试到达最大次数,发送失败");
|
||||
}
|
||||
|
||||
public class ReportException extends RuntimeException {
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package com.htfp.weather.schedule;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author : shiyi
|
||||
* @Date : 2024/6/13 14:08
|
||||
* @Description : 邮件
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
public class Mail {
|
||||
// 邮件主题
|
||||
private String subject;
|
||||
// 邮件内容
|
||||
private String content;
|
||||
// 收件邮箱列表
|
||||
private String[] tos;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.htfp.weather.schedule;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @Author : shiyi
|
||||
* @Date : 2024/6/13 16:38
|
||||
* @Description :
|
||||
*/
|
||||
@SpringBootTest
|
||||
class GridDataProcessorTest {
|
||||
|
||||
@Resource
|
||||
GridDataProcessor gridDataProcessor;
|
||||
@Test
|
||||
void importToTableStore() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void download() {
|
||||
gridDataProcessor.download();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue