Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
ne-shield-notifycenter
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 79898e55
authored
Sep 19, 2022
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加自动重试发送邮件功能
1 parent
8301b7b2
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
98 additions
and
2 deletions
src/main/java/cn/com/glsx/Application.java
src/main/java/cn/com/glsx/notify/modules/service/MailService.java
src/main/java/cn/com/glsx/notify/modules/service/RetryService.java
src/test/java/cn/com/glsx/notify/modules/service/RetryServiceTest.java
src/test/java/cn/com/glsx/test/NotifyApplicationTests.java
src/main/java/cn/com/glsx/Application.java
View file @
79898e5
...
...
@@ -8,6 +8,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.context.annotation.EnableMBeanExport
;
import
org.springframework.jmx.support.RegistrationPolicy
;
import
org.springframework.retry.annotation.EnableRetry
;
import
org.springframework.scheduling.annotation.EnableAsync
;
import
org.springframework.transaction.annotation.EnableTransactionManagement
;
import
org.springframework.web.socket.config.annotation.EnableWebSocket
;
...
...
@@ -21,6 +22,7 @@ import static java.util.TimeZone.getTimeZone;
/**
* @author payu
*/
@EnableRetry
@EnableAsync
@EnableWebSocket
@EnableRestAdmin
...
...
src/main/java/cn/com/glsx/notify/modules/service/MailService.java
View file @
79898e5
...
...
@@ -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.MessageTemplateMapper
;
import
com.glsx.plat.mail.utils.MailUtils
;
import
lombok.extern.slf4j.Slf4j
;
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
javax.mail.MessagingException
;
...
...
@@ -17,6 +22,7 @@ import java.util.Map;
/**
* @author payu
*/
@Slf4j
@Service
public
class
MailService
{
...
...
@@ -29,6 +35,9 @@ public class MailService {
@Autowired
private
MessageTemplateMapper
messageTemplateMapper
;
@Autowired
private
RetryService
retryService
;
/**
* 发送文本邮件
*
...
...
@@ -93,7 +102,7 @@ public class MailService {
//以逗号切割 兼容收件人为多个的情况
String
[]
split
=
to
.
split
(
","
);
for
(
String
toOne
:
split
)
{
mailUtils
.
sendText
Mail
(
toOne
,
subject
,
content
);
retryService
.
send
Mail
(
toOne
,
subject
,
content
);
}
MessageLog
log
=
new
MessageLog
();
...
...
src/main/java/cn/com/glsx/notify/modules/service/RetryService.java
0 → 100644
View file @
79898e5
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==="
);
}
}
src/test/java/cn/com/glsx/notify/modules/service/RetryServiceTest.java
0 → 100644
View file @
79898e5
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
src/test/java/cn/com/glsx/test/NotifyApplicationTests.java
View file @
79898e5
...
...
@@ -92,7 +92,8 @@ public class NotifyApplicationTests {
@Test
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
);
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment