Commit 79898e55 by 杨林龙

添加自动重试发送邮件功能

1 parent 8301b7b2
...@@ -8,6 +8,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ...@@ -8,6 +8,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy; import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.EnableWebSocket;
...@@ -21,6 +22,7 @@ import static java.util.TimeZone.getTimeZone; ...@@ -21,6 +22,7 @@ import static java.util.TimeZone.getTimeZone;
/** /**
* @author payu * @author payu
*/ */
@EnableRetry
@EnableAsync @EnableAsync
@EnableWebSocket @EnableWebSocket
@EnableRestAdmin @EnableRestAdmin
......
...@@ -5,7 +5,12 @@ import cn.com.glsx.notify.modules.entity.MessageTemplate; ...@@ -5,7 +5,12 @@ import cn.com.glsx.notify.modules.entity.MessageTemplate;
import cn.com.glsx.notify.modules.mapper.MessageLogMapper; import cn.com.glsx.notify.modules.mapper.MessageLogMapper;
import cn.com.glsx.notify.modules.mapper.MessageTemplateMapper; import cn.com.glsx.notify.modules.mapper.MessageTemplateMapper;
import com.glsx.plat.mail.utils.MailUtils; import com.glsx.plat.mail.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.mail.MessagingException; import javax.mail.MessagingException;
...@@ -17,6 +22,7 @@ import java.util.Map; ...@@ -17,6 +22,7 @@ import java.util.Map;
/** /**
* @author payu * @author payu
*/ */
@Slf4j
@Service @Service
public class MailService { public class MailService {
...@@ -29,6 +35,9 @@ public class MailService { ...@@ -29,6 +35,9 @@ public class MailService {
@Autowired @Autowired
private MessageTemplateMapper messageTemplateMapper; private MessageTemplateMapper messageTemplateMapper;
@Autowired
private RetryService retryService;
/** /**
* 发送文本邮件 * 发送文本邮件
* *
...@@ -93,7 +102,7 @@ public class MailService { ...@@ -93,7 +102,7 @@ public class MailService {
//以逗号切割 兼容收件人为多个的情况 //以逗号切割 兼容收件人为多个的情况
String[] split = to.split(","); String[] split = to.split(",");
for (String toOne : split) { for (String toOne : split) {
mailUtils.sendTextMail(toOne, subject, content); retryService.sendMail(toOne, subject, content);
} }
MessageLog log = new MessageLog(); MessageLog log = new MessageLog();
......
package cn.com.glsx.notify.modules.service;
import com.glsx.plat.mail.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
/**
* springBoot 重试机制测试 service
*
* @author Yanglinlong
* @date 2022/9/19 11:29
*/
@Slf4j
@Service
public class RetryService {
@Autowired
private MailUtils mailUtils;
@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))
public void devide(double a, double b) {
log.info("开始进行除法运算");
if (b == 0) {
throw new RuntimeException();
}
log.info("{} / {} = {}", a, b, a / b);
}
/**
* 重试发送邮件
*
* @param toEmail
* @param subject
* @param content
*/
@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
public void sendMail(String toEmail, String subject, String content) {
log.info("发送邮件===toEmail:" + toEmail + ",subject:" + subject + ",content:" + content);
mailUtils.sendTextMail(toEmail, subject, content);
}
/**
* 重试机制次数耗尽,执行补偿机制,可以是邮件通知异常数据
*
* @return
*/
@Recover
public void recover() {
log.info("has try 3 times ,sendMail fail===");
}
}
package cn.com.glsx.notify.modules.service;
import cn.com.glsx.Application;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* spring boot 重试机制测试类
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RetryServiceTest {
@Autowired
private RetryService retryService;
@Test
public void retryTest() {
//int count = retryService.retry(-1);
retryService.devide(-1, 0);
//logger.info("库存为:" + count);
}
}
\ No newline at end of file \ No newline at end of file
...@@ -92,7 +92,8 @@ public class NotifyApplicationTests { ...@@ -92,7 +92,8 @@ public class NotifyApplicationTests {
@Test @Test
public void testMailOnlyContent() throws Exception { public void testMailOnlyContent() throws Exception {
mailService.sendSimpleMailOnlyContent("592761523@qq.com,yangll@didihu.com.cn", "notifycenter test" + DateUtils.format(new Date()), "杨林龙测试"); // mailService.sendSimpleMailOnlyContent("592761523@qq.com,yangll@didihu.com.cn", "notifycenter test" + DateUtils.format(new Date()), "杨林龙测试");
mailService.sendSimpleMailOnlyContent("yangll@didihu.com.cn", "notifycenter test" + DateUtils.format(new Date()), "杨林龙测试");
Thread.sleep(30000); Thread.sleep(30000);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!